[Tex/LaTex] Precompiled preamble with TikZ/Externalize

pdftexpreambletikz-externaltikz-pgf

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:

fmt.tex – the header

\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}

Best Answer

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

So it should be:

fmt.tex - the header

\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}
Related Question