Having solved my previous problem involving \savebox and \tikzexternalizeI am already facing the next one. I would like to use a function that I have defined either in or after the preamble with \pgfmathdeclarefunction in a savebox. It works nicely when using it outside of a tikzpicture, but inside it will get optimized away (see comments in the example below). Why is that so? Or: How can I get TikZ not to optimize the figure away? Note that this solution doesn't help.
\documentclass{article}
\usepackage[english]{babel}
\usepackage{tikz,pgfplots}
\usetikzlibrary{positioning}
\usepgfplotslibrary{external}
\tikzexternalize
\begin{document}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1*exp(-(#2)^2/(#1^2))}%
}
\newsavebox\boxCPAin
\savebox\boxCPAin{
\begin{tikzpicture}
\tikzset{external/optimize=false}%
\draw[domain=-1:1,samples=200] plot(\x,{gauss(.33,\x)});
\end{tikzpicture}
}
\usebox\boxCPAin % works!
\begin{tikzpicture}
\node (test) {\usebox\boxCPAin}; % tikzpicture optimized away because it does not contribute to exported PDF
\end{tikzpicture}
\end{document}