2

I want to create MCQ questions for my students. For that I have used the following code but I am facing difficulty (see comments at the end).

\documentclass[10pt,a4paper]{exam}
\usepackage{amsmath,amsthm,amsfonts,amssymb}
\usepackage[left=1in,right=0.5in,top=0.75in,bottom=0.75in]{geometry}
\usepackage{ifthen}

% Accumulate the answers. Unmodified from Phil Hirschorn's answer
% http://tex.stackexchange.com/questions/15350/showing-solutions-of-the-questions-separately/15353
\newbox\allanswers
\setbox\allanswers=\vbox{}

\newenvironment{answer}
{%
    \global\setbox\allanswers=\vbox\bgroup
    \unvbox\allanswers
}%
{%
    \bigbreak
    \egroup
}

\newcommand{\showallanswers}{\par\unvbox\allanswers}
% End Phil's answer


% Is there a better way?
\newcommand*{\getanswer}[5]{%
    \ifthenelse{\equal{#5}{a}}
    {\begin{answer}\thequestion. (a)~#1\end{answer}}
    {\ifthenelse{\equal{#5}{b}}
        {\begin{answer}\thequestion. (b)~#2\end{answer}}
        {\ifthenelse{\equal{#5}{c}}
            {\begin{answer}\thequestion. (c)~#3\end{answer}}
            {\ifthenelse{\equal{#5}{d}}
                {\begin{answer}\thequestion. (d)~#4\end{answer}}
                {\begin{answer}\textbf{\thequestion. (#5)~Invalid answer choice.}\end{answer}}}}}
}

\setlength\parindent{0pt}
%usage \choice{ }{ }{ }{ }
%(A)(B)(C)(D)
\newcommand{\fourch}[5]{
    \par
    \begin{tabular}{*{4}{@{}p{0.23\textwidth}}}
        (a)~#1 & (b)~#2 & (c)~#3 & (d)~#4
    \end{tabular}
    \getanswer{#1}{#2}{#3}{#4}{#5}
}

%(A)(B)
%(C)(D)
\newcommand{\twoch}[5]{
    \par
    \begin{tabular}{*{2}{@{}p{0.46\textwidth}}}
        (a)~#1 & (b)~#2
    \end{tabular}
    \par
    \begin{tabular}{*{2}{@{}p{0.46\textwidth}}}
        (c)~#3 & (d)~#4
    \end{tabular}
    \getanswer{#1}{#2}{#3}{#4}{#5}
}

%(A)
%(B)
%(C)
%(D)
\newcommand{\onech}[5]{
    \par
    (a)~#1 \par (b)~#2 \par (c)~#3 \par (d)~#4
    \getanswer{#1}{#2}{#3}{#4}{#5}
}

\newlength\widthcha
\newlength\widthchb
\newlength\widthchc
\newlength\widthchd
\newlength\widthch
\newlength\tabmaxwidth

\setlength\tabmaxwidth{0.96\textwidth}
\newlength\fourthtabwidth
\setlength\fourthtabwidth{0.25\textwidth}
\newlength\halftabwidth
\setlength\halftabwidth{0.5\textwidth}

\newcommand{\choice}[5]{%
\settowidth\widthcha{AM.#1}\setlength{\widthch}{\widthcha}%
\settowidth\widthchb{BM.#2}%
\ifdim\widthch<\widthchb\relax\setlength{\widthch}{\widthchb}\fi%
    \settowidth\widthchb{CM.#3}%
\ifdim\widthch<\widthchb\relax\setlength{\widthch}{\widthchb}\fi%
    \settowidth\widthchb{DM.#4}%
\ifdim\widthch<\widthchb\relax\setlength{\widthch}{\widthchb}\fi%
\ifdim\widthch<\fourthtabwidth
    \fourch{#1}{#2}{#3}{#4}{#5}
\else\ifdim\widthch<\halftabwidth
\ifdim\widthch>\fourthtabwidth
    \twoch{#1}{#2}{#3}{#4}{#5}
\else
    \onech{#1}{#2}{#3}{#4}{#5}
\fi\fi\fi}

\newcommand{\uline}[1]{\rule[0pt]{#1}{0.4pt}}

\begin{document}
\begin{center}
{\Large MCQ's Test}
\end{center}
\begin{questions}

\question Open pairs are those which have
\choice{point or line contact between the two elements when in motion}{surface contact between the two elements when in motion}{elements of pairs not held together mechanically}{two elements that permit relative motion}{c}

\question Whitworth quick return mechanism is obtained by inversion of :
\choice{slider crank mechanism}{kinematic chain}{five link mechanism}{roller cam mechanism}{a}

\end{questions}

%\newpage  %Uncomment to put on new age
\bigskip
Answer Key:
\bigskip  
\showallanswers % Phil Hirschorn

\end{document}

I am getting output like this

enter image description here

As you can see, there are no options (answers) for question no. 56 and options for question no.63 should be 2 per row instead of 4 per row.

0

1 Answer 1

5

Note the changes in \fourthtabwidthand \halftabwidth to match the \parbox widths used. Also removed an \ifdim from the main test.

\documentclass[10pt,a4paper]{exam}
\usepackage{amsmath,amsthm,amsfonts,amssymb}
\usepackage[left=1in,right=0.5in,top=0.75in,bottom=0.75in]{geometry}
\usepackage{ifthen}

% Accumulate the answers. Unmodified from Phil Hirschorn's answer
% http://tex.stackexchange.com/questions/15350/showing-solutions-of-the-questions-separately/15353
\newbox\allanswers
\setbox\allanswers=\vbox{}

\newenvironment{answer}
{%
    \global\setbox\allanswers=\vbox\bgroup
    \unvbox\allanswers
}%
{%
    \bigbreak
    \egroup
}

\newcommand{\showallanswers}{\par\unvbox\allanswers}
% End Phil's answer


% Is there a better way?
\newcommand*{\getanswer}[5]{%
    \ifthenelse{\equal{#5}{a}}
    {\begin{answer}\thequestion. (a)~#1\end{answer}}
    {\ifthenelse{\equal{#5}{b}}
        {\begin{answer}\thequestion. (b)~#2\end{answer}}
        {\ifthenelse{\equal{#5}{c}}
            {\begin{answer}\thequestion. (c)~#3\end{answer}}
            {\ifthenelse{\equal{#5}{d}}
                {\begin{answer}\thequestion. (d)~#4\end{answer}}
                {\begin{answer}\textbf{\thequestion. (#5)~Invalid answer choice.}\end{answer}}}}}
}

\setlength\parindent{0pt}
%usage \choice{ }{ }{ }{ }
%(A)(B)(C)(D)
\newcommand{\fourch}[5]{
    \par
    \begin{tabular}{*{4}{@{}p{0.23\textwidth}}}
        (a)~#1 & (b)~#2 & (c)~#3 & (d)~#4
    \end{tabular}
    \getanswer{#1}{#2}{#3}{#4}{#5}
}

%(A)(B)
%(C)(D)
\newcommand{\twoch}[5]{
    \par
    \begin{tabular}{*{2}{@{}p{0.46\textwidth}}}
        (a)~#1 & (b)~#2
    \end{tabular}
    \par
    \begin{tabular}{*{2}{@{}p{0.46\textwidth}}}
        (c)~#3 & (d)~#4
    \end{tabular}
    \getanswer{#1}{#2}{#3}{#4}{#5}
}

%(A)
%(B)
%(C)
%(D)
\newcommand{\onech}[5]{
    \par
    (a)~#1 \par (b)~#2 \par (c)~#3 \par (d)~#4
    \getanswer{#1}{#2}{#3}{#4}{#5}
}

\newlength\widthcha
\newlength\widthchb
\newlength\widthchc
\newlength\widthchd
\newlength\widthch
\newlength\tabmaxwidth

\setlength\tabmaxwidth{0.96\textwidth}
\newlength\fourthtabwidth
\setlength\fourthtabwidth{0.23\textwidth}
\newlength\halftabwidth
\setlength\halftabwidth{0.46\textwidth}

\newcommand{\choice}[5]{%
\settowidth\widthcha{AM.#1}\setlength{\widthch}{\widthcha}%
\settowidth\widthchb{BM.#2}%
\ifdim\widthch<\widthchb\relax\setlength{\widthch}{\widthchb}\fi%
    \settowidth\widthchb{CM.#3}%
\ifdim\widthch<\widthchb\relax\setlength{\widthch}{\widthchb}\fi%
    \settowidth\widthchb{DM.#4}%
\ifdim\widthch<\widthchb\relax\setlength{\widthch}{\widthchb}\fi%
\ifdim\widthch<\fourthtabwidth
    \fourch{#1}{#2}{#3}{#4}{#5}
\else\ifdim\widthch<\halftabwidth
    \twoch{#1}{#2}{#3}{#4}{#5}
\else
    \onech{#1}{#2}{#3}{#4}{#5}
\fi\fi}

\newcommand{\uline}[1]{\rule[0pt]{#1}{0.4pt}}

\begin{document}
\begin{center}
{\Large MCQ's Test}
\end{center}
\begin{questions}

\question Open pairs are those which have
\choice{point or line contact between the two elements when in motion}{surface contact between the two elements when in motion}{elements of pairs not held together mechanically}{two elements that permit relative motion}{c}

\question Whitworth quick return mechanism is obtained by inversion of :
\choice{slider crank mechanism}{kinematic chain}{five link mechanism}{roller cam mechanism}{a}

\end{questions}

%\newpage  %Uncomment to put on new age
\bigskip
Answer Key:
\bigskip  
\showallanswers % Phil Hirschorn

\end{document}

spacing choices

2
  • That was awesome sir. These new codes solved my problem. Thanks for help. Commented Jan 9, 2016 at 3:15
  • Appreciated, although you might want to click the accept button as well. Commented Jan 9, 2016 at 16:33

You must log in to answer this question.

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