Will Robertson
Will Robertson

Reputation: 64520

Makefile for multi-file LaTeX document

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!)

Upvotes: 8

Views: 5284

Answers (3)

kch
kch

Reputation: 698

Please consider using latexmk

http://www.phys.psu.edu/~collins/software/latexmk-jcc/

It sorts out whether running of pdflatex/latex, bibtex, makeindex etc. is needed (and how many times) to completely compile your source.

latexmk is a perl script that is included in most latex distributions. You only need to make sure that perl is installed on your system.

Upvotes: 2

Baruch Even
Baruch Even

Reputation: 2636

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.

Upvotes: 2

David Z
David Z

Reputation: 131550

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)}"

Upvotes: 8

Related Questions