我正在課堂上寫考試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
(聲明=如果已定義,則不執行任何操作),以便\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 的解決方案更乾淨、更簡單。