2008-07-15

Auto-building LaTeX documents with SCons

How many commands does it take to compile LaTeX document?
1. pdflatex mypaper.tex
2. bibtex mypaper
3. pdflatex mypaper.tex
4. pdflatex mypaper.tex
— and how many times do you have to type them when you edit an article? A lot.

Fortunately, there is a simple cure. A SCons build system. Install it and create a new SConstruct file where the LaTeX document lives, with a contents like this:

PDF(target = 'mypaper.pdf', source = 'mypaper.tex')
Then just run
$ scons
And SCons will automatically run bibtex if you use it, it will run PDFLaTeX as many times as it needs (SCons is smart enough to read the log file of the LaTeX), SCons will remember MD5 sum of the source file and will avoid re-compiling the PDF if the source file did not change. Very convenient!

I also prefer to re-build the PDF whenever I change any of the figures. Given that my figures are in imgs/ subfolder and all are either *.pdf or *.png files, I can create a list of “source” files using SCons' Glob function. This is what SContstruct then looks like:

src_list = [ 'mypaper.tex', Glob('imgs/*.pdf'), Glob('imgs/*.png') ]
PDF(target = 'mypaper.pdf', source = src_list)
Of cource, one can still use make to compile LaTeX documents. This is a good Makefile for a start.

This post in Russian: Автоматическая компиляция документов LaTeX