# Precompiled preamble with TikZ/Externalize

I'm using the externalize capability of TikZ (external library) to speed up compilation of my TikZ pictures (as explained in Script to automate externalizing TikZ graphics).

In addition, I would like to speed up compilation by precompiling the preamble. It is explained in http://magic.aladdin.cs.cmu.edu/2007/11/02/precompiled-preamble-for-latex/ how this can be achieved. The command for precompiling the header is:

pdflatex -ini -job-name="main" "&pdflatex fmt.tex\dump"


where fmt.tex contains the documentclass and package declarations, and the header is subsequently inserted into the file by having the first line say

%&fmt


Both methods by themselves work well and result in nice speed-ups. Now, I am trying to apply both methods at the same time. This gives me the following error-message during compilation:

! Package tikz Error: Sorry, the system call 'pdflatex -shell-escape -halt-on-e
rror -interaction=batchmode -jobname "figures/fmt-figure0" "\def\tikzexternalre
aljob{fmt}\input{fmt}"' did NOT result in a usable output file 'figures/fmt-fig
ure0' (expected one of .pdf:.jpg:.jpeg:.png:). Please verify that you have enab
led system calls. For pdflatex, this is 'pdflatex -shell-escape'. Sometimes it
is also named 'write 18' or something like that. Or maybe the command simply fa
iled? Error messages can be found in 'figures/fmt-figure0.log'. If you continue
now, I'll try to typeset the picture.


Note: I am compiling with option -shell-escape already. In addition, I precompiled the header with that option as well.

Minimal working (breaking) example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[prefix=figures/]


test.tex - the main file

%&fmt
\begin{document}
\begin{tikzpicture}
\node[draw, circle, minimum width=1cm] {};
\end{tikzpicture}
\end{document}

• Note that you can place links directly into questions and answers. You don't need to use the 'add link' button. The links look the same or even better: links to other stackexchange posts will be formatted automatically. Apr 27 '11 at 10:32
• Maybe you need to move some of the TikZ code to the dynamic preamble. The guide you linked states that everything which loads auxiliary files must not be pre-compiled, so just move the external library from the static to the dynamic preamble. If this doesn't work please consider to add a minimal working example (MWE) that causes this error. Otherwise it will be difficult to fix it. Apr 27 '11 at 11:18
• I'm doing the exact same thing here, yet all is good. Note that my \tikzexternalize command is in my dynamic preamble. The library itself can be in the static preamble unlike with Martin said. Apr 27 '11 at 11:51
• @remeovs: Thanks for sharing. I just meant to experiment with the position of the externalization code. Moving the library would just be the first step. Apr 27 '11 at 12:52
• Thanks guys. The problem was that \tikzexternalize was in the static preamble. Apr 27 '11 at 15:18

Add the \tikzexternalize command to the dynamic preamble and all will be good.

So it should be:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}


test.tex - the main file

%&fmt
\tikzexternalize[prefix=figures/]
\begin{document}
\begin{tikzpicture}
\node[draw, circle, minimum width=1cm] {};
\end{tikzpicture}
\end{document}

• Could you provide a full example? I struggle getting it working... Feb 23 '15 at 12:08
• Does not work for me (not even with the given example), unless I forget to recompile the preamble, \tikzexternalize is ignored and the document is built without externalization. Using MacTeX (that is TeXLive, essentially) 2016, and -shell-escape. Jul 21 '17 at 2:14

Other answers didn't work for me and since I can't comment, this is how I did it: I added a -fmt=yourfmtfile option in the external/system call option. This is how my main file begins:

%&preamble
\usetikzlibrary{external}
\tikzset{external/system call={xelatex -fmt=preamble.fmt \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"}}
\tikzexternalize


I am using xelatex but I guess that it is the same for pdflatex as well.

• Guessing can be a bit dangerous; does it actually work with pdflatex? Also, it might be helpful if you explained where this should be in the questioner's files and used the MWEs he posted, so he can see exactly what you did and try it easily to see if works. May 25 '15 at 4:03
• EDITED: This worked for me for pdflatex, replacing xelatex with pdflatex in the \tikzset argument. I also needed \tikzexternalize in the dynamic prelude (the rest doesn't matter), per tex.stackexchange.com/a/16764/1340, and of course -shell-escape for the main document. Jul 21 '17 at 16:11

If you need the tikzexternalize to be called in the static preamble (for example if you have other macros relying on that), you can do one of the following :

• name your format file after after the project jobname, either by running

pdflatex -ini -job-name="<yourJobNameHere>" "&pdflatex fmt.tex\dump"


or if you don't want to have to recompile the preamble each time you make a new project, by simply creating a symbolic link in your working directory, named yourJobNameHere.fmt, linking to the file main.fmt.

• add in the dynamic preamble the following command :

\renewcommand{\tikzexternaljobname}{yourJobNameHere}


Depending on the macros you need to write in the precompiled preamble, the second solution might lead to problems, though.