7

I am creating multiple choice questions using exam class however I faced two issues: 1). Want to have two columns choices but it displays by default as A, C, B, D, and 2). Figures in choices are bottom aligned instead of top alignment. Any suggestion? Below is my MWE:

\documentclass{exam}
\usepackage{multicol}
\usepackage{graphicx}
\begin{document}
    \begin{questions}
        \question
        \begin{minipage}[t]{0.65\linewidth}
        Consider the network graph shown in the figure, which of the following is NOT a \textit{tree} of this graph?
            \begin{multicols}{2}
                \begin{choices}
                    \choice
                        \includegraphics[scale=0.2]{example-image-a}
                    \choice
                        \includegraphics[scale=0.2]{example-image-b}
                    \choice
                        \includegraphics[scale=0.2]{example-image-c}
                    \CorrectChoice
                        \includegraphics[scale=0.2]{example-image-a}
                \end{choices}
            \end{multicols}
    \end{minipage}
    \hspace{2em}%
    \begin{minipage}[t]{0.35\linewidth}
        \vspace{-\baselineskip}
        \includegraphics[scale=0.4]{example-image-a}
    \end{minipage}
\end{questions}
\end{document}

Output after running the above code gives as: enter image description here

However, I wish to have something like this: enter image description here

2 Answers 2

6

Here's one option:

\documentclass{exam}
\usepackage{multicol}
\usepackage{graphicx}
\usepackage{adjustbox}

\begin{document}
    \begin{questions}
        \question
        \begin{minipage}[t]{0.65\linewidth}
        Consider the network graph shown in the figure, which of the following is NOT a \textit{tree} of this graph?
                \begin{choices}
            \begin{multicols}{2}
                    \choice
                        \adjustbox{valign=t}{\includegraphics[scale=0.2]{example-image-a}}
                    \choice
                        \adjustbox{valign=t}{\includegraphics[scale=0.2]{example-image-b}}
            \end{multicols}\par
            \begin{multicols}{2}
                    \choice
                        \adjustbox{valign=t}{\includegraphics[scale=0.2]{example-image-c}}
                    \CorrectChoice
                        \adjustbox{valign=t}{\includegraphics[scale=0.2]{example-image-a}}
            \end{multicols}
                \end{choices}
    \end{minipage}\hfill%
        \begin{adjustbox}{minipage={0.30\linewidth},valign=t}
        \includegraphics[width=\linewidth]{example-image-a}
    \end{adjustbox}
\end{questions}
\end{document}

enter image description here

I used the adjustbox package to get the desired top alignment. For the numbering schema I used two multicols environments instead of just one.

I also changed the scale option for \includegraphics to widthand changed some spaces to prevent overfull\hbox`es.

6
  • this multicols environment gives "! Extra }, or forgotten \endgroup" when used with \printanswers command. Any suggestion on what to avoid this situation please.
    – Khaaba
    Commented Oct 11, 2015 at 2:09
  • @Khaaba We need to see an example of the actual code that you are using. Since this is not directly related to the original question, the best thing to do is to open a fresh new question with this new issue. Please provide a MWE there allowing us to reproduce the error mentioned. Commented Oct 11, 2015 at 2:13
  • I have added actual code I am using with your last suggestion. Usually when I used \printanswers command, the \CorrectChoice should make the partchoice boldface indicating that it is the correct answer out of choices given therein.
    – Khaaba
    Commented Oct 11, 2015 at 2:25
  • @Khaaba No. I didn'¿t mean add the code in the original question. I meant open a fresh new question. I think it's better. Commented Oct 11, 2015 at 2:27
  • @Khaaba The other answer doesn't have problems with \printanswers. Commented Oct 11, 2015 at 2:46
3

egreg has answers for almost every thing. For your first issue, you may use this solution give by egereg:

%%------------------egreg's code starts-------------
\usepackage{environ,xparse}
\ExplSyntaxOn
\cs_undefine:N \choices
\cs_undefine:N \endchoices
\NewEnviron{choices}{%
  \par\nopagebreak
  \harish_dochoices:V \BODY
  \par
}
\tl_new:N \l_harish_choices_tl
\seq_new:N \l_harish_choices_seq
\seq_new:N \l_harish_choices_final_seq

\cs_new_protected:Npn \harish_dochoices:n #1
 {
  \setcounter{choice}{0}
  \tl_set:Nn \l_harish_choices_tl { #1 }
  \tl_replace_once:Nnn \l_harish_choices_tl { \CorrectChoice } { \choice* }
  \seq_set_split:NnV \l_harish_choices_seq { \choice } \l_harish_choices_tl
  \seq_pop_left:NN \l_harish_choices_seq \l_tmpa_tl
  \seq_set_map:NNn \l_harish_choices_final_seq \l_harish_choices_seq
   { \exp_not:n { \makechoice ##1 } }
  %\seq_map_inline:Nn \l_harish_choices_final_seq { \makebox[.5\linewidth][l]{##1}\hspace{0pt plus .3\linewidth} }
% }
    \seq_map_inline:Nn \l_harish_choices_final_seq
   {
    \makebox[.5\linewidth][l]{
      \parbox[t]{.45\linewidth}{\raggedright \hangindent1.5em ##1\strut}
    }\hspace{0pt plus .3\linewidth}
   }
 }
\cs_generate_variant:Nn \harish_dochoices:n { V }
\ExplSyntaxOff
\makeatletter
    \NewDocumentCommand{\makechoice}{s}
 {%
  \stepcounter{choice}
  \makebox[1.2em][l]{\IfBooleanT{#1}{\ifprintanswers\CorrectChoice@Emphasis\fi}\Alph{choice}.}
 }
\makeatother
\CorrectChoiceEmphasis{\bfseries\boldmath}

%%------------------egreg's code ends-------------

He helped me once upon a time when I was a victim of similar requirement while I was doing some question paper.

For the second issue, you can load the adjustbox package (as Gonzalo did) but with export option. This will make keys like valign etc available for \includegraphics command. So you have to use

\includegraphics[scale=0.2,valign=t]{example-image-a}

Remember that in this case graphicx is loaded by adjustbox.

Here is the full code:

\documentclass{exam}
\usepackage[export]{adjustbox}

%%------------------egreg's code starts-------------
\usepackage{environ,xparse}
\ExplSyntaxOn
\cs_undefine:N \choices
\cs_undefine:N \endchoices
\NewEnviron{choices}{%
  \par\nopagebreak
  \harish_dochoices:V \BODY
  \par
}
\tl_new:N \l_harish_choices_tl
\seq_new:N \l_harish_choices_seq
\seq_new:N \l_harish_choices_final_seq

\cs_new_protected:Npn \harish_dochoices:n #1
 {
  \setcounter{choice}{0}
  \tl_set:Nn \l_harish_choices_tl { #1 }
  \tl_replace_once:Nnn \l_harish_choices_tl { \CorrectChoice } { \choice* }
  \seq_set_split:NnV \l_harish_choices_seq { \choice } \l_harish_choices_tl
  \seq_pop_left:NN \l_harish_choices_seq \l_tmpa_tl
  \seq_set_map:NNn \l_harish_choices_final_seq \l_harish_choices_seq
   { \exp_not:n { \makechoice ##1 } }
  %\seq_map_inline:Nn \l_harish_choices_final_seq { \makebox[.5\linewidth][l]{##1}\hspace{0pt plus .3\linewidth} }
% }
    \seq_map_inline:Nn \l_harish_choices_final_seq
   {
    \makebox[.5\linewidth][l]{
      \parbox[t]{.45\linewidth}{\raggedright \hangindent1.5em ##1\strut}
    }\hspace{0pt plus .3\linewidth}
   }
 }
\cs_generate_variant:Nn \harish_dochoices:n { V }
\ExplSyntaxOff
\makeatletter
    \NewDocumentCommand{\makechoice}{s}
 {%
  \stepcounter{choice}
  \makebox[1.2em][l]{\IfBooleanT{#1}{\ifprintanswers\CorrectChoice@Emphasis\fi}\Alph{choice}.}
 }
\makeatother
\CorrectChoiceEmphasis{\bfseries\boldmath}

%%------------------egreg's code ends-------------

\begin{document}
    \begin{questions}
        \question
        \begin{minipage}[t]{0.65\linewidth}
        Consider the network graph shown in the figure, which of the following is NOT a \textit{tree} of this graph?
            %\begin{multicols}{2}
                \begin{choices}
                    \choice
                        \includegraphics[scale=0.2,valign=t]{example-image-a}
                    \choice
                        \includegraphics[scale=0.2,valign=t]{example-image-b}
                    \choice
                        \includegraphics[scale=0.2,valign=t]{example-image-c}
                    \CorrectChoice
                        \includegraphics[scale=0.2,valign=t]{example-image-a}
                \end{choices}
            %\end{multicols}
    \end{minipage}
    \hfill%
    \begin{minipage}[t]{0.3\linewidth}
        \vspace{-\baselineskip}
        \includegraphics[width=\linewidth]{example-image-a}
    \end{minipage}
\end{questions}
\end{document}

enter image description here

If you are not struck with exam class, I suggest you to use exsheets class which has a native support for this kind of lists.

3
  • The above code gives the following error when run using with TexWorks[pdflatex] on MacTeX 2015: ! Undefined control sequence. \harish_dochoices:n ...hoice *}\seq_set_split:NnV \l_harish_choices_seq {\ch... l.64 \end{choices} ? Have I missed anything?
    – Khaaba
    Commented Oct 11, 2015 at 3:08
  • 1
    @Khaaba I don't have mactex. Sorry. The code works with latest miktex for me.
    – user11232
    Commented Oct 11, 2015 at 3:10
  • Yes, you are right. It works on MikTeX. Just tested it on my chamber pc. Figure choices are too close to each other vertically though. Here is another good option given last evening by touhami at [![tex.stackexchange.com/questions/272278/…
    – Khaaba
    Commented Oct 12, 2015 at 11:27

You must log in to answer this question.

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