考試課程:定義取決於 \printanswers 的新環境

考試課程:定義取決於 \printanswers 的新環境

我在定義新環境時遇到問題,該環境取決於考試課程的 \printanswers 選項。

\documentclass{exam}
\usepackage{framed}

\printanswers
%\noprintanswers 

\newenvironment{note}{\ifprintanswers\begin{framed}\noindent\textbf{Note:}\par\noindent}{\end{framed}\fi}

\begin{document}
\begin{questions}
\question
question
\begin{solution}
    this is the solution
\end{solution}

\begin{note}
    this is a note
\end{note}

\end{questions}
\end{document}

當我使用 \printanswers 時,一切看起來都很好,但是當我使用 \noprintanswers 時,我收到錯誤:

! Incomplete \iffalse; all text was ignored after line 17.

我希望在使用 \noprintanswers 時隱藏筆記環境。

答案1

要抑制環境,一種技巧是使其等於{comment}逐字包定義的環境。另一種是將命令體放入 a\vbox並丟棄\vbox,這是考試環境使用的解決方案。方法如下\vbox

\documentclass{exam}
\usepackage{framed}

\newenvironment{note}{%
    \ifprintanswers
        \begin{framed}\noindent\textbf{Note:}\par\noindent
    \else
        \setbox0\vbox\bgroup
    \fi
}{%
    \ifprintanswers
        \end{framed}%
    \else
        \egroup
    \fi%
}

\printanswers
%\noprintanswers

\begin{document}
\begin{questions}
\question
question
\begin{solution}
    this is the solution
\end{solution}

\begin{note}
    this is a note
\end{note}

\end{questions}
\end{document} 

相關內容