I am LaTex newbie, please, be patient.
The great answer tells how to correctly work with \tikzexternalize:
Problem with environment expansion and the Tikz external library.
My question is: how do I extend the code to add \caption and \label parameters?
EDIT: Thanks to gernot my code works fine:
\NewEnviron{mytikz}[3][]%
{\begin{figure}[htp]
\centering
\begin{tikzpicture}[#1]
\BODY
\end{tikzpicture}
\caption{#2}%
\label{#3}%
\end{figure}%
}
% Elemcube command taken from: http://blog.dorian-depriester.fr/latex/tikz/empilements-de-cubes-sous-tikz
\newcommand{\elemcube}[4][white]
{
\draw [fill=#1!30,very thin] (#2+1,#3,#4) -- ++(0,1,0) -- ++(0,0,1) -- ++(0, -1, 0) -- cycle;
\draw [fill=#1!40,very thin] (#2,#3+1,#4) -- ++(1,0,0) -- ++(0,0,1) -- ++(-1, 0, 0) -- cycle;
\draw [fill=#1!10,very thin] (#2,#3,#4) -- ++(1,0,0) -- ++(0,1,0) -- ++(-1, 0, 0) -- cycle;
}
\begin{mytikz} [x=(90:0.2cm), y=(0:0.2cm), z=(40:0.1cm), axis/.style={->,blue,thick}]{A sample 3-d array.}{fig:flyx}
\def\xx{8}
\def\yy{8}
\def\zz{8}
\foreach \z in{\zz,...,0}
{
\foreach \x in{0,...,\xx}
{
\foreach \y in{0,...,\yy}
{
\elemcube{\x}{\y}{\z}
}
}
}
% draw axes
\draw[axis] (0,0,0) -- (\xx+1.5,0,0) node[anchor=north east]{$lat$};
\draw[axis] (0,0,0) -- (0,\yy+1.5,0) node[anchor=north west]{$lon$};
\draw[axis] (\xx+1,\yy+1,0) -- (\xx+1,\yy+1,\zz+1.5) node[anchor=south]{\large $time$};
\end{mytikz}

\tikzexternalize, you can simply place\usetikzlibrary{external}and\tikzexternalizein the preamble and then place yourtikzpictureenvironments insidefigureenvironments with the appropriate\caption,\label. This answer gives some basics of using theexternalTikZ library or check the Externalization Library section of thepgffor the thorough documentation. – Dai Bowen Mar 01 '17 at 21:09]]{A sample 3-d array.}when you mean to have]{A sample 3-d array.}so the first token after the optional argument is]which is taken to be the first mandatory argument rather than{A sample 3-d array.}. – Dai Bowen Mar 02 '17 at 09:48