Compiling LaTeX from Vim

From Vim Tips Wiki

Jump to: navigation, search
Tip 1678 Printable Monobook Previous Next

created November 28, 2011 · complexity basic · author Wuzzeb · version 7.0


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 feature of Vim (:help quickfix), so before using this tip you need to be familiar with the basics of quickfix operation.

[edit] 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 where there are more steps to building than just running pdflatex. This makes building using the vim-latex provided features somewhat awkward.

If you want to automatically compile and refresh xdvi whenever you write the current buffer, you can add the following code into your tex.vim ftplugin.

au BufWritePost *.tex silent call Tex_RunLaTeX()
au BufWritePost *.tex silent !pkill -USR1 xdvi.bin

Note that Vim-LaTeX should be downloaded from its web page. The version at script#475 is outdated and will not be updated.

[edit] Automatic LaTeX plugin

The AutomaticLaTeX Plugin has a build-in solution for compiling LaTeX files when they got changed (using CursorHold and CursorHoldI autcommand groups). Compiles documents in the background, and shows a progress bar in the vim status line. It also rewrites the log file into a vim readable form and has options to filter errors, warnings and info messages in the QuickFix window. It also has :MakeLatex command which builds the document and runs TeX's friends when necessary. Additionally, it includes script#3109 to provide interface to Latexmk.

This plugin works under Linux and MacOs and for best performance requires a recent version of Vim (7.3.468) with Python interface. The plugin has a very rich completion, which makes writing LaTeX files much faster and easier. For a long list of features see the web page [1], online documentation there are also some videos which advertise some nice features.

[edit] Other plugins

Automatic LaTeX Plugin and Vim-LaTeX are fully flagged plugins, if you prefer a smaller solutions there are also these plugins available:

There are also other LaTeX plugins which do not provide interface for compilation"

[edit] 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 in 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-file, 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.

[edit] Manual setup

I strongly suggest you use one of the above options, but it is possible to set makeprg 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.

[edit] Comments

In the #Manual setup section, what's wrong with the default makeprg and errorformat set up by the "compiler tex" command? This section should probably go first to show what Vim offers out-of-the-box, then explain what is wrong with it, THEN present the alternatives. --Fritzophrenic 21:02, June 15, 2012 (UTC)

Personal tools