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

share|improve this question
    
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

2 Answers 2

up vote 6 down vote accepted

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