# Error using \tikzexternalize with forest

Using forest package for simple truth trees, and I want to externalize because there are a lot of them. Here is the code example:

\documentclass{article}
\usepackage[external]{forest}
\tikzexternalize

\begin{document}

\begin{forest}
[$B \rightarrow C$
[$C$
[$\neg A$
[$\neg B$
]
[$C$
]
]
[$B$
[$\neg B$
]
[$C$
]
]
]
]
\end{forest}

\end{document}


Here is the error:

Command Line:   texify.exe --src --tex-option=--interaction=errorstopmode
--tex-option=--synctex=-1

.....

ABD: EveryShipout initializing macros forest: Invoking a recursive call to
generate the external picture 'Test2-fores t-1' for the following
context+code: 'dj,8.39996pt,0.4pt,.3333em,.5\pgflinewidt h
,.3333em,.5\pgflinewidth @@ [$B \rightarrow C$ [$C$ [$\neg A$ [$\neg B$ ] [$C$ ] ] [$B$ [$\neg B$ ] [$C$ ] ] ] ] ' ! Undefined control sequence.
\forest@temp ->\path
[draw](node@2.parent anchor)--(node@3.child anchor);
l.24 \end{forest}

? x No pages of output. Transcript written on Test2.log. texify: latex failed
for some reason (see log file).


Everything works fine if I comment out \tikzexternalize.

• This looks like a major bug. It seems that the forest environment doesn’t include the tikzpicture environment correctly when externalizing. (forest also presets the command to copy files to cp which does not work (at least on Windows).) It might be best to notify the author, see the package documentation for contact information. – Qrrbrbirlbel Oct 15 '13 at 18:00

This is indeed a bug, thanks for finding it!

I have introduced the bug in v1.03 while introducing keys begin draw and end draw. (The idea behind the keys is to be able to customize the tikzpicture environment used throughout the document, or even use something else than tikz to draw the tree.)

The bug arose because the previous \tikz command was replaced by a pair of \pgfkeysalso{/forest/begin draw} and \pgfkeysalso{/forest/end draw} defaulting to \begin{tikzpicture} and \end{tikzpicture}. While processing the expansion of \begin{tikzpicture}, tikz's externalization macros were searching for \end{tikzpicture} to collect the picture environment. However, \end{tikzpicture} was hidden in the expansion of \pgfkeysalso{/forest/end draw} and could thus not be found. I have corrected this by expanding the keys in advance. (I have also fixed another bug I found along the way, in \forest@externalizing@maybeoptimize.)

The corrected version can be temporarily found here. Please let me know if it works. If it does, I'll post a new version to CTAN asap.

A side note on pgf/tikz's externalization mechanism. While I found it extremely handy in general, I needed to hack into it to use it in forest and the resulting code seems like black magic. (I shudder to think what adjustments will be necessary with the new version of pgf/tikz.) While working on the issue, I had some ideas on how to make it easier to use for package writers (also for packages that do not use pgf/tikz) and, most importantly, how to make it radically faster using multi-page PDFs. So, a question: is there interest for such a new version?

• That definitely worked. – pgregory Oct 16 '13 at 15:18
• Just wondering if you could say whether this should currently work with TeX Live 2015 and the current version of forest. I've tried with both 1.07 and 1.03 (from your link) and get errors in both cases. (Adding etex for the 1.03 version, of course.) On the other hand, this has never worked for me, so maybe I am the bug. But see this question. – cfr Jul 10 '15 at 2:34

I agree that this looks like a bug. As @Qrrbrbirlbel mentioned, the tikzpicture environment is not being set up correctly when using the external option, and therefore the compiler fails to recognize tikz commands, such as \path.

I managed to compile the example by changing the following lines in the forest package code:

%% Old
\pgfkeysalso{/forest/begin draw}%
...
\pgfkeysalso{/forest/end draw}%


to

%% New
\begin{tikzpicture}
...
\end{tikzpicture}


Perhaps someone can help with understanding why this error arises only when the external option is set, and propose a better solution.

Here is a different version that works. (Note: this needs to be compiled using: pdflatex -shell-escape file.tex.)

\documentclass{article}
\usepackage[external]{forest2}
\tikzexternalize

\begin{document}

\begin{forest}
[$B \rightarrow C$
[$C$
[$\neg A$
[$\neg B$
]
[$C$
]
]
[$B$
[$\neg B$
]
[$C$
]
]
]
]
\end{forest}

\end{document}