Vim Tips Wiki
Advertisement

When editing LaTeX files from Vim, you want to be able to compile latex, retrieve a list of errors, step through errors, etc. This tip explains the different options. They all work with the quickfix (:help quickfix) feature of Vim, so before setting up this tip, you should be familiar with the basics of quickfix in Vim.

Vim-Latex Plugin

The vim-latex plugin sets up make, makeprog, and errorformat for compiling single latex files on any system. See the vim-latex documentation for more details about compiling. The vim-latex compilation has several limitations on large multi-document projects which include figures, plots, etc which make building using the vim-latex provided features somewhat awkward.

Rubber plus Make

Rubber (sudo apt-get install rubber) is a wonderful tool for compiling latex files. It knows how many times to compile, knows to run bibtex, filters error messages and warnings (so you can ignore overfull hboxes until you want to see them), and so on. Rubber combined with make is a great fit for Vim's quickfix.

First, create a generic makefile for compiling latex using rubber and place it in some global location (I use ~/academic/tools/latex.mk)

.PHONY: clean

%.pdf: %.tex $(DEPENDS)
	rubber -f --pdf -s $<
	rubber-info --check $<

clean:
	rm -rf *.aux *.bbl *.blg *.log *.pdf *.toc *.snm *.out *.nav tags

Edit to your taste. In particular, you might consider updating the clean to not remove all pdfs (if you have figures in pdfs) perhaps using rubber --clean (see rubber's man page.) Other changes include ignoring certain warnings or passing other options to rubber (see the man page.) Also, you might add a tags target which runs ctags with the correct options (see tag-list documentation for ctags arguments.)

Now into your tex.vim ftplugin add the following lines

setlocal errorformat=%f:%l:\ %m,%f:%l-%\\d%\\+:\ %m
if filereadable('Makefile')
    setlocal makeprg=make
else
    exec "setlocal makeprg=make\\ -f\\ ~/academic/tools/latex.mk\\ " . substitute(bufname("%"),"tex$","pdf", "")
endif

If the current directory does not have a Makefile, it sets makeprog to compile using the generic makefile. This works great on single-file latex files where a makefile is overkill.

If you have a multi-project, complicated build project, you can create a custom makefile for the project. Mine normally start out like

DEPENDS=intro.tex somesection.tex somethingelse.tex appendix.tex refs.bib

.PHONY: all
all: mypaper.pdf

include ~/academic/tools/latex.mk

and from here you can add more targets, creating figures, etc. Anything make can do, you can insert it here.

Manual Setup

I strongly suggest you use one of the above options, but it is possible to set makeprog and errorformat directly and use the built in tex compiler.

let b:tex_flavor = 'pdflatex'
compiler tex
set makeprg=pdflatex\ \-file\-line\-error\ \-interaction=nonstopmode
set errorformat=%f:%l:\ %m

While it is possible to grep the output to filter the output from pdflatex, using rubber is a much better option since it knows all about filtering and compiling more than once.

Advertisement