这在某种程度上是一个后续问题这个问题。我在写答案的时候遇到了这个问题,但我以为我以后可以轻松解决这个问题,结果却错了。真可惜。
问题是,虽然过滤器对于诸如的键可以正常工作type
,但对于键却不行difficulty
。
以下是 MWE:
\documentclass{article}
\usepackage{exercise}
\begin{document}
Here is a short exercise :
\ExerciseSelect[type={short}]
\begin{Exercise}[type={short}]
Prove that $1+1=2$.
\end{Exercise}
\begin{Exercise}[type={long}]
Prove that $P \neq NP$.
\end{Exercise}
\ExerciseStopSelect
\vspace{1cm}\hrule\vspace{1cm}
Now here is a hard exercise :
\ExerciseSelect[difficulty={2}]
\begin{Exercise}[difficulty=0]
Prove that $1+1=2$.
\end{Exercise}
\begin{Exercise}[difficulty=2]
Prove that $P \neq NP$.
\end{Exercise}
\ExerciseStopSelect
Or an easy one :
\ExerciseSelect[difficulty={0}]
\begin{Exercise}[difficulty={0}]
Prove that $1+1=2$.
\end{Exercise}
\begin{Exercise}[difficulty={2}]
Prove that $P \neq NP$.
\end{Exercise}
\ExerciseStopSelect
\end{document}
其输出如下:
正如您所见,过滤器没有选择任何内容difficulty
。
答案1
解决方案比我最初想象的要复杂一些。该软件包exercise
过于依赖计数器来确定难度级别,因此我的第一个方法没有用。我认为,在测试难度标志时,是其对应的计数器导致标志无法升起。
我在这里所做的是保存代码中exercise
我认为不需要重写但确实需要扩充的部分。我还创建了一个新的标志调用howdifficult
。它的设置方式与相同,difficult
我相信具有相同的预期效果。(我还没有测试所有可能的情况,但这需要我比以前更彻底地阅读手册。)
下面的代码似乎有效。该difficulty
键触发了几个标志。我认为此代码触发了所有必要的标志。但就目前而言,这确实提供了一个无错误的补丁。
\makeatletter
%% Adding new key to the "Exercise" environment
%% save old initialization code in a temporary command and then redefine.
\let\@tmp@InitExe\@InitExe
\def\@InitExe{\@tmp@InitExe%
\gdef\ExerciseHowDifficult{}%
\global\@ExeHowDifficultfalse%
}
%% For "Exercise" environment flags, set key-value="howdifficult" and
%% corresponding flags to true. Also, set the counter for
%% key-value='difficult'. These additional settings for 'difficulty' may
%% not be necessary; I've put it here just to be on the safe side.
\newif\if@ExeHowDifficult \@ExeHowDifficultfalse
\define@key{PPExercise}{howdifficult}%
{\global\@ExeHowDifficulttrue\gdef\ExerciseHowDifficult{#1}%
\global\@ExeDifficultytrue%
\global\ExerciseDifficulty=\number#1}
%% set the Header for key-value="howdifficult"
%% to be the header used by key-value="difficulty"
\let\@tmp@getExerciseInfo\@getExerciseInfo
\def\@getExerciseInfo{\@tmp@getExerciseInfo%
\if@ExeHowDifficult\else\def\ExerciseHeaderDifficulty{}\fi%
}
%% Adding new key to "Exercise Selection" routine to guarantee shipout
%% routine is properly carried out.
%% save old initialization code in a temporary command and then redefine.
\let\@tmp@selectExercise\@selectExercise
\def\@selectExercise{\@tmp@selectExercise%
\if@ExeSelectHowDifficult
\@for\@howdifficult:=\ExerciseSelectHowDifficult\do
{ \ifthenelse{\equal{\@howdifficult}{\ExerciseHowDifficult}}{
\global\@@ShipThisExercisetrue
}{}
}
\if@@ShipThisExercise\else\@ShipThisExercisefalse\fi
\global\@@ShipThisExercisefalse
\fi
}
%% Set appropriate difficulty flags for exercise selection.
\newif\if@ExeSelectHowDifficult \@ExeSelectHowDifficultfalse
\define@key{PPExerciseSelect}{howdifficult}%
{\global\@ExeSelectHowDifficulttrue\gdef\ExerciseSelectHowDifficult{#1}}
%----------------------------------------------------------------------
%% It's not clear to me that the following code is at all necessary. I
%% put it here so that the user of the package can completely dispense with
%% all uses of the key-value="difficulty".
%----------------------------------------------------------------------
%% translate infor from key-value="howdifficult" to "\QuestionDifficulty"
\define@key{PPQuestion}{howdifficult}{%
\global\@QuestionDifficultytrue\global\QuestionDifficulty=\number#1}
%% translate infor from key-value="howdifficult" to "\subQuestionDifficulty"
\define@key{PPsubQuestion}{howdifficult}{%
\global\@subQuestionDifficultytrue\global\subQuestionDifficulty=\number#1}
%% translate infor from key-value="howdifficult" to "\subsubQuestionDifficulty"
\define@key{PPsubsubQuestion}{howdifficult}{%
\global\@subsubQuestionDifficultytrue\global\subsubQuestionDifficulty=\number#1}
%% translate infor from key-value="howdifficult" to "\ExePartDifficulty"
\define@key{PPExePart}{howdifficult}{\global\@ExePartDifficultytrue%
\global\ExePartDifficulty=\number#1}
\makeatother
我应该指出,同时使用键值difficulty
和可能会导致不确定的结果。因此,我添加了代码,虽然不是完全必要的,但允许包的用户在所有可能需要的情况下howdifficult
忘记密钥difficulty
和用户。howdifficult