Vim Tips Wiki
(Insert TipProposed template + minor manual clean)
Line 64: Line 64:
   
 
==Manual setup==
 
==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.
+
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.
 
<pre>
 
<pre>
 
let b:tex_flavor = 'pdflatex'
 
let b:tex_flavor = 'pdflatex'

Revision as of 13:57, 30 May 2012

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
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.

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

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.

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.

Comments