How to speed up LaTeX compilation with several TikZ pictures?
Read section 53, "Externalizing Graphics", of the PGF manual.
Make is Your Friend!
This problem was solved ages ago, so let's take advantage of all the nice tools Unix gave us!
The best solution I can think of for a large document is to simply not have any \begin{tikzpicture}
or \tikz
macros in it and do everything with \includepdf
.
To this end, I made a makefile (for GNU Make) that looks (and I do mean looks – Makefiles are old technology – so old that tabs are actually significant syntax...) sort of like this for a project of mine:
TIME = /usr/bin/time -p
LATEXMK = latexmk -silent -f -g --pdf
PDFLATEX = pdflatex -interaction=batchmode
PDFCROP = pdfcrop
RM = /bin/rm
default : Project.pdf
StandAloneGraphicsTeXFiles = $(wildcard *_sag.tex)
PDFGraphics = $(patsubst %_sag.tex,%_sag.pdf,$(StandAloneGraphicsTeXFiles))
InputTeXFiles = $(wildcard *_input.tex)
%_sag.pdf : %_sag.tex
$(PDFLATEX) $<
$(PDFCROP) $@ $@
Project.pdf : $(PDFGraphics) $(InputTeXFiles) Project.tex
$(TIME) $(LATEXMK) Project.tex
clean : .PHONY
$(RM) -f -- *.aux *.bak *.bbl *.blg *.log *.out *.toc *.tdo _region.*
depclean : clean
$(RM) -f -- *_sag.pdf
distclean : depclean
$(RM) -f -- Project.pdf
.PHONY :
How do I use that?!
Whenever you'd use a tikzpicture
environment or a \tikz
macro, give your picture a suggestive name, say riemann_sum
, put the TikZ code in a single standalone document (with some boilerplate such that it matches the style of your main document. For example we don't want Computer Modern in our pictures while the main document is typeset with Times or a 10pt/12pt font size clash) called riemann_sum_sag.tex
and use \includepdf{riemann_sum_sag}
instead. The goal is to not have a single picture being compiled when you run make
without having modified a *_sag.tex
file. If this is not possible because you need to \ref
something inside a picture, then so be it, but try to keep that to a minimum and instead choose good captions or something.
You'll also notice that there is a rule for files matching *_input.tex
. This is for splitting the project into multiple files which is of course always a good idea when doing large projects. The rule detects whether such a file has been modified, and if it has triggers a recompilation of the document. LaTeX's \includeonly
feature might be a good companion to this.
You can try creating a Precompiled Preamble (Wayback machine snapshot) or "format" for your document. This technique basically caches all the computations that LaTeX does when processing the preamble which is the stuff before \begin{document}
. If your packages, macro definitions, etc are not changing it can produce a noticeable savings in time.
Update
Just noticed the reference to TikZ- in this case Scott's suggestion is the most pertinent.
Update 2:
The mylatexformat package can aid in creating and using format files.
See also Speeding up compilation using precompiled preamble with LuaTeX