# Reasons for saving a box?

I've been reading through a number of answers that use \newsavebox to save the contents of a box. As far as I can tell it's a workaround for fragility of some kind. Is that right? If not, what is the point of saving a box?

-
Usually it is one of either for 1) measuring the dimenension of the content (for some later use; for example \phantoms), or 2) re-use the content multiple times (say, same header content on all pages). –  morbusg Dec 16 '12 at 11:48
Further to @morbusg's comment, it is needed when you want to align the content and the alignment depends on the dimensions of the content as well as the dimensions of other, similar contents. For example, if you want to align m x n pictures inside a figure environment. –  Marc van Dongen Dec 16 '12 at 12:50

Box usage isn't (normally) related to fragility. Boxes (and glue) are the fundamental typesetting constructs that TeX uses. All of TeX's typesetting is done by positioning boxes. Saving a box (as opposed to saving a command) means that you are saving a fragment of typeset text rather than saving a list of tokens to be executed. As mentioned in comments there are many reasons for wanting to do that. It might be to measure the width of some typeset construct, it might be that executing the commands is very costly in terms of execution time and you want to re-set copies of the same result multiple times, or it may just be that you want to pass a typeset fragment to another command (such as \fbox where it is more convenient to first set the contents into a box rather than directly as a command argument (perhaps because the contents involve verbatim material.)

-

i've used it to save and reuse a small graphic to replace the amsthm proof "tombstone":

\newsavebox{\QEDbox}
\savebox{\QEDbox}{%
\raisebox{-3pt}{\includegraphics[scale=.7]{proofpicture}}}
\renewcommand{\qedsymbol}{\usebox{\QEDbox}}


relevant quote from the texbook (p.329):

It's much faster to copy a box than to build it up from scratch.

-

I can't name all of the situations to use a savebox but one particular case that I've found it very useful is for aligning subfigures

The idea is to save the largest image into a box, which you can then measure to help with the \vfill in the other subfigures. This allows the caption and the images to remain aligned as you wish.

\documentclass{article}

\usepackage{tikz}
\usepackage{subcaption}

\newsavebox{\tempbox}
\begin{document}

% store the bigger of the pictures in a vbox
\sbox{\tempbox}{%
\begin{tikzpicture}
\draw circle (1.25cm) {};
\end{tikzpicture}%
}

\begin{figure}
\begin{subfigure}[t]{0.3\textwidth}
\centering
% use the save box
\usebox{\tempbox}
\caption{Biggest (using a savebox)}
\end{subfigure}%
\begin{subfigure}[t]{0.3\textwidth}
\centering
% for the other figures, you can use \vfill as follows
\vbox to\ht\tempbox{
\begin{tikzpicture}
\draw circle (.5cm) {};
\end{tikzpicture}%
\vfill
}
\caption{Top justified}
\end{subfigure}%
\begin{subfigure}[t]{0.3\textwidth}
\centering
% vertically centered
\vbox to\ht\tempbox{
\vfill
\begin{tikzpicture}
\draw circle (.5cm) {};
\end{tikzpicture}%
\vfill
}
\caption{Vertically centered}
\end{subfigure}%
\end{figure}

\end{document}

-

Mostly I use it to reuse parts of graphics inside the picture environment. An example is printing my business card:

\documentclass[11pt,DIV=33]{scrlttr2}
\usepackage[utf8]{inputenc}
\usepackage{ngerman}
\usepackage{hyperref}

\pagestyle{empty}

\begin{document}

\setlength{\unitlength}{1mm}

\newkomavar{name}
\newkomavar{street}
\newkomavar{city}
\newkomavar{zip}
\newkomavar{phone}
\newkomavar{mobile}
\newkomavar{email}
\newkomavar{website}

\setkomavar{name}{Mrs. Example}
\setkomavar{street}{Some street 123}
\setkomavar{zip}{12345}
\setkomavar{city}{Hometown}
\setkomavar{phone}{123 456 789}
\setkomavar{mobile}{987 654 321}

\fontshape{sc}
\selectfont

\newsavebox{\myfrontside}

\savebox{\myfrontside}(85,54){
\put(-42.5,0){\makebox(85,54){\shortstack[r]{\vspace{-3pt} \tiny www.\\ \tiny mail@}
\large \usekomavar{name}
\small .com}}
\put(-40,2.5){\makebox(80,0)[bl]{\tiny\shortstack[l]{
\usekomavar{street}, \usekomavar{zip} \usekomavar{city}\\
\usekomavar{phone}, \usekomavar{mobile}
}}}
}

\begin{picture}(178,253)(-7,22)

% crop marks
\linethickness{0.0001in}
\multiput(-2,0)(0,54){6}{\line(-1,0){5}}
\multiput(172,0)(0,54){6}{\line(1,0){5}}
\multiput(0,-2)(85,0){3}{\line(0,-1){5}}
\multiput(0,272)(85,0){3}{\line(0,1){5}}

% place ten cards on the page
\multiput(0,0)(0,54){5}{\usebox{\myfrontside}}
\multiput(85,0)(0,54){5}{\usebox{\myfrontside}}

\end{picture}
\end{document}


See https://en.wikibooks.org/wiki/LaTeX/Picture#Multiple_Use_of_Predefined_Picture_Boxes for another example and explanations.

-
Can you give the details of your answer here? it is much better to include the instructions in the answer's body, as links may expire and render the post useless. –  hpesoj626 Dec 16 '12 at 15:11
@hpesoj626 You're right, I added an example. –  hielsnoppe Dec 17 '12 at 7:52