Skip to Main Content

Makefile for LaTeX

When writing a large paper or book that has several .tex files and numerous images, it gets difficult to keep track of everything. I wrote a Makefile to help make life easier. Now when I make a revision (either text or an image), all I type is `make` and my new images are re-configured, and both LaTeX and pdfLaTeX are executed and even re-executed if necessary (to get the numbering/references right). Below I describe how the Makefile works.

Using LaTeX in xfig

When drawing in xfig, one often wants to put in LaTeX symbols. All you do is put in the text as LaTeX, then edit the text properties and set the "special flag" option to "special". Then you execute `fig2dev` twice: once to convert the .fig file to .esp and once to generate the symbols used in your LaTeX, within the image (see details below).

Exporting .fig to .eps

The fig2dev command takes care of this.

fig2dev -L pstex foo.fig > foo.eps

Exporting .eps to .pdf

The epstopdf command takes care of this.

epstopdf foo.eps > foo.pdf

Exporting .fig to .tex

The LaTeX in your .fig file will not show up in the image. Try:

fig2dev -L pstex_t -p foo foo.fig > foo.tex

This will create the file foo.tex that calls the foo image file and places the LaTeX over the image. It looks something like this


followed by the LaTeX needed to place the layer over the image. Then in your .tex source file you need only call the foo.tex file.

\caption{Put the caption here.}

Interestingly LaTeX and pdfLaTeX are smart enough to add the .eps and .pdf extensions to foo in the \includegraphics function call. This is a really nice feature--otherwise we would need to change the extenstions between .eps and .pdf when calling LaTeX and pdfLaTeX, respectively.

Putting it all in a Makefile

A Makefile is an indispensable tool in software development. It allows for the compilation of files that have been revised without having to recompile all the source. Specifically it allows for the comparison of timestamps of the individual source files with that of the executable. The files that are newer than the executable can then be recompiled and the older ones left alone. For our purposes, we want to check any to see if any of our .tex sources or .fig images have been changed. If any .tex source files have been changed, we need to run LaTeX and/or pdfLaTeX. If any .fig files have been changed, we want to re-export to .esp, .pdf, and the .tex files in the ./figures directory, and likewise run LaTeX and/or pdfLaTeX.

Another caveat in the process is that both equation and citation changes in LaTeX require that it be re-run. We capture the output and rerun it if the phrase "Rerun to get cross-references" is found in the output.

Below we supply the entire makefile.

# LaTeX Makefile for dvi, ps, and pdf file creation.
# By Jeffrey Humpherys
# Written April 05, 2004
# Revised January 13, 2005
# Thanks Bjorn and Boris
# Usage:
# make          # make dvi, ps, and pdf
# make dvi      # make dvi
# make ps       # make ps (and dvi)
# make pdf      # make pdf

MAIN		= book
SOURCES	= $(wildcard ./*.tex)
EPSFIGURES	= $(patsubst %.fig,%.eps,$(wildcard ./figures/*.fig))
PDFFIGURES	= $(patsubst %.fig,%.pdf,$(wildcard ./figures/*.fig))

all: dvi ps pdf

dvi: ${MAIN}.dvi
pdf: ${MAIN}.pdf
ps: ${MAIN}.ps

        latex ${MAIN}
        @while ( grep "Rerun to get cross-references"	\
                        ${MAIN}.log > /dev/null ); do		\
                echo '** Re-running LaTeX **';		\
                latex ${MAIN};				\

        pdflatex ${MAIN}
        @while ( grep "Rerun to get cross-references" 	\
                        ${MAIN}.log > /dev/null ); do		\
                echo '** Re-running LaTeX **';		\
                pdflatex ${MAIN};				\

${MAIN}.ps : ${MAIN}.dvi
        # running dvips
        dvips ${MAIN}.dvi -o ${MAIN}.ps
        rm -f ./figures/*.tex
        rm -f ./figures/*.eps
        rm -f ./figures/*.pdf
        rm -f ./figures/*.bak
        rm -f ./*.aux
        rm -f ./*.tex~
# (re)Make .eps is .fig if newer
%.eps : %.fig
        #Creates .eps file
        fig2dev -L pstex $*.fig > $*.eps
        #Creates .tex file
        fig2dev -L pstex_t -p $* $*.fig > $*.tex
# (re)Make .pdf if .esp is newer
%.pdf : %.eps
        #Creates .pdf files from .esp files
        epstopdf $*.eps > $*.pdf