8

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:

Expected result

Wrong result with tikzexternalize:

Result with tikzexternalize

filippo
  • 598

1 Answers1

5

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}

Expected output w/o optimisation

cfr
  • 198,882
  • 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). – filippo Dec 25 '15 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 – filippo Dec 25 '15 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 '15 at 21:05