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?
[Tex/LaTex] Reasons for saving a box
boxesmacros
Related Solutions
You can do this, with certain restrictions, using the wrapfig
package together with mdframed
or tcolorbox
. Here's a little example using tcolorbox
:
\documentclass{article}
\usepackage{wrapfig}
\usepackage{tcolorbox}
\usepackage{lipsum}
\newenvironment{WrapText}[1][r]
{\wrapfigure{#1}{0.5\textwidth}\tcolorbox}
{\endtcolorbox\endwrapfigure}
\newcommand\Text{% some text for the example
tesque cursus luctus mauris. Nulla malesuada porttitor diam. Donec felis erat, congue non, volutpat at, tincidunt tristique, libero. Vivamus viverra fermentum felis. Donec nonummy pellentesque ante. Phasellus adipiscing semper elit. Proin fermentum massa ac quam. Sed diam turpis, molestie vitae, placerat a, molestie nec, leo. Maecenas lacinia. Nam ipsum ligula, eleifend at, accumsan nec, suscipit a, ipsum.}
\begin{document}
\begin{WrapText}
\Text
\end{WrapText}
\lipsum[2]
\lipsum[2]
\begin{WrapText}
\Text
\end{WrapText}
\lipsum[1-3]
\end{document}
And a solution with mdframed
:
\documentclass{report}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}
\newenvironment{WrapText}[1][r]
{\wrapfigure{#1}{0.5\textwidth}\mdframed[backgroundcolor=gray!20,skipabove=0pt,skipbelow=0pt]}
{\endmdframed\endwrapfigure}
\newcommand\Text{% some text for the example
tesque cursus luctus mauris. Nulla malesuada porttitor diam. Donec felis erat, congue non, volutpat at, tincidunt tristique, libero. Vivamus viverra fermentum felis. Donec nonummy pellentesque ante. Phasellus adipiscing semper elit. Proin fermentum massa ac quam. Sed diam turpis, molestie vitae, placerat a, molestie nec, leo. Maecenas lacinia. Nam ipsum ligula, eleifend at, accumsan nec, suscipit a, ipsum.}
\begin{document}
\begin{WrapText}
\Text
\end{WrapText}
\lipsum[2]
\lipsum[2]
\begin{WrapText}
\Text
\end{WrapText}
\lipsum[1-3]
\end{document}
Since both mdframed
and tcolorbox
are highly customizable, you can obtain pretty easily the desired layout for the colored box.
And now, the bad news. Since wrapfig is being used, some restrictions will have to be observed; in particular, the package documentation mentions these:
• The environment should be placed so as to not run over a page break.
• The environment must not be placed in special places like lists.
• For esthetic reasons, only plain text should wrap around the figure. Section titles and big equations look bad; lists are bad if the figure is on the left. (All these function properly, they just don’t look very good.) Small equations look fine.
• It is convenient to begin the environment between paragraphs, but if you want placement in the middle of a paragraph, you must put the environment between two words where there is a natural line break.
TeX provides us with macros and registers. Macros are (simply) replaced by their definition, and so are used 'raw'. LaTeX wraps this concept up in \newcommand
, with the result that
\newcommand{\foo}{....}
can then be used as simply \foo
. On the other hand, registers have to be correctly accesses, at least in places where TeX does not 'expect' them. Again, LaTeX provides interfaces, for example
\newcounter{foo}
\value{foo}
which does the same as the TeX construct
\newcount\c@foo
\the\c@foo
Notably, using the register here needs an accessor function.
Boxes are registers, which while they don't quite work in the same way as other ones do broadly follow the same rules. In particular, they are not macros, so
\newsavebox{\foo}
\sbox{\foo}{...}
\foo
would be inconsistent: \foo
looks like a command (macro), but cannot be set using \newcommand
, etc. There are also places where TeX 'expects' a reference to a box, not the typeset content of a box. As you almost point out, those are not LaTeX constructs (\wd
is not a LaTeX command but a TeX primitive not moved 'out of the way' by LaTeX2e). However, in general the LaTeX2e kernel does leave us able to use plain TeX constructs along with LaTeX ones as a lot of the programming for LaTeX2e is done that way. Thus breaking
\newsavebox{\foo}% or \newbox\foo in TeX syntax
\wd\foo
would be problematic. (So would having \newsavebox
and \newbox
work fundamentally differently.)
Worth noting in this context is that
\ifdim\wd\somebox=...
is an expandable construct, while \widthof
and similar are not expandable (they typeset the material in a temporary box for measurement). As such, while the \ifdim\wd
syntax is TeX, not LaTeX, there are advantages to using it (e.g. works inside a \dimexpr
). Thus there are things that are much easier/clearer to do using the register number rather than the content of the box.
Best Answer
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.)