tikzexternalize optimizes away savebox boxes?

I'm having some issues with savebox and tikzexternalize, when I compile the following code without external everything works as expected, if I enable it the single images get generated correctly but the final document optimizes them away.

Is there a better way to combine tikzpictures as nodes of another tikzpicture? Am I using savebox correctly?

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{external}
\tikzexternalize

\begin{document}
\newsavebox{\boxA}
\savebox{\boxA}{
\begin{tikzpicture}
\node {I am box A};
\end{tikzpicture}
}

\newsavebox{\boxB}
\savebox{\boxB}{
\begin{tikzpicture}
\node {I am box B};
\end{tikzpicture}
}

\begin{figure}
\begin{tikzpicture}
\node (C) {CENTER NODE};
\node [above=10pt of C] {\usebox{\boxA}};
\node [below=10pt of C] {\usebox{\boxB}};
\end{tikzpicture}
\end{figure}
\end{document}


Expected result:

Wrong result with tikzexternalize:

• IIRC, each externalized tikz picture produces an encapsulated PDF file, so instead of saveboxes you could use \includegraphics with the files Dec 25, 2015 at 14:53
• John, could you elaborate more? maybe with an example? Dec 25, 2015 at 16:00

What I do is turn off optimisation for the relevant pictures. You could fine-tune this further if you wished, but it generally isn't worth it for me, at least. (The pictures are typically the time-consuming bit in terms of compilation, so optimising other things away doesn't gain me much. Hence, switching off optimisation altogether for just the relevant pictures makes negligible difference to compilation time as far as I can tell.)

For example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,external}
\tikzexternalize

\begin{document}
\newsavebox{\boxA}
\savebox{\boxA}{%
\tikzset{external/optimize=false}%
\begin{tikzpicture}
\node {I am box A};
\end{tikzpicture}%
}
\newsavebox{\boxB}
\savebox{\boxB}{%
\tikzset{external/optimize=false}%
\begin{tikzpicture}
\node {I am box B};
\end{tikzpicture}%
}
\begin{tikzpicture}
\node (C) {CENTER NODE};
\node [above=10pt of C] {\usebox{\boxA}};
\node [below=10pt of C] {\usebox{\boxB}};
\end{tikzpicture}
\end{document}


• It works, with optimize=false. It is really slow compared to single big file compiling without externalize... now I'm trying to understand if the caching advantage justifies such a slower initial compile time (like seconds to minutes slower! maybe I'm doing something wrong). Dec 25, 2015 at 18:58
• I guess I'm giving up on tikzexternalize, too obscure for me to debug, and too slow with my project. Will try a more conservative standalone + GNU make approach Dec 25, 2015 at 19:29
• Whether it is worth it depends a lot on your particular situation e.g. how often the code of the pictures changes, how long the document takes to compile anyway etc. You can do things conditionally e.g. if certain packages take a while to load and you don't need them for the pictures, you can load them only when not externalising. But if you're happier with the make approach, that is surely a better solution for you ;).
– cfr
Dec 25, 2015 at 21:05