6

I want to have a list in my saved box. Why doesn't the following work?

\documentclass{article}
\usepackage{setspace}

\newsavebox{\mybox}

\newenvironment{myenv}{%
  \begin{lrbox}{\mybox}%
  \begin{list}{}{}%
}{%
  \end{list}%
  \end{lrbox}%
  \usebox{\mybox}%
}%

\begin{document}
\begin{myenv}
\item First item.
\item Second item.
\end{myenv}
\end{document} 

Must I use a \minipage?

  • lrbox is the environment form of \sbox; you can insert a minipage in lrbox. – egreg Jan 19 '12 at 22:48
7

The evironment lrbox uses the a simple hbox to save the contents. So you have no line breaks. Instead of using lrbox (internal it is a savebox) you can also use the command \setbox in combination with vbox to allow line breaks.

\newenvironment{myenv}{%
 \setbox\mybox=\vbox\bgroup%
  \begin{list}{}{}%
}{%
  \end{list}%
  \egroup
  \usebox{\mybox}%
}%

Here an complete example:

\documentclass{article}
\usepackage{setspace}

\newsavebox{\mybox}

\newenvironment{myenv}{%
 \setbox\mybox=\vbox\bgroup%
  \begin{list}{}{}%
}{%
  \end{list}%
  \egroup
  \usebox{\mybox}%
}%

\begin{document}
\begin{myenv}
\item First item.
\item Second item.
\end{myenv}
\end{document}
8

Yes, you do have to use a minipage. Indeed lrbox is just the environment form of \sbox that is a companion to \mbox which wouldn't accept a list either: you can't directly typeset a paragraph in a \mbox. But

\begin{lrbox}{\mybox}
\begin{minipage}{5cm}
\begin{enumerate}
\item a
\item b
\end{enumerate}
\end{minipage}
\end{lrbox}

is correct code. In a \mbox or \sbox or lrbox TeX doesn't split lines.

2

The environ package allows for "storing" of content in a macro \BODY. Perhaps you might be interested in this implementation:

enter image description here

\documentclass{article}
\usepackage{environ}% http://ctan.org/pkg/environ
\newsavebox{\mybox}
\NewEnviron{myenv}{%
  \let\olditem\item%
  \renewcommand{\item}[1][]{\olditem[##1]\strut}%
  \begin{lrbox}{\mybox}%
  \noindent\begin{minipage}{\linewidth}
  \begin{list}{}{}%
    \BODY%
  \end{list}%
  \end{minipage}
  \end{lrbox}%
  \usebox{\mybox}%
}

\begin{document}
Some text.\strut

\begin{myenv}
\item First item.
\item Second item.
\end{myenv}

Some text.
\end{document}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Like with most box-implementations of minipage, you may have to play around with the baselineskip measures of \prevdepth. The above redefinition of \item (adding \strut) is a quick-fix. For more on this, see How to keep a constant baselineskip when using minipages (or \parboxes)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.