Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile for multi-file LaTeX document

Tags:

makefile

latex

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

like image 299
Will Robertson Avatar asked Mar 12 '09 02:03

Will Robertson


People also ask

How do I compile multiple files in LaTeX?

One option is the standalone package. In your main document, \usepackage{standalone} . In your subfile, \documentclass{standalone} and use your regular preamble. When you input/include a subfile, the standalone package will ignore everything until \begin{document} and then compile the rest of the file as normal.

How do I include a LaTeX file in another LaTeX file?

The standard tools to insert a LaTeX file into another are \input and \include . Use this command in the document body to insert the contents of another file named filename. tex ; this file should not contain any LaTeX preamble code (i.e. no \documentclass , \begin{document} or \end{document} ).

How do I import TEX files into overleaf?

Yes: Click on the Overleaf menu icon above the file list, and set the "Main document" option to the . tex file you need. This will be the file that is compiled and previewed each time you return to your project.

How do I read a LaTeX file?

LaTeX documents can be viewed and edited in any text editor, since they're just plain text files. Notepad in Windows, Notepad++, and Vim and are some examples of text editor programs.


1 Answers

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)}"
like image 118
David Z Avatar answered Oct 23 '22 23:10

David Z