Using latexmk and Make for Building LaTeX Documents

Writing proper Makefiles for building LaTeX documents is a pain, since the steps for creating the final documents depend on the LaTeX features that are used, e.g., BibTeX, makeindex, etc.

John Collins has written an excellent tool named "latexmk" for processing LaTeX documents. It can be obtained from the latexmk webpage. Using "latexmk" simplifies writing a Makefiles for building LaTeX documents drastically, a template Makefile looks like this:

LATEX=pdflatex
LATEXMK=latexmk
LATEXOPT=-file-line-error


MAIN=plessl2007a
SOURCES=$(MAIN).tex Makefile bib/plessl.bib
FIGURES := $(shell ls fig/*.pdf)

all:    $(MAIN).pdf

.refresh:
        touch .refresh

$(MAIN).pdf: $(MAIN).tex .refresh $(SOURCES) $(FIGURES)
        $(LATEXMK) -pdf $(MAIN).tex

force:
        touch .refresh
        $(MAKE) $(MAIN).pdf

.PHONY: clean force all

clean:
        $(LATEXMK) -C $(MAIN).tex
        rm -f $(MAIN).pdfsync
        rm -rf *~ *.tmp

You can use this Makefile for building LaTeX papers that use the file layout proposed in my earlier article File and Directory Layout for Storing a Scientific Paper in Subversion. The only change needed is to adapt the MAIN variable to match the identifier of the paper. The Makefile provides the following targets:

make
make clean
make force

make builds the PDF file for the paper. make clean removes all temporary and intermediate files. make force forces a rebuild, even if make thinks that you don't need to rebuild the paper.

The Makefile assumes that you are using a PDF tool-flow, i.e., you are processing your LaTeX files with pdflatex. This tool-flow implies, that your figures are also stored in PDF format. From my experience, I can wholeheartedly recommend the pdflatex tool-flow, but if you prefer the traditional latex -> dvips -> ps toolflow, the Makefile can be adapted easily.

Written January 11th, 2007