考試課程:重新定義 \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.cls,這樣我就不必l在環境的開頭添加該獎勵,並且可以使用預設包含的名稱question來代替?

編輯:一個最小的工作範例:

\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\bfseries在開始時具有」。您發現的問題是questions環境定義了\question命令。在這種情況下,我認為你的例子適用於

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

我對需要\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}標籤處發生的事情。當然,還有更高級的方法可以做到這一點,但這是最簡單的方法,在這種情況下不需要更複雜的方法。

相關內容