Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
lrbox is the environment form of \sbox; you can insert a minipage in lrbox. –  egreg Jan 19 '12 at 22:48

3 Answers 3

up vote 5 down vote accepted

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}
share|improve this answer

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.

share|improve this answer

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)?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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