Friday, September 23, 2011

Synctex with GVim and Okular

What is synctex?

Synctex is the method of synchronization of your latex editor with the pdf/dvi output. Forward synchronization is where you jump from any line in your latex editor to the corresponding line in pdf/dvi output. Reverse synchronization is where you jump any line in your pdf/dvi output to the corresponding line in your tex editor. Synctex has been around since TeXLive 2008. Many latex editors and pdf/dvi viewer support synctex. Some of these are texworks texmaker, gedit with latex plugin. All of the above can be installed in ubuntu via synaptic. However, AFAIK the lucid version of gedit latex plugin doesn't support synctex. It's better to install the gedit plugin from it's website. GVim by default doesn't support synctex. I wrote couple of scripts, which, along with okular helps in bringing synctex support in GVim.
Even though I haven't tried implementing synctex in Emacs editor, for those of you who are interested can check this blog post out.


The following are the requirements:
  • Vim version 7 or higher.
  • Okular with synctex option compiled. Version 0.10.5 works.
  • latexmk: Version 4.25 or higher
  • Zenity
  • (g)awk
In ubuntu install the above softwares (excepting latexmk) via synaptic or apt-get
sudo apt-get install vim-gtk okular zenity gawk
The latexmk package found in Ubuntu lucid (10.04) is an older version. One needs at least version 4.25. Download latexmk from here. Unzip it and copy the file to your $PATH.
mkdir -p $HOME/bin/             ## Creating $HOME/bin/ directory.
chmod +x             ## To make the script executable.
cp $HOME/bin/latexmk ## Copying and changing the name
## of the script to latexmk.
Note that if you didn't have a $HOME/bin/ directory, then you have to logout and log back in.    

Installation of scripts

Download the scripts makelatex2, deljunk and makelatex.vim. After downloading the scripts rename them makelatex2, deljunk and tex_makelatex.vim respectively. Copy the scripts makelatex2, deljunk to $HOME/bin/. To make the scripts executable run the command
chmod +x makelatex2 deljunk 
cp makelatex2 $HOME/bin/
cp deljunk $HOME/bin/
Copy the file tex_makelatex.vim to $HOME/.vim/ftplugin/ directory. Create the directory if you don't have it. Notice the ``dot" in-front of ``vim". This makes it a hidden directory. Also copy the file editexisting.vim from the runtime/macros directory of vim to your local plugin directory. This is required for reverse search.

mkdir -p $HOME/.vim/ftplugin
cp tex_makelatex.vim $HOME/.vim/ftplugin
mkdir -p $HOME/.vim/plugin
cp /usr/share/vim/addons/plugin/editexisting.vim $HOME/.vim/plugin/
Create a hidden file ``$HOME/.vimrc" if you already don't have one and add the following lines.
" For filetype plugins
filetype plugin on
Note that the ``dot" infront of vimrc. The dot makes the file hidden.

Okular config

Okular > Settings > Configure Okular > 
    General > Program Features > Uncheck "Reload document on change"
    Editor > 
        Editor: Custom Text Editor
        Command: gvim +%l %f


Open or create a new tex file in gvim. Press ``Esc" and then ``Alt+1" to compile via pdflatex. Okular automatically opens the pdf file in the exact page where your cursor is in gvim.
For forward search (gvim to okular), put your cursor in any text part of your tex file and press ``Esc" and then ``Shift+f". Okular opens the file to the exact page of your text.
For reverse search (okular to gvim), ``Shift+click" on any part of your pdf file in okular. Gvim scrolls the file in the exact location.

Multi-document project

For a multi-document project, each sub-document files should have the following ``magic comment" in the first 5 lines of the tex file
%!TeX root = master.tex
where ``master.tex" is the main tex file on which compilation ought to be done.

Selecting type-setting command

Compilation by default is via pdflatex. However to compile via some other typesetting command (eg. latex+dvipdf), specify the typesetting command to be used via the following magic comment in the first 5 lines of the input tex file
%!TeX program = latex+dvipdf
This is compatible with texworks. Typesetting commands can be
Custom typesetting commands are also supported. Note that
  • Forward and reverse search doesn't work with ps file, i.e. with latex+dvips.
  • For multi-document project the typesetting comment ``%!TeX program" has to be specified only in the master tex file.

Optional (g)vim configurations

For other useful (g)vim configurations you can check out my last two posts on the topic here and here.

This file was generated via LATEX2html.

No comments:

Post a Comment