This article originally appeared on http://olezfdtd.wordpress.com I’ve copied it over to my current blog to consolidate all my blogging efforts over the years in one place.
Using pgfplots allows you to quickly create very beautiful plots, that look like they belong to your paper, by automatically using the correct fonts and styles. It does this by using the latex compiler to make the figures. This approach is very powerful but has a major drawback: the latex compiler was not made to do this. As a result, latex tends to run out of memory very fast, and projects with a lot of pgfplots tend to take a long time to compile.
We’ve previously shown how to increase the amount of memory latex has available, but this only solves half the problem.
A proper way to fix this is using the tikzexternalize command. It causes latex to compile the figures in a separate run, generating pdfs. These pdfs are then included in the main document, reducing both the memory requirements and the compilation time.
Using tikzexternalize is very simple, two step process:
Call latex with the “shell-escape” option. This looks like this on linux:
pdflatex -shell-escape mydocument.tex
The easiest way to do this is to modify the build command of your latex-editor of choice.
Add the following lines of code to your latex preamble
The first line loads the tikzexternalize library, the second line activates it. The second line also contains an option that specifies the folder where the resulting pdfs and intermediary files should be stored.
Latex will now externalize any tikzpicture you make. You can optionally use
to specify a name for the resulting pdf file. This is strongly recommended, because latex will only recompile a figure if you remove the resulting pdf.