試験クラス: カスタム書式を適用するために \question コマンドを再定義する最良の方法

試験クラス: カスタム書式を適用するために \question コマンドを再定義する最良の方法

編集: 要約すると、回答は本当にエレガントで簡潔になりました。以下のすべての回答に感謝します!

\usepackage{etoolbox}
%...
\renewcommand\questionshook{\preto{\question}{\large\bfseries}}

% If we don't want the question's parts to be huge as well!
%\renewcommand\partshook{\normalsize\normalfont}

簡単に言うと、私はカスタム試験シートドキュメントクラスを生成したいのですが、examクラス同僚のグループで使用するために、デフォルトの\questionコマンドを再定義してカスタム書式を適用します。

\questionデフォルトのセクションを大きく太字にしたいとします。現在行っている操作は次のとおりです。

\newcommand\lquestion{\large\bfseries\question}

したがって、カスタム書式を使用するには、代わりにドキュメントに次のように記述する必要があります。

\begin{lquestion}
A large and bold question: what is the mass of the sun?
\end{lquestion}

(LaTeX ではコマンドを環境として使用しても問題ないと読んだことがありますが、水中の石が隠れてしまう場合は訂正してください。)

環境の先頭にそのボーナスを追加する必要がなくなり、代わりにデフォルトの含まれる名前を使用できるように、questionから取得されるコマンドを環境として再定義する最適な方法は何でしょうか?exam.clslquestion

編集: 最小限の動作例:

\documentclass{exam}
\usepackage[utf8]{inputenc}

\title{MWE}
\author{John Doe}
\date{November 2020}

%print solutions by default (for MWE's sake)
\printanswers
% do not put a box/frame around printed solutions
\unframedsolutions

\begin{document}

\maketitle

\begin{questions}
    %this is basically what I want the result to look like:
    {\large\bfseries
        \question My large and bold question:
        \begin{parts}
        \part First part
        \part Second part
        \end{parts}
    }
    %but instead of this rather inconvenient form, I want to define an environment {question} with the same effect and use it like:
    %
    % \begin{question}
    % My large and bold question:
    % \begin{parts}
    %     \part First part
    %     \part Second part
    % \end{parts}
    % \end{question}
    
    
    \begin{solution}
    My general solution prerequisite.
    \begin{parts}
        \part My first part solution
        \part My second part solution
    \end{parts}
    \end{solution}
\end{questions}

\end{document}

の定義\questionは、2845行目にあります。exam.cls環境内questions:


\newenvironment{questions}{%
  %.... lots of code here
  \def\question{%
    \@bonusfalse
    \def\thequestiontitle{\csname p@question\endcsname
                          \csname thequestion\endcsname}%
    \process@question
  }%
  %.... lots of other code here
%and waaaaay down on line 3143
}% End of the second argument of \newenvironment{questions}

前もって感謝します!

答え1

コマンドを環境として使用できることはすでにわかっているようですので、質問は実際には「コマンドを先頭に再定義するquestionにはどうすればよいか」ということです。あなたが発見した問題は、環境がコマンドを定義しているということです。その場合、あなたの例は次のように動作すると思います。\large\bfseriesquestions\question

\usepackage{etoolbox}
\appto{\questions}{%
 \preto{\question}{\large\bfseries}%
 \printanswers%
}

そこに入れなければならないのは残念ですが、そうしないと解決策が消えてしまいます。そのため、2 つの異なるコマンド\printanswersを追跡する必要があるかもしれません。\printanswers

環境に関する質問については、\begin{myenv}...\end{myenv}は になります\begingroup\myenv ...\endmyenv\endgroup(\endmyenvが存在しなくてもエラーは発生しません)。 結果として、 は自動的にグループ化され\large\bfseries、ドキュメントの残りの部分は変更されなくなります。

答え2

\questionshook実際には、、および を使用し\partshook\SolutionEmphasis書式設定の変更を処理することができます。

\documentclass{exam}
\usepackage[utf8]{inputenc}

\title{MWE}
\author{John Doe}
\date{November 2020}

%print solutions by default (for MWE's sake)
\printanswers
% do not put a box/frame around printed solutions
\unframedsolutions

\renewcommand\questionshook{\large\bfseries}
\renewcommand\partshook{\normalsize\normalfont}
\SolutionEmphasis{\normalsize\normalfont}

\begin{document}

\maketitle

\begin{questions}
     \question %\begingroup ... \endgroup not needed
     My large and bold question:
     \begin{parts}
         \part First part
         \part Second part
     \end{parts}
    
    \begin{solution}
    My general solution prerequisite.
    \begin{parts}
        \part My first part solution
        \part My second part solution
    \end{parts}
    \end{solution}
\end{questions}

\end{document}

答え3

環境を作成する最も基本的な方法は、 を使用することです\newenvironment{<name>}{<begin code>}{<end code>}。したがって、あなたの場合、これは単に になります。\newenvironment{\lquestion}{\large\bfseries\question}{}開始コードまたは終了コードに好きなものを追加できます。開始コードは\begin{lquestion}タグで発生するもので、終了コードは タグで発生するものです\end{lquestion}。もちろん、これを行うにはより高度な方法がありますが、これは最も簡単な方法であり、この場合はより複雑なものは必要ありません。

関連情報