LaTeX forum ⇒ Document ClassesHow do I keep exam choice options together on the same page?

Information and discussion about specific document classes and how to create your own document classes.
LavaTyper
Posts: 68
Joined: Sat Feb 11, 2012 2:38 am

How do I keep exam choice options together on the same page?

Postby LavaTyper » Tue Sep 02, 2014 3:33 pm

1. How do I keep exam choice options together on the same page?

2. And how do I change the spacing between adjacent choices?

I already went through the exam document class documentation and it did not answer my questions.


  1. \documentclass[answers, 10pt]{exam}
  2. \usepackage[margin=0.75in]{geometry}
  3. \usepackage{lipsum}
  4. \usepackage{indentfirst}
  5. \usepackage{enumerate}
  6.  
  7. \linespread{1}
  8.  
  9. \renewcommand{\rmdefault}{phv}
  10. \renewcommand{\sfdefault}{phv}
  11.  
  12.  
  13. \begin{document}\pagestyle{empty}\raggedright
  14.  
  15. \lipsum[1] \lipsum[1] \lipsum[1] \lipsum[1] \lipsum[1] \lipsum[2]
  16.  
  17.  
  18.  
  19. \begin{questions}
  20.  
  21. \question Which of the following is an example of the correct answer?
  22.  
  23. \begin{choices}
  24. \CorrectChoice first answer
  25. \choice second answer
  26. \choice third answer
  27. \choice fourth answer
  28. \choice fifth answer
  29. \end{choices}
  30.  
  31.  
  32. \question Which of the following is an example of the correct answer?
  33.  
  34. \begin{choices}
  35. \CorrectChoice first answer
  36. \choice second answer
  37. \choice third answer % This should be kept together with the first two choices
  38. \choice fourth answer
  39. \choice fifth answer
  40. \end{choices}
  41.  
  42.  
  43. \end{questions}
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. \end{document}

Tags:

User avatar
Johannes_B
Site Moderator
Posts: 3930
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Tue Sep 02, 2014 6:20 pm

It is right there behind the choices environment including an example.

  1. \documentclass[answers, 10pt]{exam}
  2. %\usepackage[margin=0.75in]{geometry}
  3. \usepackage{lipsum}
  4. %\usepackage{indentfirst}
  5. %\usepackage{enumerate}
  6.  
  7. %\linespread{1}
  8. %
  9. %\renewcommand{\rmdefault}{phv}
  10. %\renewcommand{\sfdefault}{phv}
  11.  
  12.  
  13. \begin{document}
  14. \pagestyle{empty}
  15. %\raggedright<-Gets you into trouble
  16.  
  17. \lipsum[1-5]
  18.  
  19. \begin{questions}
  20.  
  21. \question Which of the following is an example of the correct answer?
  22.  
  23. \begin{oneparchoices}
  24. \CorrectChoice first answer
  25. \choice second answer
  26. \choice third answer
  27. \choice fourth answer
  28. \end{oneparchoices}
  29.  
  30. \question Which of the following is an example of the correct answer?
  31.  
  32. \begin{choices}
  33. \CorrectChoice first answer
  34. \choice second answer
  35. \choice third answer % This should be kept together with the first two choices
  36. \choice fourth answer
  37. \choice fifth answer
  38. \end{choices}
  39.  
  40. \end{questions}
  41. \end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

LavaTyper
Posts: 68
Joined: Sat Feb 11, 2012 2:38 am

Postby LavaTyper » Tue Sep 02, 2014 10:22 pm

Johannes_B, see my comments below:

1. How do I keep exam choice options together on the same page? (I did not ask how to condense all the options into one line; the actual exam will have short statements for response options and so I will need one choice per line.)

2. And how do I change the spacing between adjacent choices? (The space between A and B should be single spaced apart, and instead it is more than that, like, 1.5x the line spread.)

(Also ragged2e doesn't work a lot of the time for me, so that is why I use ragged right.)

User avatar
cgnieder
Site Moderator
Posts: 1988
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Wed Sep 03, 2014 10:45 am

LavaTyper wrote:1. How do I keep exam choice options together on the same page? (I did not ask how to condense all the options into one line; the actual exam will have short statements for response options and so I will need one choice per line.)

Interesting question. The usual idea would be to put it in a box since boxes never are broken at page boundaries. In LaTeX terms: put it in a {minipage}. This works but will separate the question from the choices:
  1. \question Which of the following is an example of the correct answer?
  2.  
  3. \noindent\begin{minipage}{\linewidth}
  4. \begin{choices}
  5. \CorrectChoice first answer
  6. \choice second answer
  7. \choice third answer
  8. \choice fourth answer
  9. \choice fifth answer
  10. \end{choices}
  11. \end{minipage}

I can't say if that suffices for you.

For keeping the question and the possible answers together the logical idea would be to wrap the {minipage} around both
  1. \noindent\begin{minipage}{\linewidth}
  2. \question Which of the following is an example of the correct answer?
  3.  
  4. \begin{choices}
  5. \CorrectChoice first answer
  6. \choice second answer
  7. \choice third answer
  8. \choice fourth answer
  9. \choice fifth answer
  10. \end{choices}
  11. \end{minipage}

but for reasons I don't yet understand this destroys the formatting (the list is then too close to the question).

LavaTyper wrote:(Also ragged2e doesn't work a lot of the time for me, so that is why I use ragged right.)

That's strange... loading ragged2e shouldn't change anything unless you use an option like \usepackage[document]{ragged2e}. However, whatever your problems with the package are IMHO it might be worth trying to solve them and use the package, anyway...

Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

User avatar
Johannes_B
Site Moderator
Posts: 3930
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Wed Sep 03, 2014 10:51 am

Sorry, i missread the question. That happens.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

LavaTyper
Posts: 68
Joined: Sat Feb 11, 2012 2:38 am

Postby LavaTyper » Thu Sep 04, 2014 2:49 pm

So, the exam class has no built-in option to keep choices together and I have to use a minipage for each question? That seems rather disappointing.

Well, what about this?

LavaTyper wrote:2. And how do I change the spacing between adjacent choices? (The space between A and B should be single spaced apart, and instead it is more than that, like, 1.5x the line spread.)


Any idea?

User avatar
Johannes_B
Site Moderator
Posts: 3930
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Thu Sep 04, 2014 3:45 pm

You can define your own environment and use this instead. It will safe you some work.

To the second thing a little qick and dirty hack:
  1. \documentclass[answers, 10pt]{exam}
  2. \usepackage[margin=0.75in]{geometry}
  3. \usepackage{lipsum}
  4. \usepackage{indentfirst}
  5. \usepackage{enumerate}
  6. \usepackage{etoolbox}
  7. \patchcmd{\choices}{\topsep=0pt}{%
  8. \topsep=0pt
  9. \itemsep=42pt}{}{}
  10.  
  11.  
  12. %\linespread{1}
  13.  
  14. \renewcommand{\rmdefault}{phv}
  15. \renewcommand{\sfdefault}{phv}
  16.  
  17. \begin{document}\pagestyle{empty}\raggedright
  18.  
  19. \lipsum[1] \lipsum[1] \lipsum[1] \lipsum[1] \lipsum[1] \lipsum[2]
  20.  
  21.  
  22. \begin{questions}
  23.  
  24. \begin{minipage}{\linewidth}
  25. \question Which of the following is an example of the correct answer?
  26.  
  27. \begin{choices}
  28. \CorrectChoice first answer
  29. \choice second answer
  30. \choice third answer
  31. \choice fourth answer
  32. \choice fifth answer
  33. \end{choices}
  34.  
  35. \end{minipage}
  36.  
  37. \begin{minipage}{\linewidth}
  38. \question Which of the following is an example of the correct answer?
  39.  
  40. \begin{choices}
  41. \CorrectChoice first answer
  42. \choice second answer
  43. \choice third answer % This should be kept together with the first two choices
  44. \choice fourth answer
  45. \choice fifth answer
  46. \end{choices}
  47.  
  48. \end{minipage}
  49.  
  50. \end{questions}
  51. \end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

LavaTyper
Posts: 68
Joined: Sat Feb 11, 2012 2:38 am

Postby LavaTyper » Mon Sep 08, 2014 11:32 pm

That is not satisfying.

I have given up on the exam class and have gone back to using basic enumerate.

Johannes, you mentioned that using \raggedright "gets you into trouble" - well then how do I take care of the options in {enumerate}?

Also, how do I get the list items (choices) to have the same alignment even if one option continues onto a second line?

  1. \documentclass[10pt]{article}
  2. \usepackage[showframe, margin=0.75in]{geometry}
  3. \usepackage{lipsum}
  4. \usepackage{indentfirst}
  5. \usepackage{enumerate}
  6. \usepackage{ragged2e}
  7.  
  8. \renewcommand{\rmdefault}{phv}
  9. \renewcommand{\sfdefault}{phv}
  10.  
  11.  
  12.  
  13. % LINE SPACING DEFINITIONS
  14.  
  15. \linespread{1}
  16. \def\titlelinespace{6pt}
  17. \def\itemindentadjustment{2em}
  18. \def\itemspaceadjustment{-0.25em}
  19.  
  20.  
  21. % QUESTION ENVIRONMENT
  22.  
  23. \newcounter{questionNumber}
  24. \setcounter{questionNumber}{0}
  25.  
  26. \newenvironment{question}{\noindent
  27. \begin{minipage}[t]{\textwidth}\vspace{0pt}
  28. \addtocounter{questionNumber}{1}\arabic{questionNumber}.}
  29. %
  30. {\end{minipage}}
  31.  
  32. \newenvironment{questionoptions}{
  33. \vspace{-1em}
  34. \begin{enumerate}[(a)]
  35. \setlength{\itemsep}{\itemspaceadjustment}
  36. \setlength{\itemindent}{\itemindentadjustment}
  37. }
  38. %
  39. {\vspace{1.3\baselineskip}\end{enumerate}}
  40.  
  41.  
  42.  
  43. % THE DOCUMENT
  44. \begin{document}\pagestyle{empty}
  45.  
  46. \lipsum[1]
  47.  
  48.  
  49.  
  50. \begin{question}
  51. I am testing a long question. Suppose that my students will be able to respond to it. Which of these will be the answer?
  52.  
  53. \begin{questionoptions}
  54. \item not this one
  55. \item definitely not this one
  56. \item don't even think about selecting this one
  57. \item well students will tend to select the long answer because it has all the details and looks undoubtedly sophisticated
  58. \item this is correct
  59. \end{questionoptions}
  60. \end{question}
  61.  
  62.  
  63. \begin{question}
  64. What goes here?
  65.  
  66. \begin{questionoptions}
  67. \item something
  68. \item something
  69. \item something
  70. \item something
  71. \item something
  72. \end{questionoptions}
  73. \end{question}
  74.  
  75.  
  76. \end{document}

User avatar
Johannes_B
Site Moderator
Posts: 3930
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Tue Sep 09, 2014 10:52 am

Package enumerate has its limitations. In the following code i used the more modern enumitem. The troubling raggedright was referring to the oneparchoices-environment. Both are incompatible with one another. There is no problem in the current example (at least i see none).

  1. \documentclass[10pt]{article}
  2. \usepackage[showframe, margin=0.75in]{geometry}
  3. \usepackage{lipsum}
  4. \usepackage{indentfirst}
  5. %\usepackage{enumerate}
  6. \usepackage{enumitem}
  7. \usepackage{ragged2e}
  8.  
  9. \renewcommand{\rmdefault}{phv}
  10. \renewcommand{\sfdefault}{phv}
  11.  
  12. \newlength{\titlelinespace}
  13. \setlength{\titlelinespace}{6pt}
  14. \newlength{\itemspaceadjustment}
  15. \setlength{\itemspaceadjustment}{-.25em}
  16. \newlength{\itemindentadjustment}
  17. \setlength{\itemindentadjustment}{2em}
  18.  
  19. \raggedright %<-
  20.  
  21. \newcounter{questionNumber}
  22.  
  23. \newenvironment{question}{\noindent
  24. \begin{minipage}[t]{\textwidth}\vspace{0pt}
  25. \refstepcounter{questionNumber}\arabic{questionNumber}.}
  26. %
  27. {\end{minipage}}
  28.  
  29. \newenvironment{questionoptions}[1][]{
  30. \vspace{-1em}
  31. \begin{enumerate}[itemsep=\itemspaceadjustment,leftmargin=*,labelindent=1em,label={(\alph*)},#1]
  32. %\setlength{\itemsep}{\itemspaceadjustment}
  33. %\setlength{\itemindent}{\itemindentadjustment}
  34. }
  35. %
  36. {\vspace{1.3\baselineskip}\end{enumerate}}
  37.  
  38. \begin{document}\pagestyle{empty}
  39. \lipsum[1]
  40.  
  41. \begin{question}
  42. I am testing a long question. Suppose that my students will be able to respond to it. Which of these will be the answer?
  43.  
  44. \begin{questionoptions}
  45. \item not this one
  46. \item definitely not this one
  47. \item don't even think about selecting this one
  48. \item well students will tend to select the long answer because it has all the details and looks undoubtedly sophisticated
  49. \item this is correct
  50. \end{questionoptions}
  51. \end{question}
  52.  
  53.  
  54. \begin{question}
  55. What goes here?
  56.  
  57. \begin{questionoptions}[noitemsep]
  58. \item something
  59. \item something
  60. \item something
  61. \item something
  62. \item something
  63. \end{questionoptions}
  64. \end{question}
  65. \end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

StarValkyrie
Posts: 89
Joined: Fri Jan 24, 2014 12:42 am

Postby StarValkyrie » Wed Sep 24, 2014 5:39 am

I have my exam.cls-based exam template in LyX set up to prevent page breaks between choices. My solution only works in LyX though because LyX is set up already so that you can automatically append a custom command (which I can't take credit for - I googled for it) to all instances of a particular environment - in this case, both "choice" and "correctchoice".

In Preamble, I put:
  1. \makeatletter
  2. \newcommand\mynobreakpar{\par\nobreak\@afterheading}
  3. \makeatother


Then using the LyX Layout's RightDelim option, I have it add ~\mynobreakpar at the end of each choice line. That keeps the choices together. To prevent a page break between those answers and the question they belong to, I then have LyX append \pagebreak[3] after each \end{choices}.

This solution might or might not be reproducible without LyX by copying the relevant sections of code from the exam.cls that pertain to choices, choice and correctchoice environments and then inserting the commands - you'd have to play around with it and see.


Return to “Document Classes”

Who is online

Users browsing this forum: No registered users and 2 guests