The ultimate Latex Makefile
Friday, March 24th, 2006After a long time of trial and errors (a.k.a. procrastination), I finally finished my “make phd” script for my thesis. The script uses bibtex (rerunning Latex as many times as needed) and can use either Latex or PDFLatex (if you want to use hyperref and nice PDF stuff) and if necessary converts all your images to PDFs.
You can use it like this:
make all <- build the document using latex, dvips, epstopdf
make clean <- clean up (remove all intermediate (and final), including converted eps images (in using pdflatex)
PDF=true make all <- builds the document using PDFLatex
Here’s the main makefile:
Makefile for Tadek's thesis
This file builds a paper - PDF file and if (PDF is defined) a PDF with hyperlinks
#
Written (c)2005 by Tadeusz Pietraszek (pie@zurich.ibm.com)
$Id: Makefile 597 2006-01-12 15:43:59Z pie $
#
Egrep trick from: http://xpt.sourceforge.net/techdocs/Latex/
MakefileForTeX/Latex08.000.html
TEXFILES=thesis.tex
TEXFILES=$(wildcard *.tex)
TARGETS=$(patsubst %.tex,%.pdf,$(TEXFILES))
RERUN = "(There were undefined references|Rerun to get (cross-references|the bars) right)" RERUNBIB = "No file..bbl|Citation.undefined"
phd: all
all: all-recursive $(TARGETS)
clean: clean-recursive rm -f *.aux *.log *.bbl *.blg *.brf *.cb *.ind *.idx *.ilg \ *.inx *.ps *.dvi *.pdf *.toc *.out *.lot *~ *.backup
all-recursive: for dir in $(wildcard *); do if [ -d $$dir ] && [ -f $$dir/Makefile ]; then cd $$dir; $(MAKE) all; cd ..; fi; done
clean-recursive: for dir in $(wildcard *); do if [ -d $$dir ] && [ -f $$dir/Makefile ]; then cd $$dir; $(MAKE) clean; cd ..; fi; done
ifndef PDF
building the document using .dvi, .ps, -> .pdf
%.dvi: %.tex echo $(SHFILES) latex $< egrep -c $(RERUNBIB) $.log && (bibtex $;latex $<); true egrep $(RERUN) $.log && (latex $<) ; true egrep $(RERUN) $.log && (latex $<) ; true
%.ps: %.dvi dvips -Ppdf -ta4 -G0 $<
%.pdf: %.ps ps2pdf $<
else
this is making the same file using pdfelatex
%.pdf: %.tex pdfelatex $< egrep -c $(RERUNBIB) $.log && (bibtex $;pdfelatex $<); true egrep $(RERUN) $.log && (pdfelatex $<) ; true egrep $(RERUN) $.log && (pdfelatex $<) ; true
endif
My pictures are built using the following Makefile (in a subdirectory of the main paper):
Makefile for Tadek's thesis - pictures!
#
Supports two targets: all and clean - and calls other makefiles recursively
If PDF is defined, converts EPS into PDFs
#
$Id:$
#
If you want to add your custom grpahic file .foo you need to
#
FOOFILES=#(wildcard *.foo)
EPSFILES=... $(patsubst %.foo,%.eps,$(FOOFILES))
#
%.eps: %.foo:
foo2eps -o $*.eps $<
source files
DIAFILES=$(wildcard *.dia) INKSCAPEFILES=$(wildcard *.svg)
target files (only those that we are building, and will be deleted)
EPSFILES=$(patsubst %.dia,%.eps,$(DIAFILES)) $(patsubst %.svg,%.eps,$(INKSCAPEFILES))
ifdef PDF
target files (if we're building PDFs)
PDFFILES=$(patsubst %.eps,%.pdf,$(EPSFILES) $(wildcard *.eps)) else PDFFILES= endif