Forest
Tutorial
2.7 Externalization¶
Forest can be quite slow, due to the slowness of both PGF/TikZ and its own computations. However, using externalization, the amount of time spent in Forest in everyday life can be reduced dramatically. The idea is to typeset the trees only once, saving them in separate PDFs, and then, on the subsequent compilations of the document, simply include these PDFs instead of doing the lenghty tree-typesetting all over again.
Forest’s externalization mechanism is built on top of TikZ’s external library. It enhances it by automatically detecting the code and context changes: the tree is recompiled if and only if either the code in the forest environment or the context (arbitrary parameters; by default, the parameters of the standard node) changes.
To use Forest’s externalization facilities, say:1
\usepackage[external]{forest}
~\tikzexternalize~
If your forest environment contains some macro, you will probably want the externalized tree to be recompiled when the definition of the macro changes. To achieve this, use \ forestset{external/depends on macro=\macro}. The effect is local to the TeX group.
TikZ’s externalization library promises a \label inside the externalized graphics to work out-of-box, while \ref inside the externalized graphics should work only if the externalization is run manually or by make [Tantau 2007, §32.4.1]. A bit surprisingly perhaps, the situation is roughly reversed in Forest. \ref inside the externalized graphics will work out-of-box. \label inside the externalized graphics will not work at all. Sorry. (The reason is that Forest prepares the node content in advance, before merging it in the whole tree, which is when TikZ’s externalization is used.)
1 When you switch on the externalization for a document containing many forest environments, the first compilation can take quite a while, much more than the compilation without externalization. (For example, more than ten minutes for the document you are reading!) Subsequent compilations, however, will be very fast.