views:

659

answers:

3

I'm trying to simplify/improve the Makefile for compiling my thesis. The Makefile works nicely for compiling the whole thing; I've got something like this:

show: thesis.pdf
    open thesis.pdf

thesis.pdf: *.tex
    pdflatex --shell-escape thesis

This allows me to type make and any changes are detected (if any) and it's recompiled before being displayed.

Now I'd like to extend it to conditionally compile only individual chapters. For example, this allows me to write make xpmt to get just a single chapter in a round-about sort of way:

xpmt: ch-xpmt.pdf
    open ch-xpmt.pdf

ch-xpmt.pdf: xpmt.tex
    pdflatex --shell-escape --jobname=ch-xpmt \
      "\includeonly{xpmt}\input{thesis}"

But I don't want to have to write this down identically for each individual chapter. How can I write the rules above in a general enough way to avoid repetition?

(More of an exercise in learning how to write Makefiles rather than to solve any real problem; obviously in this case it would actually be trivial to copy and paste the above code enough times!)

+4  A: 

If you have chapters named xpmt (guessing that's "experiment"?) and, say, thry, anls, conc, or whatever:

xmpt thry anls conc: %: ch-%.pdf
    open $<

ch-%.pdf: %.tex
    pdflatex --shell-escape --jobname=ch-$* "\includeonly{$*}\input{thesis}"

Or to do it the "proper" way with make variables, I think it'd be something like this:

chapters = xmpt thry anls conc
main = thesis
.PHONY: $(chapters) show

show: $(main).pdf
    open $<

$(main).pdf: $(main).tex $(addsuffix .tex,$(chapters))
    pdflatex --shell-escape $(main)

$(chapters): %: ch-%.pdf
    open $<

ch-%.pdf: %.tex
    pdflatex --shell-escape --jobname=ch-$* "\includeonly{$*}\input{$(main)}"
David Zaslavsky
Thanks! Couple of tricks in there I'll need to look up...
Will Robertson
"info make", that tells me everything I ever need to know about make ;-)
David Zaslavsky
+2  A: 

You should consider something like rubber to handle the LaTeX building for you. While it is possible to use make to do most of the work a specialized tool can handle the intricacies of LaTeX such as rerunning bibtex a number of times to get all references sorted and things like that.

Baruch Even
+1  A: 

Also, check out the ultimate latex makefile.

Eugene Morozov