[Tex/LaTex] How to setup synctex with vim, pdflatex and an open-source PDF-viewer under Linux

forward-inverse-searchpdfpdftexvim

Inspired by the comments to this answer I am curious how to actually setup and use forward/reverse (inverse) search with vim, pdflatex and an open-source PDF-viewer under Linux.

Btw, to give more details:

Forward search means that when you at some location in your text editor you issue some command and auto-magically the PDF-viewer jumps to the corresponding location in the typeset PDF. Perhaps even marks it somehow.

Reverse search means that issuing a command in your PDF-Viewer (e.g. clicking somewhere in the document), instructs the PDF-viewer to send a command to your editor that then jumps to the corresponding source location.

Edit: Currently I am not using any latex plugin in vim. And usually the LaTeX documents I edit with vim make use of \include or \input a lot.

Best Answer

It would help to know which, if any, LaTeX plugin you're using for vim. (E.g., the latex-suite, vim-auctex, latex-box, etc.)

Next, as far as viewer choice, the only widely used open source PDF viewer for Linux which currently supports SyncTeX well out of the box is Okular. That's probably your best choice.

There are instructions fo setting up SyncTeX with Okular with the vim-latex plugin here, and some related observations here. I had mixed results following those directions. Here's what seems to be important.

  1. Be sure that your LaTeX compilation method (which will depend on your plugin) calls pdflatex (or xelatex or whatever) with the -synctex=1 flag.

  2. I think something like this should suffice for Forward Searches with Okular, though it might be better to try to rewrite or modify the forward search function for your plugin (there's some info on that in one of the links above). Put this in your .vimrc (and change the mapping to whatever you like).

    function! SyncTexForward()
         let execstr = "silent !okular --unique %:p:r.pdf\\#src:".line(".")."%:p &"
         exec execstr
    endfunction
    nmap <Leader>f :call SyncTexForward()<CR>
    
  3. For reverse searches, set the editor line in Okular to gvim --servername GVIM --remote +%l %f. It might also work to use gvim --servername GVIM --remote-send "<Esc>%lgg" if you only use it with the file already open. Change the servername to whatever you use. (Not sure if it's different with regular vim, but this doesn't make much sense out of a graphical environment.)

I do not have Okular installed right now, however, so I could not test any of that. (And the links I gave earlier have slightly different advice which is worth trying.) I really hope someone with both Okular and gvim installed can test this advice, and correct where I went wrong.

And all of that advice isn't going to work well if you're using subdocuments called through \input{...} or \include{...}, where the PDF name doesn't match the name of the document you're editing. There are ways around that, but it would require knowing more about what LaTeX plugins and methods you're already using, if any.

However, other choices are kinda/sorta already available. The next version of evince will support SyncTeX through D-Bus, and apparently someone is already working on a plugin for vim to make use of it. Details here. However, it's very unlikely that this version of evince is already available for your Linux distribution, and there may be some problems with it.

There's an old fork of an old version evince that provides synctex support; there are instructions that come with that detail how to set this up with gvim. It works fine. It's easy to set up if you're using Arch Linux, since this is in the AUR. If you're not, I don't know how hard it would be to compile. (I used to use Ubuntu before Arch, and couldn't get it working there, but that may have been my ignorance.)

Next, I wrote some scripts that provide very limited, very poor, but still better than nothing, synctex support between gvim and the open-source vim-like PDF viewer Zathura, which uses vim-like keybindings. You'll find them mentioned and detailed in this thread in the Arch forums here. (post #370)

Finally, I think this kind of stuff will work its way into the major LaTeX plugins for vim soon, and then you don't have to resort to so much trial and error.

Related Question