\documentclass{exam}
\begin{document}
\begin{questions}
\question Sample Question 1
\begin{choices}
\choice Option1
\choice Option2
\choice Option3
\correctchoice Option4
\end{choices}
\question Sample Question 2
\begin{choices}
\choice Option1
\choice Option2
\correctchoice Option3
\choice Option4
\end{choices}
\end{questions}
\end{document}
问题是在格式的所有问题末尾生成答案
1.Ans:(D)
2.Ans:(C)
等等。这种问题已经被问过几次了,但到目前为止还没有令人满意的答案。如果有人能对此做出贡献,那就太好了。任何帮助都将不胜感激。
答案1
这个答案描述了两种解决方案。第一种解决方案将正确答案的数量/选项与答案一起输出,第二种解决方案仅显示数字(并在一行中收集几个正确选项)。
列出正确答案及其编号
如下所示标记正确答案的文本(或者更确切地说,应该显示在答案列表中的部分)。
\CorrectAnswer{... answer ...}
此命令将答案与当前问题和选项编号一起存储。您必须像
\begin{choices}
\choice Option1
\choice Option2
\choice Option3
\correctchoice \CorrectAnswer{Option4} % before and after \CorrectAnswer{} there may be other text which will appear only here
\end{choices}
在环境之后的某个地方questions
,可以通过以下方式打印答案
\printAnswers
每个答案的格式由
\printAnswer{question number}{choice number}{answer}
您必须在序言中添加以下定义。
\documentclass{exam}
\newcommand\Answers{} % Accumulates triples {{question number}{choice number}{answer}}
\newcommand\CorrectAnswer[1]{% {answer} % adds new triple to \Answers
\xdef\Answers{\Answers{{\arabic{question}}{\Alph{choice}}{#1}}}%
#1
}
\newcommand\printAnswer[3]{% {question number}{choice number}{answer} % format answer
\par\noindent #1. Ans: (#2) #3\par
}
\newcommand\printAnswers{% do \printAnswer for every answer in \Answers
\expandafter\printAnswersX\Answers{}%
}
\newcommand\printAnswersX[1]{% {{qu. number}{ch. number}{answer}} % auxilary command implementing loop
\def\tmp{#1}%
\ifx\tmp\empty
\let\tmp\relax
\else
\def\tmp{\printAnswer#1\printAnswersX}
\fi
\tmp
}
\documentclass{exam}
\newcommand\Answers{} % Accumulates triples {{question number}{choice number}{answer}}
\newcommand\CorrectAnswer[1]{% {answer} % adds new triple to \Answers
\xdef\Answers{\Answers{{\arabic{question}}{\Alph{choice}}{#1}}}%
#1
}
\newcommand\printAnswer[3]{% {question number}{choice number}{answer} % format answer
\par\noindent #1. Ans: (#2) #3\par
}
\newcommand\printAnswers{% do \printAnswer for every answer in \Answers
\expandafter\printAnswersX\Answers{}%
}
\newcommand\printAnswersX[1]{% {{qu. number}{ch. number}{answer}} % auxilary command implementing loop
\def\tmp{#1}%
\ifx\tmp\empty
\let\tmp\relax
\else
\def\tmp{\printAnswer#1\printAnswersX}
\fi
\tmp
}
\begin{document}
\begin{questions}
\question Sample Question 1
\begin{choices}
\choice Option1
\choice Option2
\choice Option3
\correctchoice \CorrectAnswer{Option4}
\end{choices}
\question Sample Question 2
\begin{oneparchoices}
\choice Option1
\choice Option2
\correctchoice \CorrectAnswer{Option3}
\choice Option4
\end{oneparchoices}
\end{questions}
\printAnswers
\end{document}
仅列出选择
将以下几行添加到你的序言中:
\documentclass{exam}
\usepackage{xpatch}
\makeatletter
\xpatchcmd\choices{\fi\do@choice@pageinfo}{\fi\CorrectAnswer\do@choice@pageinfo}{}{}
\xpatchcmd\oneparchoices{\fi\ifnum}{\fi\CorrectAnswer\ifnum}{}{}
\makeatother
\newcommand\Answers{} % Accumulates pairs {{question number}{choice number}}
\newcommand\CorrectAnswer{% {answer} % adds new pair to \Answers
\xdef\Answers{\Answers{{\arabic{question}}{\Alph{choice}}}}%
}
\newcommand\printAnswer[2]{% {question number}{choice number}{answer} % format answer
\ifnum#1=\lastQuestion
, (#2)% additional correct answer
\else
\par\noindent #1. Ans: (#2)% First correct answer of question
\fi
\def\lastQuestion{#1}%
}
\newcommand\printAnswers{% do \printAnswer for every answer in \Answers
\def\lastQuestion{0}%
\expandafter\printAnswersX\Answers{}%
\par
}
\newcommand\printAnswersX[1]{% {{qu. number}{ch. number}} % auxilary command implementing loop
\def\tmp{#1}%
\ifx\tmp\empty
\let\tmp\relax
\else
\def\tmp{\printAnswer#1\printAnswersX}%
\fi
\tmp
}
在文档末尾,该命令\printAnswers
将生成答案列表。
\documentclass{exam}
\usepackage{xpatch}
\makeatletter
\xpatchcmd\choices{\fi\do@choice@pageinfo}{\fi\CorrectAnswer\do@choice@pageinfo}{}{}
\xpatchcmd\oneparchoices{\fi\ifnum}{\fi\CorrectAnswer\ifnum}{}{}
\makeatother
\newcommand\Answers{} % Accumulates pairs {{question number}{choice number}}
\newcommand\CorrectAnswer{% {answer} % adds new pair to \Answers
\xdef\Answers{\Answers{{\arabic{question}}{\Alph{choice}}}}%
}
\newcommand\printAnswer[2]{% {question number}{choice number}{answer} % format answer
\ifnum#1=\lastQuestion
, (#2)% additional correct answer
\else
\par\noindent #1. Ans: (#2)% First correct answer of question
\fi
\def\lastQuestion{#1}%
}
\newcommand\printAnswers{% do \printAnswer for every answer in \Answers
\def\lastQuestion{0}%
\expandafter\printAnswersX\Answers{}%
\par
}
\newcommand\printAnswersX[1]{% {{qu. number}{ch. number}} % auxilary command implementing loop
\def\tmp{#1}%
\ifx\tmp\empty
\let\tmp\relax
\else
\def\tmp{\printAnswer#1\printAnswersX}%
\fi
\tmp
}
\begin{document}
\begin{questions}
\question Sample Question 1
\begin{choices}
\choice Option1
\choice Option2
\choice Option3
\correctchoice Option4
\end{choices}
\question Sample Question 2
\begin{oneparchoices}
\choice Option1
\correctchoice Option2
\correctchoice Option3
\correctchoice Option4
\end{oneparchoices}
\end{questions}
\printAnswers
\end{document}