1

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}
Antonio
  • 197
  • You don't need to follow the answer you link to to use \tikzexternalize, you can simply place \usetikzlibrary{external} and \tikzexternalize in the preamble and then place your tikzpicture environments inside figure environments with the appropriate \caption, \label. This answer gives some basics of using the external TikZ library or check the Externalization Library section of the pgf for the thorough documentation. – Dai Bowen Mar 01 '17 at 21:09
  • 1
    In response to your edit, you have ]]{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

1 Answers1

2

I suppose you don't need to use the xargs package. In this case you can define a mytikz environment just as

\usepackage{environ}

\NewEnviron{mytikz}[3][]%
  {\begin{figure}[htp]
   \centering
   \begin{tikzpicture}[#1]
   \BODY
   \end{tikzpicture}
   \caption{#2}%
   \label{#3}%
   \end{figure}%
  }

and use it as

\begin{mytikz}[tikz options for your drawing]{caption}{label}
... tikz commands ...
\end{mytikz}

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize
\usepackage{environ}

\NewEnviron{mytikz}[3][]%
  {\begin{figure}[htp]
   \centering
   \begin{tikzpicture}[#1]
   \BODY
   \end{tikzpicture}
   \caption{#2}%
   \label{#3}%
   \end{figure}%
  }

\begin{document}
\tikzset{external/force remake=true}
\begin{mytikz}[every path/.style={red}]{my caption}{figlabel}
\draw(0,0) circle (1cm);
\node {hello world};
\end{mytikz}

See my great figure~\ref{figlabel}.

\end{document}
gernot
  • 49,614