我想为我的学生创建 MCQ 问题。为此,我使用了以下代码,但遇到了困难(请参阅最后的评论)。
\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}
我得到这样的输出
如您所见,问题 56 没有选项(答案),问题 63 的选项应该是每行 2 个,而不是每行 4 个。
答案1
\fourthtabwidth
注意和的变化\halftabwidth
以匹配使用的宽度。还从主测试中\parbox
删除了。\ifdim
\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}