Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom formats for latex citation #64

Open
benbrastmckie opened this issue Nov 10, 2023 · 9 comments
Open

Custom formats for latex citation #64

benbrastmckie opened this issue Nov 10, 2023 · 9 comments

Comments

@benbrastmckie
Copy link

benbrastmckie commented Nov 10, 2023

I believe to be doing the right thing, but am not getting the right results upon running the command :Telescope bibtex format=CiteT. Here is what I have in the config:

      custom_formats = {
        {id = 'citet', cite_maker = '\\citet{%s}'}
      },
      -- Custom format for citation label
      format = 'citet',
      -- Format to use for citation label.
      -- Try to match the filetype by default, or use 'plain'

I have also tested it with cite_maker = '#%s#' or with parentheses but would only get the citation key alone instead of wrapped inside the citation command that I want to be added as well.

I have also tried the command :Telescope bibtex but still get \cite{KEY} instead of \citet{KEY} as desired. Any pointers would be much appreciated.

@noahares
Copy link
Collaborator

Thanks for posting about this issue. That seems like some sort of regression with telescope load-order. I tested it on my system and had the same issue. Which plugin manager are you using? I fixed the issue by removing the require"telescope".load_extension("bibtex") from the config section in lazy.nvim and put it bellow where I call telescope.setup(). Otherwise, the custom_formats table was empty upon initializing telescope-bibtex. I will adjust the README for that. Let me know if that works for you as well

@benbrastmckie
Copy link
Author

Thanks for your response! I am using packer (for now) and would like to try to emulate what you suggest above. Right now I have this in my config file

local status_ok, telescope = pcall(require, "telescope")
if not status_ok then
  return
end

local actions = require "telescope.actions"

telescope.setup {
  defaults = {
  ...

  load_extensions = { "yank_history", "bibtex" },
  extensions = {
    bibtex = {
      depth = 1,
      -- Depth for the *.bib file
      custom_formats = {
        {id = 'citet', cite_maker = '\\citet{%s}'}
      },
      -- Custom format for citation label
      format = 'citet',
...

When you say 'put it below telescope.setup()' are you suggesting that I delete load_extensions = { "yank_history", "bibtex" }, from telescope.setup() and instead create a new block below like:

require"telescope".load_extensions = { "yank_history", "bibtex" }

I tried this but it gave me an error and as I'm sure you can tell, I'm not experienced enough to know what I'm doing wrong.

Thanks again for your help,
Ben

@noahares
Copy link
Collaborator

I didnt know you could have load_extensions in the telescope setup function... is that a new feature? But yes, I would suggest you remove "bibtex" there (you can keep "yank_history" there if it works) and then add require"telescope".load_extension("bibtex") (note singular) right below telescope.setup{}

@benbrastmckie
Copy link
Author

I tried adding require"telescope".load_extension = { "yank_history", "bibtex" } after telescope.setup{}, but it only worked with the plural load_extensions. Otherwise I got the error:

packer.nvim: Error running config for telecope-bibtex.nvim: [string "..."]:0: attempt to call field 'load_extension' (a table value)

I noticed that it didn't care if load_extensions was plural when inside telescope.setup{}. It would pull up the Telescope search either way, but I'm still getting citations of the form acharya2017 instead of \citet{acharya2017} upon running the command :Telescope bibtex format=citet. Is there anything else you might suggest that I try?

Thanks for the help!

@noahares
Copy link
Collaborator

Don't use "= {...} after setup, you need to call the function: require"telescope".load_extension("bibtex")

@benbrastmckie
Copy link
Author

Shoots, still no dice. Here is the full config for Telescope:

local status_ok, telescope = pcall(require, "telescope")
if not status_ok then
  return
end

local actions = require "telescope.actions"

telescope.setup {
  defaults = {
    prompt_prefix = " ",
    selection_caret = " ",
    path_display = { "smart" },
    mappings = {
      i = {
        ["<C-n>"] = actions.cycle_history_next,
        ["<C-p>"] = actions.cycle_history_prev,

        ["<C-j>"] = actions.move_selection_next,
        ["<C-k>"] = actions.move_selection_previous,

        ["<C-c>"] = actions.close,

        ["<Down>"] = actions.move_selection_next,
        ["<Up>"] = actions.move_selection_previous,

        ["<CR>"] = actions.select_default,
        ["<C-x>"] = actions.select_horizontal,
        ["<C-v>"] = actions.select_vertical,
        ["<C-t>"] = actions.select_tab,

        ["<C-u>"] = actions.preview_scrolling_up,
        ["<C-d>"] = actions.preview_scrolling_down,

        ["<PageUp>"] = actions.results_scrolling_up,
        ["<PageDown>"] = actions.results_scrolling_down,

        ["<Tab>"] = actions.toggle_selection + actions.move_selection_worse,
        ["<S-Tab>"] = actions.toggle_selection + actions.move_selection_better,
        ["<C-q>"] = actions.send_to_qflist + actions.open_qflist,
        ["<M-q>"] = actions.send_selected_to_qflist + actions.open_qflist,
        ["<C-l>"] = actions.complete_tag,
        ["<C-_>"] = actions.which_key, -- keys from pressing <C-/>
      },

      n = {
        ["<esc>"] = actions.close,
        ["<CR>"] = actions.select_default,
        ["<C-x>"] = actions.select_horizontal,
        ["<C-v>"] = actions.select_vertical,
        ["<C-t>"] = actions.select_tab,

        ["<Tab>"] = actions.toggle_selection + actions.move_selection_worse,
        ["<S-Tab>"] = actions.toggle_selection + actions.move_selection_better,
        ["<C-q>"] = actions.send_to_qflist + actions.open_qflist,
        ["<M-q>"] = actions.send_selected_to_qflist + actions.open_qflist,

        ["j"] = actions.move_selection_next,
        ["k"] = actions.move_selection_previous,
        -- ["H"] = actions.move_to_top,
        -- ["M"] = actions.move_to_middle,
        -- ["L"] = actions.move_to_bottom,

        ["<Down>"] = actions.move_selection_next,
        ["<Up>"] = actions.move_selection_previous,
        ["gg"] = actions.move_to_top,
        ["G"] = actions.move_to_bottom,

        ["<C-u>"] = actions.preview_scrolling_up,
        ["<C-d>"] = actions.preview_scrolling_down,

        ["<PageUp>"] = actions.results_scrolling_up,
        ["<PageDown>"] = actions.results_scrolling_down,

        ["?"] = actions.which_key,
      },
    },
  },
  pickers = {
    -- Default configuration for builtin pickers goes here:
    -- picker_name = {
    --   picker_config_key = value,
    --   ...
    -- }
    -- Now the picker_config_key will be applied every time you call this
    -- builtin picker
  },
  load_extensions = { "yank_history" },
  extensions = {
    bibtex = {
      depth = 1,
      -- Depth for the *.bib file
      global_files = {'~/texmf/bibtex/bib/Zotero.bib'},
      -- Path to global bibliographies (placed outside of the project)
      search_keys = { 'author', 'year', 'title' },
      -- Define the search keys to use in the picker
      citation_format = '{{author}} ({{year}}), {{title}}.',
      -- Template for the formatted citation
      citation_trim_firstname = true,
      -- Only use initials for the authors first name
      citation_max_auth = 2,
      -- Max number of authors to write in the formatted citation
      -- following authors will be replaced by "et al."
      custom_formats = {
        {id = 'citet', cite_maker = '\\citet{%s}'}
      },
      -- Custom format for citation label
      format = 'citet',
      -- Format to use for citation label.
      -- Try to match the filetype by default, or use 'plain'
      context = true,
      -- Context awareness disabled by default
      context_fallback = true,
      -- Fallback to global/directory .bib files if context not found
      -- This setting has no effect if context = false
      wrap = false,
      -- Wrapping in the preview window is disabled by default
    },
    -- Your extension configuration goes here:
    -- extension_name = {
    --   extension_config_key = value,
    -- }
    -- please take a look at the readme of the extension you want to configure
  },
}

require"telescope".load_extension("bibtex")

Would love to know if I'm making some mistake. Thanks again for your help.

@noahares
Copy link
Collaborator

I did some more testing and there seems to be a deeper issue with how the custom formats are set. I will look into this when I have the time, but for the next 2 weeks I cannot guarantee it. Thanks again for bringing this to my attention.

@noahares
Copy link
Collaborator

For now I pushed a quick fix so you can do :Telescope bibtex format_string=\\citet{%s} . I hope this helps somewhat.

@benbrastmckie
Copy link
Author

Amazing, that works great! Thanks so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants