9

I'm using the exam class to prepare a test.

Unfortunately, the package allows page breaks inside the solution list of a question. Considering this toy example:

\question Example of question. Choose a solution:
\begin{checkboxes}
\choice A
\choice B
\choice C
\CorrectChoice D
\end{checkboxes}

It could be that a pagebreak happens between \choice B and \choice C. How could I prevent that?

Update

The solution proposed by Mico works unless we want to print the solutions to the test AND the last answer of a question is a \CorrectChoice.

Here's a MWE that reproduces the problem:

\documentclass[answers]{exam}
\usepackage[utf8]{inputenc}
\usepackage[a4paper]{geometry}
\usepackage{etoolbox}
\AtBeginEnvironment{checkboxes}{\par\medskip%
     \begin{minipage}{\textwidth}}
\AtEndEnvironment{checkboxes}{%
     \end{minipage}}

\CorrectChoiceEmphasis{}

\begin{document}

\begin{questions}

\question Random text for question 1 \begin{checkboxes} \choice answer 1 \choice answer 2 \choice answer 3 \choice answer 4 \choice answer 5 \CorrectChoice answer 6 \end{checkboxes}

\question Random text for question 2 \choice answer 1 \choice answer 2 \choice answer 3 \choice answer 4 \choice answer 5 \CorrectChoice answer 6 \end{checkboxes}

\question Random text for question 3 \choice answer 1 \choice answer 2 \choice answer 3 \choice answer 4 \choice answer 5 \CorrectChoice answer 6 \end{checkboxes}

\question Random text for question 4 \choice answer 1 \choice answer 2 \choice answer 3 \choice answer 4 \choice answer 5 \CorrectChoice answer 6 \end{checkboxes}

\question Random text for question 5 \choice answer 1 \choice answer 2 \choice answer 3 \choice answer 4 \choice answer 5 \CorrectChoice answer 6 \end{checkboxes}

\end{questions}

\end{document}

I've solved it by following the 2nd suggestion given here, that is, surround what you don't want to be broken by \parbox. (For the records, using \samepage didn't work.)

Jir
  • 265

2 Answers2

11

I suggest you modify the checkboxes environment to place them in a LaTeX minipage automatically, as is done in the following MWE. Note that the amount of vertical spacing introduced by the \medskip command can be changed, e.g., to \smallskip or \bigskip, or whatever amount you prefer.

Addendum, posted 10/25/2011: The OP has noted that my originally-provided answer causes a problem if the list of choices ends with a \CorrectChoice option. The answer below is updated to incorporate this possibility; the solution consists of adding the line

\if@correctchoice \endgroup \fi

to the code in \AtEndEnvironment{...}.

The full MWE is:

\documentclass[answers]{exam}
\usepackage{etoolbox}
\AtBeginEnvironment{checkboxes}{%
   \par\medskip\begin{minipage}{\linewidth}}
\makeatletter
\AtEndEnvironment{checkboxes}{%
   \if@correctchoice \endgroup \fi%
   \end{minipage}}
\makeatother

\begin{document}
\begin{questions}

\question Random text for question 1.

Choose a solution: 
\begin{checkboxes}
\choice A
\choice B
\choice C
\CorrectChoice D
\end{checkboxes}
\end{questions}
\end{document}

Observe that this solution implements Werner's suggestion to use the commands \AtBeginEnvironment and \AtEndEnvironment commands provided by the etoolbox package.

Mico
  • 506,678
  • 3
    I think it is much cleaner to patch it using etoolbox - using something like \AtBeginEnvironment/\AtEndEnvironment rather than redefining the entire environment in virtually the same way. Have you tried that? – Werner Oct 23 '11 at 01:07
  • Thanks, @Werner, for this suggestion. I've changed my answer. – Mico Oct 23 '11 at 12:02
  • 1
    I removed the [tag:etoolbox] tag from the question again. Tags should be about the question, not about the answers. As long etoolbox is not requested or mentioned by the question the tag should not be added. – Martin Scharrer Oct 23 '11 at 16:58
  • Well... there are some problems when there is a \CorrectChoice as last choice and I want to print the solution sheet. It says ! Extra }, or forgotten \endgroup. \endminipage ...pagefalse \color@endgroup \egroup \expandafter \@iiiparbox \... l.60 \end{checkboxes} But I cannot see how to modify the environment to prevent that. – Jir Oct 23 '11 at 19:09
  • Please provide a more-complete example of the problems you're encountering, say as an addendum to your original question. E.g., what other packages are you loading? – Mico Oct 23 '11 at 21:22
  • Sorry, I didn't notice your comment. I've edited the questions and provided a MWE reproducing the problem. – Jir Oct 25 '11 at 16:25
  • Please see my revised answer. – Mico Oct 25 '11 at 17:44
  • But this still allows page breaks between the question prompt and the list of answers. – CPBL Oct 06 '14 at 21:04
  • 1
    @ChristopherBarrington-Leigh - The OP's objective, as stated, was about preventing page breaks inside the list of answers. He/she did not seem concerned about having a potential page break between the question and the list of answers. – Mico Oct 06 '14 at 21:16
  • @Mico.. Quite right! [I'm not sure whether it is inappropriate to add another answer below (which is as much of a question than an answer) on this page, or whether it should be a new question..] – CPBL Oct 06 '14 at 21:21
  • @ChristopherBarrington-Leigh - I think the issue you raise is sufficiently different from one covered in this posting to warrant you posting a new question. There may also have been substantive changes in the exam document class in the meantime, making it worth taking a fresh look at these issues. – Mico Oct 06 '14 at 21:27
  • Thank you! Instead, I think this question http://tex.stackexchange.com/questions/120046/how-do-i-keep-questions-from-splitting-between-pages-in-exam/204863#204863 already covers it, so I've posted there and hope that topic gets reawakened. Thanks again – CPBL Oct 06 '14 at 21:31
5

If you wrap checkboxes in a minipage, that will stop pagebreaks

\begin{minipage}{\linewidth}
\begin{checkboxes}
 \choice A
 \choice B
 \choice C
 \CorrectChoice D
 \end{checkboxes}
 \end{minipage}

If you would like to allow pagebreaks between some choices, but not others, you might use the needspace package, which would allow code such as

\begin{checkboxes}
  \choice A
  \needspace{2\baselineskip}
  \choice B
  \choice C
  \CorrectChoice D
 \end{checkboxes}

This particular example would allow a break after choice A, but not between choice B and choice C

cmhughes
  • 100,947
  • It works, thanks! I voted for the other option as it saves me from modifying all environments. – Jir Oct 23 '11 at 17:55