A Makefile for LaTeX documents

This is a Makefile that I’ve perfected over the years for rendering typical LaTeX articles or presentations (e.g. for scientific papers). It handles almost all LaTeX documents, except for large documents that use the \include feature (this is for another tip). It assumes your document has a bibtex bibliography.

The Makefile works out of the box, you only have to configure the jobname (=name of the main .tex-file). Most common prerequisites are automatically detected:

LATEXFLAGS?=-interaction=nonstopmode -file-line-error
PDFLATEX=pdflatex
BIBTEX=bibtex
 
JOB=paper
TEXS=$(wildcard *.tex) $(wildcard *.sty) $(wildcard *.cls)
PICS=$(wildcard *.png) $(filter-out $(JOB).pdf,$(wildcard *.pdf)) $(wildcard *.jpg)
BIBS=$(wildcard *.bib) $(wildcard *.bst)
 
.DEFAULT: all
.PHONY: all clean
 
all: $(JOB).pdf
 
$(JOB).aux: | $(TEXS) $(PICS)
        $(PDFLATEX) $(LATEXFLAGS) $(JOB)
 
$(JOB).bbl: $(JOB).aux $(BIBS)
        $(BIBTEX) $(JOB)
 
$(JOB).pdf: $(TEXS) $(PICS) $(JOB).aux $(JOB).bbl
        @cp -p $(JOB).aux $(JOB).aux.bak
        $(PDFLATEX) $(LATEXFLAGS) $(JOB)
        @if cmp -s $(JOB).aux $(JOB).aux.bak; \
        then touch -r $(JOB).aux.bak $(JOB).aux; \
        else NEWS="$$NEWS -W $(JOB).aux"; fi; rm $(JOB).aux.bak; \
        if [ -n "$$NEWS" ]; then $(MAKE) $$NEWS $@; fi
 
clean:
        rm -f $(JOB).aux $(JOB).log $(JOB).blg $(JOB).bbl $(JOB).out $(JOB).pdf

LaTeX documents have a circular dependency: a latex run produces a .aux-file, which is read as the input for a following run. You have to keep running latex until this process reaches a fixpoint. This is handled as follows:

  1. An order-only prerequisite is used to produce a .aux-file if none exists.
  2. A backup copy of the .aux-file is preserved.
  3. latex is ran to produce a new .aux-file.
  4. If the .aux-file differs from the backup copy, make is restarted to go back to step #1.

The commands take special care to ensure that make considers the .aux-file as new if (and only if) it has changed.

One thought on “A Makefile for LaTeX documents

  1. Philipp

    Hi,
    thanks for the tip! But you got me interested in how the Makefile would look with the \include feature. Please share ;-)
    best regards.
    Philipp

    Reply

Leave a Reply to Philipp Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>