11

Suppose I want to use an element numerous times, which is the best way to go at it (I chose a tikz-drawn arrow, because that is what made me think of the question, but I suppose it could be anything, really):

  • define a new command with \newcommand: as I understand it, this leads to the expansion of the defined command with its definition, so it's basically a shortcut that saves time of having to repeatedly type the same code again and again. With my tikz example below that would mean, the arrow would be drawn fresh every time.

  • define a savebox/sbox with \newsavebox: I am not sure what happens here, but assumed it's something like a call-by-reference, meaning the object is only rendered once and the result is "copied", whenever \usebox is used.

To me, the end result looks the same, but - depending on the complexity of the reused object - it could result in a big difference in compile time, rendering at every occurrence vs. rendering once and essentially just copying a picture.

Ergo: Which method should be used?

Is it a matter of personal preference, or does the end result in fact differ in subtle ways my simple arrow example does not show? Are maybe my assumptions off the mark?

The code for clarification:

\documentclass{article}
\usepackage{tikz}

\newcommand{\myDownArrow}{\tikz\draw (0,0)--++(10pt,0)--++(0,-10pt)--++(5pt,0)--++(-10pt,-5pt)--++(-10pt,5pt)--++(5pt,0)--cycle;}

\newsavebox{\myDownArrowBox}
\sbox{\myDownArrowBox}{\tikz\draw (0,0)--++(10pt,0)--++(0,-10pt)--++(5pt,0)--++(-10pt,-5pt)--++(-10pt,5pt)--++(5pt,0)--cycle;}


\begin{document}
\verb|\myDownArrow|\myDownArrow \qquad \verb|\usebox{\myDownArrowBox}|\usebox{\myDownArrowBox}
\end{document}

and the obvious result: result

1 Answer 1

10

A box is fix. You can resize it but apart from this you can't change much. A macro is executed again when you call it, so it can be different depending on external conditions like counters or colors.

\documentclass{article}
\usepackage{tikz}

\tikzset{arrowstyle/.style={}}

\newcommand{\myDownArrow}{\tikz\draw[arrowstyle] (0,0)--++(10pt,0)--++(0,-10pt)--++(5pt,0)--++(-10pt,-5pt)--++(-10pt,5pt)--++(5pt,0)--cycle;}

\newsavebox{\myDownArrowBox}
\sbox{\myDownArrowBox}{\tikz\draw[arrowstyle] (0,0)--++(10pt,0)--++(0,-10pt)--++(5pt,0)--++(-10pt,-5pt)--++(-10pt,5pt)--++(5pt,0)--cycle;}


\begin{document}
\verb|\myDownArrow|\myDownArrow \qquad \verb|\usebox{\myDownArrowBox}|\usebox{\myDownArrowBox}

{\color{green}\verb|\myDownArrow|\myDownArrow \qquad \verb|\usebox{\myDownArrowBox}|\usebox{\myDownArrowBox}}

\tikzset{arrowstyle/.style={line width=2pt, red}}
\verb|\myDownArrow|\myDownArrow \qquad \verb|\usebox{\myDownArrowBox}|\usebox{\myDownArrowBox} 


\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .