試験クラスでデフォルトの質問ポイント値を設定するにはどうすればよいですか?

試験クラスでデフォルトの質問ポイント値を設定するにはどうすればよいですか?

授業で試験を受けるのです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}

(注: これらの質問の実際の難易度についてコメントするつもりはありません!)

出力は、最初の 3 つの質問が で宣言され\question[2]、次の 5 つの質問が で宣言された場合と同じになるはずです\question[3]。9 番目の質問は、デフォルトのポイントのグループの外にあるため、ポイントを添付しないでください。最後の質問は、ポイントが定義された通常の質問として機能するはずです。

すでに ah@cky の解決策があり、そのうち投稿するつもりですが、もっと良い解決策があるかどうか知りたいです。

答え1

\question基本的には、 を呼び出すように再定義するだけです\oldQuestion[⟨default value⟩]

グループ内でデフォルト ポイントがすでに 1 回設定されている場合に\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 による解決策はより明確でシンプルです。

関連情報