5

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}

% Load required packages
\usepackage{amsmath}
\usepackage{ifthen}

% Define new 'quiz' environment
\newsavebox{\quizbox}
\newlength{\quizheight}
\newboolean{showanswer}
\setboolean{showanswer}{true}

\newenvironment{quiz}{%
  \begin{lrbox}{\quizbox}
}{%
  \end{lrbox}
  \ifthenelse{\boolean{showanswer}}{%
    \usebox{\quizbox}
  }{%
    \settoheight{\quizheight}{\usebox{\quizbox}}
    \fbox{\begin{minipage}[\quizheight]{\linewidth}%
        Answer:\hspace{\linewidth}\\[1em]
    \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?

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

2 Answers 2

6

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}

% Load required packages
\usepackage{amsmath}
\usepackage{ifthen}
\usepackage[showframe=true]{geometry}

% Define new 'quiz' environment
\newsavebox{\quizbox}
\newlength{\quizheight}
\newboolean{showanswer}
\setboolean{showanswer}{true}

\newenvironment{quiz}{%
  \begin{lrbox}{\quizbox}%
  \begin{minipage}{\linewidth}%
}{%
  \end{minipage}%
  \end{lrbox}%
  \ifthenelse{\boolean{showanswer}}{%
    \noindent\usebox{\quizbox}%
  }{%
    \settoheight{\quizheight}{\usebox{\quizbox}}
    \noindent\fbox{\begin{minipage}[\quizheight]{\dimexpr\linewidth-\fboxsep-\fboxsep\relax}%
        Answer:\hspace{\linewidth}\\[1em]
    \end{minipage}}
  }
}


\begin{document}

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

\end{document}
3
  • Thanks for the answers tip. I'll use the minipage for now, but may swap to that package later.
    – jkeirstead
    Oct 1, 2012 at 15:50
  • @jkeirstead you're welcome :) I love the answers package, it's very powerful
    – cmhughes
    Oct 1, 2012 at 15:53
  • I think that this would be better: \noindent\fbox{\begin{minipage}[\quizheight]{\dimexpr\linewidth-\fboxsep-\fboxsep\relax} Oct 1, 2012 at 18:08
3

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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .