Vim+LaTeX on Linux

From NLPWiki

Jump to: navigation, search

One advantage of WYSIWYG editors is the instantaneous feedback as you make make errors and the ability to edit the errors exactly when and where you spot them. Although there are some WYSIWYG options for LaTeX, they're not for everybody. This means that most of us have learned to deal with the fact that in order to make changes, we have to read the DVI/PDF in a dedicated viewer, switch to our favorite editor, then search/scroll to the appropriate spot to make the changes. This last part can be particularly difficult in SVN-friendly TeX markup (text with many extra line breaks).

The next best thing to WYSIWYG editors is forward search and inverse search. Forward search allows you to "jump" from a line in of tex in your editor to a position on the page of a DVI or PDF. Inverse search allows you to jump from a position in the pdf to the corresponding line in the source. This is extremely useful, especially in the reviewing/editing phase.

I've been using VIM as my editor and Okular as my viewer, but most editors (Emacs included) and many viewers on all 3 major OSes support these features. Setting things up on my Mac with MacVim and Skim went fairly smoothly and worked well. But Linux (GVim + Okular) was a different story. Here's what I did to get things working.

Contents

Forward Search from (G)Vim to Okular Using the Vim-LaTeX Suite

Install Vim-Latex Suite

I use vim-latex suite to assist me in my vim-tex-ing. Since I'm on Ubuntu, I used the packaged version, but it's also possible to download and install as per the instructions on the website. If you're going to use the Ubuntu package, you'll need to do the following steps:

sudo aptitude install vim-latexsuite vim-addon-manager
vim-addons install latex-suite

That last line is very important (and also elusive). You won't have the same issues if you install things as suggested on the vim-latex suite website.

Next, update your ~/.vimrc file as described in the first part of this page. If you want to do the last part (edit the tex.vim file) and you've used Ubuntu's packed version of vim-latex, note that the file is found in /usr/share/vim/addons/ftplugin/tex.vim. You will probably have to create the file. In this latter file you will definitely want to add the following line:

TexLet g:Tex_UseEditorSettingInDVIViewer = 1

If you're like me, you don't like xpdf, xdvi, or ghostview, yet those are the default viewers in Vim-Latex. If you are okay with those you're done. Otherwise, you'll have to change them. I directly modified /usr/share/vim/addons/ftplugin/latex-suite/texrc, changing these lines:

        TexLet g:Tex_ViewRule_ps = 'ghostview'
        TexLet g:Tex_ViewRule_pdf = 'xpdf'
        TexLet g:Tex_ViewRule_dvi = 'xdvi'

to

        TexLet g:Tex_ViewRule_ps = 'okular'
        TexLet g:Tex_ViewRule_pdf = 'okular'
        TexLet g:Tex_ViewRule_dvi = 'okular'

Although I haven't tried, I think you could just add this line to your personal ~/.vimrc file:

let g:Tex_ViewRule_ps = 'okular'
let g:Tex_ViewRule_pdf = 'okular'
let g:Tex_ViewRule_dvi = 'okular'

Now we need to setup Vim-LaTeX to be able to forward search to okular (it only knows about xdvi).

DVIs

Find the lines:

                elseif Tex_GetVarValue('Tex_UseEditorSettingInDVIViewer') == 1 && viewer == "kdvi"
                        let execString = 'silent! !kdvi --unique file:'.mainfnameRoot.'.dvi\#src:'.line('.').expand("%").' &'

and paste right after:

                elseif Tex_GetVarValue('Tex_UseEditorSettingInDVIViewer') == 1 && viewer == "okular"
                        let execString = 'silent! !okular --unique '.mainfnameRoot.'.dvi\#src:'.line('.').expand("%").' &'

If you prefer working with DVIs, replace the hard-coded ".pdf" to ".dvi".

NB the way things are setup totally ignores the "target" formats of Vim-LaTeX.

Make sure your file is compiled with the --src-specials option. If you happen to compile using Vim-LaTeX, you can find this in the texrc file mentioned above (see this page)

PDFs

Find the lines:

                elseif Tex_GetVarValue('Tex_UseEditorSettingInDVIViewer') == 1 && viewer == "kdvi"
                        let execString = 'silent! !kdvi --unique file:'.mainfnameRoot.'.dvi\#src:'.line('.').expand("%").' &'

and paste right after:

                elseif Tex_GetVarValue('Tex_UseEditorSettingInDVIViewer') == 1 && viewer == "okular"
                        let execString = 'silent! !okular --unique '.mainfnameRoot.'.pdf\#src:'.line('.').expand("%").' &'

NB the way things are setup totally ignores the "target" formats of Vim-LaTeX.

If you are going to work with PDFs, you'll need to either use pdfsync (deprecated) or synctex (requires TeX Live 2008 or higher). In the case of the former, you will need the texlive-tex-extra package. Then be sure to include \usepackage{pdfsync} in the preamble of your document. In the case of the latter, you will want to add \synctex=1 to the preamble OR update your build process to use --synctex=1 with latex/pdftex (to see how to change this in Vim-Latex see this page, found in the texrc file mentioned above).

If you choose synctex, you'll need KDE 4.4 or higher (add ppa:kubuntu-ppa/backports as a source in Karmic) AND Tex Live 2009 (see this post about adding the ppa). OR upgrade to Ubuntu 10.04 Lynx.

Inverse Search to GVim

If your editor supports inverse search, and many do, you can make it target GVim by setting the command to ran as:

gvim -c ":RemoteOpen +%l %f"

(you're editor might use different ways of variable substitution for %l -- line and %f -- filename).

In Okular, you can set this in the "Editor" page of preferences, by choosing the custom editor.

Personal tools