# Centering Answers in Exam Class

MWE

\documentclass[11pt,a4paper]{exam}

\usepackage{multicol}

\begin{document}
\begin{multicols}{2}
\begin{questions}

\question FIRST QUESTION

\begin{oneparchoices}
\choice ABC
\choice DEF
\CorrectChoice GHI
\choice JKL
\choice MNO
\end{oneparchoices}

\question SECOND QUESTION

\begin{oneparchoices}
\choice ABCDEF
\choice GHIJKL
\CorrectChoice GHI
\choice JKL
\choice MNO
\end{oneparchoices}

\end{questions}

\vfill
\columnbreak

EXAM FINISH

\end{multicols}
\end{document}


That I want centering in last line E answer or D and E answers. Is it possible?

...@JasperHabicht Solution...

When options are short, something like this happens...

Can it be left-aligned (in original package) or distribute text?

• Sorry, I currently cannot think of a feasible solution of this second problem. You would have to test the length of the questions block in order to check if it is more than one line. If yes keep it left-aligned, if not set it to justify-centering. But I can't think of an easy way to do this. Also, the exam package seems to be a bit unstable which makes it even more difficult to come up with possible solutions. Maybe you really stick to the old environment and use another one with the code I proposed for the longer questions. Maybe someone else comes up with a better solution. :/ – Jasper Habicht Sep 6 '17 at 4:58
• @JasperHabicht, you're welcome. So far, your help and the information you wrote made me happy. I expect you to be interested in my other questions. – Özgür Sep 6 '17 at 5:09
• you are lucky. I just found a possible solution which I added to my answer below. – Jasper Habicht Sep 6 '17 at 5:16
• @JasperHabicht, Yes I am lucky indeed. [Solved] – Özgür Sep 6 '17 at 5:24

One solution would be to use this approach to center the last line in the paragraph while leaving the rest justified. So, you could define a new environment in your document preamble and use it instead of the old one, like this:

\newenvironment{oneparchoicescentering}{
\begingroup
\leftskip=0mm plus .5fil%
\rightskip=0mm plus -.5fil%
\parfillskip=0mm plus 1fil\relax
\begin{oneparchoices}
}{
\end{oneparchoices}
\par
\endgroup
}


The \par is needed in order to make the paragraph complete in a way. I tried without, but this doesn’t seem to work. I guess, it is needed to change from horizontal mode back into vertical in order to place the boxes properly.

The result would be:

Is this want you want to achieve?

By the way, you should use a4paper instead of paper=a4 as option in your document class. Also, I think that using only capital letters makes the text a bit hard to read ...

Edit: If you want to overwrite the oneparchoices environment in order to be able to keep the syntax, you could instead put the following in your preamble (the following at the same time being a MWE):

\documentclass[11pt,a4paper]{exam}
\usepackage{multicol}

\let\oldoneparchoices\oneparchoices
\let\oldendoneparchoices\endoneparchoices
\renewenvironment{oneparchoices}{
\begingroup
\leftskip=0mm plus .5fil%
\rightskip=0mm plus -.5fil%
\parfillskip=0mm plus 1fil\relax
\begingroup\oldoneparchoices
}{
\oldendoneparchoices\endgroup
\par
\endgroup
}

\begin{document}

\begin{multicols*}{2}
\begin{questions}

\question First Question

\begin{oneparchoices}
\choice ABC
\choice DEF
\CorrectChoice GHI
\choice JKL
\choice MNO
\end{oneparchoices}

\end{questions}
\end{multicols*}

\end{document}


In order to prevent single-line answers to be also centered (although I personally would not mix centered and ragged right text), the following code could be used making use of the package environ:

\documentclass[11pt,a4paper]{exam}
\usepackage{multicol}

\let\oldoneparchoices\oneparchoices
\let\oldendoneparchoices\endoneparchoices
\let\oneparchoices\relax         % We have to unset these to make \NewEnviron work
\let\endoneparchoices\relax
\newlength{\choiceslen}          % Creating new length for later test
\NewEnviron{oneparchoices}{
\begingroup
\settowidth{\choiceslen}{%      % Setting the above created length to the length of all answers
\begingroup\oldoneparchoices
\BODY
\oldendoneparchoices\endgroup%
}
\ifdim\choiceslen>\linewidth    % Test if length exceeds one line, if yes center last line
\leftskip=0mm plus .5fil%
\rightskip=0mm plus -.5fil%
\parfillskip=0mm plus 1fil\relax
\fi
\begingroup\oldoneparchoices
\BODY
\oldendoneparchoices\endgroup
\par
\endgroup
}

\begin{document}

\begin{multicols*}{2}
\begin{questions}

\question First Question

\begin{oneparchoices}
\choice ABC
\choice DEF
\CorrectChoice GHI
\choice JKL
\choice MNO
\end{oneparchoices}

\question Second Question

\begin{oneparchoices}
\choice ABC
\choice DEF
\CorrectChoice GHI
\end{oneparchoices}

\end{questions}
\end{multicols*}

\end{document}


I hope this works as it should. Result would be:

• Your suggestions are important to me. I will check your code. – Özgür Aug 31 '17 at 19:46
• @Julian_W: MWE added. I used the multicols* environment here only to show the desired effect. The code should also work in a single-column environment or with the unstarred version of the environment. – Jasper Habicht Sep 1 '17 at 12:04
• @Özgür: You should replace \begingroup\oldoneparchoices in line 13 (but not in line 22!) with \begingroup\renewcommand{\Cchoice}{\CorrectChoice}\oldoneparchoices. The reason why everything is counted twice is, that I told LaTeX to typeset the answers internally first in order to know whether they are too long for one line. So, \Cchoice and hence the answer counter is also called twice. With the above code, \Cchoice is reset to do only the formatting, but to not increase the answer counter, when LaTeX typesets internally. Upon real typesetting, \Cchoice will still call the counter. – Jasper Habicht Jan 21 '18 at 20:51