Я пишу экзамен в 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]
. Девятый вопрос находится вне группы точек по умолчанию, поэтому к нему не должно быть прикреплено никаких точек. Последний вопрос должен работать как обычный вопрос с определенными точками.
У меня уже есть отличное решение, и я его опубликую в будущем, но мне интересно, есть ли варианты получше.
решение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 чище и проще.