LaTeX Makefile

Example Makefile:

MAIN=$(sh grep -l '\documentclass' *.tex)
TEXFILES=$(filter-out $(MAIN), $(sh ls *.tex))
PDFLATEX=pdflatex
PDF=$(MAIN:.tex=.pdf)

DEFAULT: pdf

pdf : $(PDF)

$(PDF) : $(MAIN) $(TEXFILES)
    $(PDFLATEX) $(MAIN)
    $(PDFLATEX) $(MAIN)
  • The variable MAIN is automatically populated with grep searching for a file that contains the string \documentclass (hopefully finds the correct one...)
  • TEXFILES are all other files with the file extension .tex.
  • PDF will become the name of the generated PDF. It is taken from the MAIN file. You can change this according to your preference.
  • PDFLATEX is the program name to generate PDF from LaTeX input.
  • The first rule is the default when make is invoked without parameters. To avoid confusion and accidental overwriting, a striking DEFAULT-target is used that just tells make to build the PDF.
  • The rule for make pdf is propably only useful, if also rules for Postscript and DVI are implemented. For beginners: stick to using pdflatex to directly generate PDF files. The old way with DVI is obsolete (unless your printing service can only handle Postscript).
  • The PDF is generated from all .tex files. To get the references right, pdflatex is called twice.
  • If you're using an index or BibLaTeX/BibTeX, then append their invokation and add another two calls to pdflatex.
  • If the generation takes too long, add a quick target, that unconditionally calls pdflatex once. After fixing typos or only minor modifications, this often suffices.

    ::make quick : $(PDFLATEX) $(MAIN)

social