Penguin

In the process of writing 420 final reports, SamJansen and Tom Young worked to create some MakeFiles useful for LaTeX projects. LaTeX and Bibtex have some strange properties which makes writing MakeFiles for them an interesting challenge. This page is currently a work in progress.

A Basic Makefile

(remember that the indented lines need tabs, not spaces)

A very basic makefile that does not support Bibtex, images, or multiple input files looks something like the following:

# vim: tabstop=8 shiftwidth=8 noexpandtab
# A very basic Latex makefile by Sam Jansen.
# See http://www.wlug.org.nz/LatexMakefiles for more information
# Change the following to whatever you like. Target will have '.ps' or '.pdf'
# appended as need be. Tex should be the tex file WITHOUT the '.tex' suffix.
TEX=report
TARGET=report

# You probably don't need to change anything below the following line:
.PHONY: clean debug gvshow pdfshow acroshow dvishow
LATEX_ARGS=--interaction batchmode
LATEX=latex

$(TARGET).ps: $(TARGET).dvi
        dvips $(TARGET) -o $(TARGET).ps

$(TARGET).pdf: $(TEX).tex
        pdflatex $(LATEX_ARGS) $(TEX)

$(TARGET).dvi: $(TEX).tex
        $(LATEX) $(LATEX_ARGS) $(TEX)
        @mv $(TEX).dvi $(TARGET).dvi

clean:
        rm *.toc *.aux *.pdf *.ps *.eps *.log *.lof *.bib *.bbl *.blg *.dvi

debug:
        latex $(TEX)

gvshow: $(TARGET).ps
        gv $(TARGET).ps

pdfshow: $(TARGET).pdf
        xpdf $(TARGET).pdf

acroshow: $(TARGET).pdf
        acroread $(TARGET).pdf

dvishow: $(TARGET).dvi
        xdvi $(TARGET).dvi

Probably the most interesting thing about the above makefile is that it uses --interaction batchmode as an argument to Latex. The rest is a fairly standard makefile. Things get a little more complex when Bibtex and automatic image conversion is added to the makefiles.

Note that this is a really dangerous Makefile to use as is: this clean stanza will delete ALL pdf, eps, and ps files in the current directory. Be sure you have backups elsewhere of all your letters-of-support.pdf and all the figures.eps for your proposal, because when you run make clean with this makefile they are also going to be rm'ed. There is also a .bib extension being deleted! .bib usually contains your raw bibliography database, so it is input, just like your .tex file. Delete it and you can't ever generate your document again without a lot of pain. The clean stanza should have something more like

rm $(TARGET).{toc,aux,pdf,ps,eps,log,lof,bbl,blg,dvi}

to restrict it to relevant and output files only ( -- ChrisRae?).


A Simple Latex and Bibtex Makefile

Serious people use bibtex to manage their references. Here is a makefile that should run bibtex and latex the appropriate number of times.

(I've removed the extra targets and steps I had in here to run musixtex as well... -- JohnMcPherson)

TARGET=proposal

# make pdf by default
all: ${TARGET}.pdf

# it doesn't really need the .dvi, but this way all the refs are right
%.pdf : %.dvi
        pdflatex $*

${TARGET}.bbl: ../bib/music.bib
# in case we don't already have a .aux file listing citations
# this should probably be a separate makefile target/dependency instead
# of doing it every time... but *shrug*
        latex ${TARGET}.tex
# get the citations out of the bibliography
        bibtex ${TARGET}
# do it again in case there are out-of-order cross-references
        @latex ${TARGET}.tex

${TARGET}.dvi: ${TARGET}.bbl ${TARGET}.tex
        @latex ${TARGET}.tex

# shortcut, so we can say "make ps"
ps: ${TARGET}.ps

${TARGET}.ps: ${TARGET}.dvi
        @dvips -t a4 ${TARGET}.dvi

clean:
        rm -f ${TARGET}.{log,aux,ps,dvi,bbl,blg,log}

reallyclean: clean
        rm -f ${TARGET}.{ps,pdf}


PHONY : ps all clean reallyclean

Useful Variables

LaTeX has some EnvironmentVariables that control where it looks for various files. Note that just setting these in the Makefile isn't enough; you need to set them for the actual call to latex/pdflatex/bibtex etc. These allow you to run latex from different directories but still have relative paths work, which is useful if you have a very large document split up into smaller files and use the "\input" latex tag.

TEXINPUTS

A colon-separated list of paths of where to look for files named in \input and \usepackage (etc) tags. End with ":" so that it also checks the default system directories for class/style files. Eg

TEXINPUTS=".:..:/mylatexfiles:" latex foo

BIBINPUTS

":"-separated path to look for bibliography files

TEXFORMATS

":"-separated path to look for .fmt files - for example if you have pdflatex installed in a non-standard place, you can set this to tell it where to find the pdflatex.fmt file.

TEXMF

The base directory for the latex installation.


More Powerful Makefiles

Because of all the crap that Latex and Bibtex outputs, SamJansen decided to create a directory structure for the report that kept all the "work" files away from the main directory. There are a few additions by MattBrown below to allow images to be generated from .fig files and to spellcheck the report before generating it.

  • images/ contains all images and figures used in the report. All such images are converted to appropriate formats by the makefile in this directory.
  • work/ is used when Latex and Bibtex is run. Therefore all the extraneous files like .toc and .aux and so on are put in here.
  • The root directory holds all .tex files and the main makefile.

Usage

Place .tex file in the root directory, a .bib file in the root directory, and .eps, .png, .jpg, .fig and .dia images/figures in the images/ directory. There are various targets used, just typing 'make' will just build a pdf file. The only editing of files needed is the top of the root makefile.

Here is what the current version looks like:

Makefile in the root directory

# vim: ts=8 noexpandtab
# $Id: Makefile,v 1.11 2003/10/11 04:12:43 stj2 Exp $

# Edit the below:
TEXFILES=$(wildcard *.tex)
BIBFILE=report.bib
TARGET=report

# The following should not need editing:
export TEXFILES BIBFILE TARGET

$(TARGET).pdf: spellcheck $(TEXFILES) $(BIBFILE)
        $(MAKE) -C images pdf
        $(MAKE) -C work pdf

$(TARGET).ps: spellcheck $(TEXFILES) $(BIBFILE)
        $(MAKE) -C images ps
        $(MAKE) -C work ps

$(TARGET).dvi:
        $(MAKE) $(TARGET).ps

spellcheck:
        ispell -t $(TEXFILES)

.PHONY: show showx clean wordcount images debug spellcheck

debug:
        $(MAKE) -C images
        $(MAKE) -C work debug

pdfdebug:
        $(MAKE) -C images pdf
        $(MAKE) -C work pdfdebug

gvshow: $(TARGET).ps
        gv $(TARGET).ps

pdfshow: $(TARGET).pdf
        xpdf $(TARGET).pdf

acroshow: $(TARGET).pdf
        acroread $(TARGET).pdf

dvishow: $(TARGET).dvi
        xdvi work/$(TARGET).dvi

clean:
        @rm $(TARGET).pdf 2>/dev/null; true
        @rm $(TARGET).ps 2>/dev/null; true
        $(MAKE) -C work clean
        $(MAKE) -C images clean

wordcount:
        @echo Approximate word count: `grep -v '^\\\\' $(TEXFILES)|grep -v '^%'|wc -w`

Makefile in the work/ directory

# vim: noexpandtab ts=8
# $Id: Makefile,v 1.10 2003/10/11 12:09:37 stj2 Exp $
.PHONY: pdf ps clean

TEXS=$(foreach i,$(TEXFILES),$(addprefix ../,$i))
BIB=$(addprefix ../,$(BIBFILE))

$(TARGET).aux: $(TEXS)
        @cp -l $(TEXS) . 2>/dev/null; true
        @cp -l ../images/*.eps . 2>/dev/null; true
        latex --interaction batchmode $(TARGET)

$(TARGET).bbl: $(BIB) $(TARGET).aux
        @cp -l $(BIB) . 2>/dev/null; true
        @cp -l ../images/*.eps . 2>/dev/null; true
        bibtex -terse $(TARGET)
        latex --interaction batchmode $(TARGET)

$(TARGET).dvi: $(TEXS) $(TARGET).bbl
        @cp -l $(TEXS) . 2>/dev/null; true
        @cp -l ../images/*.eps . 2>/dev/null; true
        latex --interaction batchmode $(TARGET)

pdf: $(TARGET).dvi
        @cp -l ../images/*.pdf . 2>/dev/null; true
        pdflatex --interaction batchmode $(TARGET)
        @cp $(TARGET).pdf ..

ps: $(TARGET).dvi
        latex --interaction batchmode $(TARGET)
        dvips -q $(TARGET) -o $(TARGET).ps
        @cp $(TARGET).ps ..

debug:
        @cp -l $(TEXS) . 2>/dev/null; true
        @cp -l $(BIB) . 2>/dev/null; true
        @cp -l ../images/*.{pdf,eps} . 2>/dev/null; true
        latex $(TARGET)

pdfdebug:
        @cp -l $(TEXS) . 2>/dev/null; true
        @cp -l $(BIB) . 2>/dev/null; true
        @cp -l ../images/*.{pdf,eps} . 2>/dev/null; true
        pdflatex $(TARGET)

clean:
        rm -f *.toc *.aux *.pdf *.ps *.eps *.log *.tex *.lof *.bib *.bbl *.blg *.dvi

Makefile in the images/ directory

# vim: noexpandtab ts=8
# $Id: Makefile,v 1.6 2003/10/02 21:21:20 stj2 Exp $

.PHONY: pdf ps clean

PNGS=$(wildcard *.png)
DIAS=$(wildcard *.dia)
FIGS=$(wildcard *.fig)
EPSS=$(wildcard *.eps)

OUTPUT_EPS=$(PNGS:png=eps) $(DIAS:dia=eps) $(FIGS:fig=eps) $(EPSS)
OUTPUT_PDF=$(OUTPUT_EPS:eps=pdf)

pdf: $(OUTPUT_PDF)

ps: $(OUTPUT_EPS)

clean:
        rm $(OUTPUT_PDF) $(DIAS:dia=eps) $(PNGS:png=eps) 2>/dev/null; true
        rm $(FIGS:fig=eps) 2>/dev/null; true

# Bitmap images -> EPS: PNG and JPG are covered at the moment, using
# ImageMagick's 'convert' utility
%.eps: %.png
        convert $< $@

%.eps: %.jpg
        convert $< $@

# EPS -> PDF
# epstopdf is much better than ps2pdf... it preserves bounding box
%.pdf: %.eps
        epstopdf $<

# DIA -> EPS
%.eps: %.dia
        dia --nosplash -e $@ $<

# FIG -> EPS
%.eps: %.fig
        fig2dev -L eps $< $@