# Using align within savebox?

I have a custom environment called 'quiz' which puts its input into a savebox and then uses it later depending on a package option. The code works fine, unless I use an align* environment in which case I get 'Missing \endgroup inserted' errors.

Here's a minimum (non)-working example.

\documentclass{article}

\usepackage{amsmath}
\usepackage{ifthen}

% Define new 'quiz' environment
\newsavebox{\quizbox}
\newlength{\quizheight}

\newenvironment{quiz}{%
\begin{lrbox}{\quizbox}
}{%
\end{lrbox}
\usebox{\quizbox}
}{%
\settoheight{\quizheight}{\usebox{\quizbox}}
\fbox{\begin{minipage}[\quizheight]{\linewidth}%
\end{minipage}}
}
}

\begin{document}

\begin{quiz}
\begin{align*}
R_o = \int_{t=0}^{t_{max}} R(t)
\end{align*}
Test
\end{quiz}

\end{document}


Any thoughts?

-
You can't use align in a \mbox (and the same holds for lrbox) unless you embed it in a minipage. – egreg Oct 1 '12 at 15:35

As egreg mentioned in the comments, you can fix this with a minipage.

A side comment- you might also like to look at the answers package which does similar things to what you're working on.

\documentclass{article}

\usepackage{amsmath}
\usepackage{ifthen}
\usepackage[showframe=true]{geometry}

% Define new 'quiz' environment
\newsavebox{\quizbox}
\newlength{\quizheight}

\newenvironment{quiz}{%
\begin{lrbox}{\quizbox}%
\begin{minipage}{\linewidth}%
}{%
\end{minipage}%
\end{lrbox}%
\noindent\usebox{\quizbox}%
}{%
\settoheight{\quizheight}{\usebox{\quizbox}}
\noindent\fbox{\begin{minipage}[\quizheight]{\dimexpr\linewidth-\fboxsep-\fboxsep\relax}%
\end{minipage}}
}
}

\begin{document}

\begin{quiz}
\begin{align*}
R_o = \int_{t=0}^{t_{max}} R(t)
\end{align*}
Test
\end{quiz}

\end{document}

-
Thanks for the answers tip. I'll use the minipage for now, but may swap to that package later. – jkeirstead Oct 1 '12 at 15:50
@jkeirstead you're welcome :) I love the answers package, it's very powerful – cmhughes Oct 1 '12 at 15:53
I think that this would be better: \noindent\fbox{\begin{minipage}[\quizheight]{\dimexpr\linewidth-\fboxsep-\fboxs‌​ep\relax} – Peter Grill Oct 1 '12 at 18:08
@PeterGrill much better, thanks a lot :) – cmhughes Oct 1 '12 at 18:22

lrbox makes a horizontal (LR) box which can not directly contain vertical (par) mode display material, you need a minipage to get into vertical mode

\begin{quiz}\begin{minipage}{\textwidth}
\begin{align*}
R_o = \int_{t=0}^{t_{max}} R(t)
\end{align*}
Test
\end{minipage}\end{quiz}

-