我正在课堂上参加考试exam
,我想一次性为整个问题组设置分值。例如:
\documentclass{exam}
\title{Know your US States and Capitals!}
\begin{document}
\begin{questions}
\begingroup
\defaultpoints{2}
\question What is the capital of California?
\begin{choices}
\choice Los Angeles
\choice Sacramento
\choice San Francisco
\choice San Diego
\end{choices}
\question Which city serves as the capital of Texas?
\begin{choices}
\choice Dallas
\choice Houston
\choice Austin
\choice San Antonio
\end{choices}
\question What is the capital of Florida?
\begin{choices}
\choice Miami
\choice Orlando
\choice Jacksonville
\choice Tallahassee
\end{choices}
\endgroup
\begingroup
\defaultpoints{3}
\question Which city is the capital of New York?
\begin{choices}
\choice Buffalo
\choice Albany
\choice New York City
\choice Syracuse
\end{choices}
\question What is the capital of Nevada?
\begin{choices}
\choice Reno
\choice Henderson
\choice Carson City
\choice Las Vegas
\end{choices}
\question Which city serves as the capital of Colorado?
\begin{choices}
\choice Denver
\choice Boulder
\choice Colorado Springs
\choice Aurora
\end{choices}
\question What is the capital of Massachusetts?
\begin{choices}
\choice Springfield
\choice Boston
\choice Worcester
\choice Cambridge
\end{choices}
\question Which city is the capital of Washington state?
\begin{choices}
\choice Seattle
\choice Tacoma
\choice Spokane
\choice Olympia
\end{choices}
\endgroup
% no points should be specified
\question What is the capital of Hawaii?
\begin{choices}
\choice Maui
\choice Honolulu
\choice Kauai
\choice Hilo
\end{choices}
\question[5] Which city serves as the capital of Ohio?
\begin{choices}
\choice Cincinnati
\choice Columbus
\choice Cleveland
\choice Dayton
\end{choices}
\end{questions}
\end{document}
(注意:我无意评论这些问题的实际难度!)
\question[2]
输出结果应与前三个问题用 声明、后五个问题用 声明时相同\question[3]
。第九个问题不在默认点组内,因此不应附加任何点。最后一个问题应作为定义了点的正常问题运行。
我已经有 ah@cky 解决方案,我最终会发布它,但我很好奇是否还有更好的解决方案。
答案1
基本上只需重新定义\question
即可调用\oldQuestion[⟨default value⟩]
。
请注意使用\DeclareCommandCopy
(declare = 如果已经定义,则不执行任何操作)以便\oldQuestion
在组中已经设置了默认点的情况下不覆盖定义。
这假设所有命令都在一个组中本地定义(这似乎非常安全,因为 LaTeX 维护者确实不想破坏向后兼容性)。
%! TEX program = pdflatex
\documentclass{exam}
\title{Know your US States and Capitals!}
\NewDocumentCommand\defaultpoints{m}{%
\DeclareCommandCopy\oldQuestion\question%
\RenewDocumentCommand\question{O{#1}}{%
\oldQuestion[##1]%
}%
}
\begin{document}
\begin{questions}
\begingroup
\defaultpoints{2}
\question What is the capital of California?
\begin{choices}
\choice Los Angeles
\choice Sacramento
\choice San Francisco
\choice San Diego
\end{choices}
\question Which city serves as the capital of Texas?
\begin{choices}
\choice Dallas
\choice Houston
\choice Austin
\choice San Antonio
\end{choices}
\question What is the capital of Florida?
\begin{choices}
\choice Miami
\choice Orlando
\choice Jacksonville
\choice Tallahassee
\end{choices}
\endgroup
\begingroup
\defaultpoints{3}
\question Which city is the capital of New York?
\begin{choices}
\choice Buffalo
\choice Albany
\choice New York City
\choice Syracuse
\end{choices}
\question What is the capital of Nevada?
\begin{choices}
\choice Reno
\choice Henderson
\choice Carson City
\choice Las Vegas
\end{choices}
\question Which city serves as the capital of Colorado?
\begin{choices}
\choice Denver
\choice Boulder
\choice Colorado Springs
\choice Aurora
\end{choices}
\question What is the capital of Massachusetts?
\begin{choices}
\choice Springfield
\choice Boston
\choice Worcester
\choice Cambridge
\end{choices}
\question Which city is the capital of Washington state?
\begin{choices}
\choice Seattle
\choice Tacoma
\choice Spokane
\choice Olympia
\end{choices}
\endgroup
% no points should be specified
\question What is the capital of Hawaii?
\begin{choices}
\choice Maui
\choice Honolulu
\choice Kauai
\choice Hilo
\end{choices}
\question[5] Which city serves as the capital of Ohio?
\begin{choices}
\choice Cincinnati
\choice Columbus
\choice Cleveland
\choice Dayton
\end{choices}
\end{questions}
\end{document}
答案2
这是我最初想到的解决方案。在序言中:
\makeatletter
\let\orig@doitem\@doitem
\newcommand{\defaultpoints}[1]{
\def\@doitem{\@ifnextchar[{\@readpoints}{\@readpoints[#1]}}
}
\newcommand{\nodefaultpoints}{
\let\@doitem\orig@doitem
}
\makeatother
如你所见,我的 LaTeX 习惯已经过时了。\@doitem
为了安全起见,我复制了副本,并重新定义为在没有任何声明数字的情况下\@doitem
调用默认点号。\@readpoints
但这依赖于\question
命令的内部结构。user202729 的解决方案更简洁、更简单。