lervag / vimtex

VimTeX: A modern Vim and neovim filetype plugin for LaTeX files.
MIT License
5.29k stars 386 forks source link

[help] Get details of citation under cursor #2946

Closed anandkumar89 closed 2 months ago

anandkumar89 commented 2 months ago

I'm trying to write a script that could open pdf linked to citation under cursor. Since vimtex parses bibtex files linked, is it possible to get entire details for the citation key under cursor ?

lervag commented 2 months ago

Yes. You can use vimtex#cite#get_entry() for that.

anandkumar89 commented 2 months ago

Thanks a ton @lervag


for anyone stumbling here, looking for similar. I use zotero with better-bibtex to export bib files (file entry is automatically included). keymap below allows to open corresponding pdf when cursor is on the citation key.

vim.keymap.set('n', '<leader>zo', function()
        local citation_entry = vim.api.nvim_call_function('vimtex#cite#get_entry', {})
        local file_path = citation_entry['file']
        job:new({
                command = "/path/to/sioyek",
                args = { file_path },
                detached = true,
        }):start()
end, {desc="Open pdf related to citations"})
lervag commented 2 months ago

Thanks a ton @lervag

No problem; thanks for the request!

Notice, just in case you were not aware, that there is already a context feature that sort of provides what you are looking for here. See :help vimtex-context-cite.

for anyone stumbling here, looking for similar. I use zotero with better-bibtex to export bib files (file entry is automatically included). keymap below allows to open corresponding pdf when cursor is on the citation key. …

I think this is a slightly improved version:

vim.keymap.set('n', '<leader>zo', function()
  local citation_entry = vim.fn['vimtex#cite#get_entry']()
  job:new({
    command = '/path/to/sioyek',
    args = { citation_entry.file },
    detached = true,
  }):start()
end, {desc='Open pdf related to citations'})
anandkumar89 commented 2 months ago

Thanks for the improved version!

I wasn't aware of context menu support, this issue seems redundant in the context. I tried, butopen pdf doesn't do anything for me, I'm able to open url, Show Entry etc. (I'm using sioyek with vimtex, and works fine)

lervag commented 2 months ago

You can try to change the g:vimtex_context_pdf_viewer option.

anandkumar89 commented 2 months ago

Thank You for bearing with me 🚀

probably just a reminder to self about reading doc properly!

lervag commented 2 months ago

No problem :)

Notice, using the context function means several key presses. If you frequently want to just open the corresponding pdf, then having the direct version you asked for in this issue makes sense. That's why I didn't immediately mention the context feature. ;)