5

The following MWE demonstrates that the list of answers accumulated into a box does not respect page breaks when printed (code adapted from Multiple Choice Answer Key in exam package at the end of document:

\documentclass[answers]{exam}
\usepackage{pgffor}

\newbox\allanswers
\setbox\allanswers=\vbox{}

\newenvironment{answer}
{%
    \global\setbox\allanswers=\vbox\bgroup %
    \unvbox\allanswers%
    \thequestion. \thechoice\\
}%
{%
    \egroup%
}

\newcommand{\CC}{\CorrectChoice \leavevmode\begin{answer}\end{answer}}
\newcommand{\showallanswers}{%
    \ifprintanswers \centering Here are the answers: \par \usebox\allanswers \fi}


\begin{document}
    \begin{questions}
        \foreach \i in {1,...,50}{
        \question[5] Important Question 1
        \begin{choices}
            \CC 100
        \end{choices}
    }
    \end{questions}

    \newpage
    \showallanswers
\end{document}

enter image description here

How can I format the content of the box so that it stays into the document margins (ideally it should be printed in a multicol environment, but I can't seem to do it)?

6
  • 2
    \usebox prints an indivisible object.
    – egreg
    Commented Nov 5, 2015 at 15:37
  • @egreg ok, is there something I could use instead?
    – d-cmst
    Commented Nov 5, 2015 at 15:38
  • @domenico \unvbox\allanswers
    – egreg
    Commented Nov 5, 2015 at 15:39
  • @egreg if the list could be printed into a multicol environment it would be the perfect answer :-)
    – d-cmst
    Commented Nov 5, 2015 at 15:43
  • @egreg actually now I'm already able to put it inside a multicol environment, so you could turn your comment into an answer as it is. Thank you
    – d-cmst
    Commented Nov 5, 2015 at 15:47

1 Answer 1

6

A box cannot be split across pages, it is an indivisible unit.

However, you can “unbox” it:

\newcommand{\showallanswers}{%
  \ifprintanswers
    {\centering Here are the answers: \par}
    \unvbox\allanswers
  \fi
}

This will append the vertical list of the vbox to the current vertical list. Note that no interline glue is added at either end of an “unvboxed box”, so spacing could need some adjustment.

I would use a different method, though.

\documentclass[answers]{exam}
\usepackage{environ,pgffor}

\newtoks\allanswers

\NewEnviron{answer}
{%
 \edef\temp{%
   \the\allanswers % the previous ones
   \thequestion. \thechoice
   \noexpand\par % maybe \par has been redefined
   \unexpanded\expandafter{\BODY}%
 }
 \global\allanswers=\expandafter{\temp}
}

\newcommand{\CC}{\CorrectChoice \leavevmode\begin{answer}\end{answer}}
\newcommand{\showallanswers}{%
    \ifprintanswers {\centering Here are the answers: \par} \the\allanswers \fi}


\begin{document}
    \begin{questions}
        \foreach \i in {1,...,50}{
        \question[5] Important Question 1
        \begin{choices}
            \CC 100
        \end{choices}
    }
    \end{questions}

    \newpage
    \showallanswers
\end{document}

This avoids typesetting the answers in advance, so you can better use multicols at the time of printing the answers.

4
  • I had no idea you could break apart boxes. Does \unhbox exist as well? Is this exposed anywhere in expl3? Commented Nov 5, 2015 at 17:28
  • 1
    @SeanAllred Yes, of course: \hbox_unpack:N and \vbox_unpack:N
    – egreg
    Commented Nov 5, 2015 at 17:31
  • @SeanAllred where have you been?:-) Commented Nov 5, 2015 at 18:29
  • @DavidCarlisle In Mumps-land :) Fascinating language, Mumps -- almost as wacky as TeX :-) Commented Nov 5, 2015 at 18:58

You must log in to answer this question.

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