vimtex icon indicating copy to clipboard operation
vimtex copied to clipboard

[help] Get details of citation under cursor

Open berryoptml opened this issue 2 years ago • 7 comments

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 ?

berryoptml avatar May 04 '24 05:05 berryoptml

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

lervag avatar May 04 '24 21:05 lervag

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"})

berryoptml avatar May 05 '24 18:05 berryoptml

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'})

lervag avatar May 05 '24 18:05 lervag

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)

berryoptml avatar May 05 '24 19:05 berryoptml

You can try to change the g:vimtex_context_pdf_viewer option.

lervag avatar May 05 '24 19:05 lervag

Thank You for bearing with me 🚀

probably just a reminder to self about reading doc properly!

berryoptml avatar May 05 '24 19:05 berryoptml

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. ;)

lervag avatar May 05 '24 19:05 lervag