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

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

1 Answer 1

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

3
  • 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
    Commented 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
    – filippo
    Commented 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
    Commented Dec 25, 2015 at 21:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .