LaTeX forward/inverse searches with Zathura

2014-03-10

I use zathura to view PDF files. As of version 0.2.7 it supports full forward / inverse searches with LaTeX. Namely, you can “control click” in any place on the PDF file, and it will open the TeX source file in your editor, and move to the corresponding location. Conversely, you can configure your editor so that pressing a particular key when editing your TeX file will open the PDF file and highlight the corresponding location.

General usage

While doing this with zathura is possible, the documentation SUCKS. Here’s how: Edit ~/.config/zathura/zathurarc and add the following lines:

set synctex true
set synctex-editor-command "gvim +%{line} %{input}"

This only works on recent versions (0.3.4 or higher). Older versions require this to be given direction on the command line using -x (or -s).

If you you want to highlight line 193 column 1, then use:

zathura --synctex-forward 193:1:paper.tex paper.pdf

If you control click in the PDF, it will take you to the corresponding location in your LaTeX file by running your editor. It will, however, open a new instance of the editor every time you control click. This is obviously less than ideal, and the scripts below can help out.

Use with FVWM and VIM

If you use vim as your text editor and fvwm as your window manager, I wrote two scripts to get rid of all sorts of quirks (e.g. opening the file twice in different windows, not raising / focussing the PDF files, etc.):

  1. Save this script as szathura somewhere in your PATH. (If you’re not using fvwm as your window manager, then comment out the fvwm specific stuff.)

  2. Save this script as svim somewhere in your PATH. (Again, if you’re not using fvwm as your window manager, then comment out the fvwm specific stuff.)

  3. Edit ~/.config/zathura/zathurarc and add the lines:

    set synctex true
    set synctex-editor-command "svim -G +%{line} %{input}"
    
  4. Be sure you have the option -synctex=1 when you run LaTeX, otherwise NOTHING WILL WORK. If you’re running LaTeX through vim, add this option to your &makeprg. If you’re using latexmk, then edit ~/.latexmkrc and add the lines:

    $pdflatex = 'pdflatex -interaction=nonstopmode -synctex=1 %O %S';
    $pdf_mode = 1;
    
  5. Edit ~/.vimrc and add the lines:

    nnoremap <F9>
        \ :exec "!szathura %:r.pdf" line('.')  col('.') "% > /dev/null"<cr><cr>
    nnoremap <C-F9>
        \ :exec "!szathura %:r.pdf" > /dev/null 2>&1 &"<cr><cr>
    

    (Alternately, add them to ~/.vim/after/ftplugin/tex.vim with other fancy buffer local mappings.)

Now to use it:

  1. Edit your file with svim -G file.tex.

  2. Control click the PDF to go back to the TeX file. A new window will be opened if you’re not already editing it.

  3. Press F9 in vim to highlight the corresponding location in the PDF file and raise the window (a new window will be opened if you’re not already viewing it).

  4. Press <Ctrl-F9> in vim to open a new window showing the PDF in case you want multiple views.

🗫 Comments

  • Dino
    Works also without script

    Dino (2016-01-27 06:43:37 EST)

    Hi,

    you can get it to not open a new instance by changing the zathurarc config as follows:

    set synctex true
    set synctex-editor-command "gvim --remote-silent +%{line} %{input}"
    

    Hope this helps!

  • Arjun
    Thanks, that was really useful. How did you figure this out?

    Arjun (2016-07-16 19:01:44 EDT)

    Hi Gautam,

    Did you have to go through the source to find the “input” and “line” variables?

    Best Arjun

  • Gautam Iyer
    Re: Thanks, that was really useful. How did you figure this out?

    Gautam Iyer (2016-07-16 21:31:28 EDT)

    Did you have to go through the source to find the “input” and “line” variables?

    Thankfully no. It’s in the man page, but a little cryptic. Explicitly, the man page of zathura (under SYNCTEX SUPPORT) says “For convince zathura also knows how to parse the output of the synctex view command.” Now if you look in the help of synctex, the variables are documented. (You can access it by synctex help edit and synctex help view)

  • Laurent Dietrich
    And on vim ?

    Laurent Dietrich (2020-02-15 06:30:40 EST)

    Hi Gautam ! I don’t know if you remember me, I was at postdoc at the CNA a few years ago. Fun fact : I just found this page, trying to set up forward and backward sync with vim :)

    Your config works fine on gVim but not on Vim (set synctex-editor-command “gvim +%{line} %{input}”). Do you happen to know a way to set it up on basic Vim (for some weird reasons I prefer not to use gVim) ?

    Thanks a lot, hope you’re doing well at CMU.

  • Laurent Dietrich
    About my previous comment

    Laurent Dietrich (2020-02-15 07:02:31 EST)

    Hi Gautam, I forgot to mention that I also tried your FVWM solution but I cannot access your script ! Thanks.

  • Gautam Iyer
    Re: And on vim ?

    Gautam Iyer (2020-02-15 12:13:59 EST)

    Hi Laurant,

    Good to hear from you. Hope you’re doing well.

    Your config works fine on gVim but not on Vim (set synctex-editor-command gvim +%{line} %{input}). Do you happen to know a way to set it up on basic Vim (for some weird reasons I prefer not to use gVim) ?

    I actually use it myself on basic vim, and don’t like using gvim either. I fixed the links to the scripts szathura and svim. If you use those, you should be able to get it working with a console vim.

    Best,

    Gautam

  • vimtex+zathura
    vimtex + zathua, could you provide a .vimrc file ?

    vimtex+zathura (2019-04-18 22:52:08 EDT)

    I can not preview my pdf file with vim .

📮 Leave a comment (Spammers beware: All comments are moderated)

Sorry. There was an error submitting your comment. Please try again, or contact me if the problem persists.
Sending comment; please wait.
Thanks. Your comment was successfully submitted. It will appear here shortly if it isn't spam.