0
\documentclass[12pt,a4paper]{exam}
\usepackage{amsmath,amsthm,amsfonts,amssymb,dsfont}
\usepackage{ifthen}
\usepackage[margin = 0.8in]{geometry}
\usepackage{gensymb}
\newcommand{\ttt}{\textbf{- T}}
\newcommand{\fff}{\textbf{- F}}
\newcommand{\fbb}[1]{\textbf{\underline{#1}}}

% Accumulate the answers. Unmodified from Phil Hirschorn's answer
% https://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%

% These if statements were bypassing the \onech option.
% \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}

% Allows for the \onech option.
\ifdim\widthch>\halftabwidth
    \onech{#1}{#2}{#3}{#4}{#5}
\else\ifdim\widthch<\halftabwidth
\ifdim\widthch>\fourthtabwidth
    \twoch{#1}{#2}{#3}{#4}{#5}
\else
    \fourch{#1}{#2}{#3}{#4}{#5}
\fi\fi\fi}



\begin{document}
\begin{questions}
\question For airborne sounds, the reference sound pressure taken to measure the sound pressure level is (\textbf{1991})
\choice{$2\:X\:10^3\:N/m^2$}{$2\:X\:10^{-3}\:N/m^2$}{$2\:X\:10^5\:N/m^2$}{$2\:X\:10^{-5}\:N/m^2$}{d}
\question In a force-feed lubrication system adopted in the tractor engine, the type of pump generally used is
\choice{Centrifugal pump}{Gear pump}{Rotary vane pump}{Plunger pump}{b}
\question The type of bearing used to support the disc on a standard disc plough is
\choice{Bush bearing}{Plain roller bearing}{Tapered roller bearing}{Ball bearing}{c}


\end{questions}

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



\end{document}
cgnieder
  • 66,645

1 Answers1

2

The following comes without warranties:

\documentclass[12pt,a4paper]{exam}
\usepackage[dvipsnames]{xcolor}
\usepackage[colorlinks=true]{hyperref}
\usepackage{amsmath,amsthm,amsfonts,amssymb,dsfont}
\usepackage{ifthen}
\usepackage[margin = 0.8in]{geometry}
\usepackage{gensymb}
\newcommand{\ttt}{\textbf{- T}}
\newcommand{\fff}{\textbf{- F}}
\newcommand{\fbb}[1]{\textbf{\underline{#1}}}

% Accumulate the answers. Unmodified from Phil Hirschorn's answer % https://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

\makeatletter \definecolor{getanscolor}{named}{ForestGreen} \newcommand\linktoanswer[1]{% \begingroup \hypersetup{linkcolor=getanscolor, linkbordercolor=getanscolor}% \hyperref[{question@\arabic{question}Ans}]{#1}% \endgroup } \newcommand\linktoquestion[1]{% \begingroup \hypersetup{linkcolor=getanscolor, linkbordercolor=getanscolor}% \hyperref[question@\arabic{question}]{#1}% \endgroup }% \def\questionlabel{\linktoanswer{\thequestion.}} % Is there a better way? \newcommand*{\getanswer}[5]{% \ifthenelse{\equal{#5}{a}} {\begin{answer}\phantomsection\label{question@\arabic{question}Ans}% \linktoquestion{\thequestion. (a)~#1}\end{answer}} {\ifthenelse{\equal{#5}{b}} {\begin{answer}\phantomsection\label{question@\arabic{question}Ans}% \linktoquestion{\thequestion. (b)~#2}\end{answer}} {\ifthenelse{\equal{#5}{c}} {\begin{answer}\phantomsection\label{question@\arabic{question}Ans}% \linktoquestion{\thequestion. (c)~#3}\end{answer}} {\ifthenelse{\equal{#5}{d}} {\begin{answer}\phantomsection\label{question@\arabic{question}Ans}% \linktoquestion{\thequestion. (d)~#4}\end{answer}} {\begin{answer}\textbf{\thequestion. (#5)~Invalid answer choice.}\end{answer}}}}} } \makeatother

\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%

% These if statements were bypassing the \onech option. % \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}

% Allows for the \onech option. \ifdim\widthch>\halftabwidth \onech{#1}{#2}{#3}{#4}{#5} \else\ifdim\widthch<\halftabwidth \ifdim\widthch>\fourthtabwidth \twoch{#1}{#2}{#3}{#4}{#5} \else \fourch{#1}{#2}{#3}{#4}{#5} \fi\fi\fi}

\begin{document} \begin{questions} \question \linktoanswer{For airborne sounds, the reference sound pressure taken to measure the sound pressure level is (\textbf{1991})} \choice{$2:X:10^3:N/m^2$}{$2:X:10^{-3}:N/m^2$}{$2:X:10^5:N/m^2$}{$2:X:10^{-5}:N/m^2$}{d} \question \linktoanswer{In a force-feed lubrication system adopted in the tractor engine, the type of pump generally used is} \choice{Centrifugal pump}{Gear pump}{Rotary vane pump}{Plunger pump}{b} \question \linktoanswer{The type of bearing used to support the disc on a standard disc plough is} \choice{Bush bearing}{Plain roller bearing}{Tapered roller bearing}{Ball bearing}{c}

\end{questions}

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

\end{document}

enter image description here

enter image description here

Ulrich Diez
  • 28,770