是否可以在考试类别中对一个问题应用多个标签并仅打印标有特定标签的问题?
例如 :
\begin{questions}
\question This is question 1. \label{2002}\label{1996}
\question This is question 2. \label{1996}
..
..
\question This is question 12. \label{1996}\label{2000}
\end{questions}
现在,在生成 PDF 输出时,是否可以仅包含标有标签 \label{1996} 的问题?
编辑:请使其灵活,以便允许用户打印任意数量的标签。很抱歉,我没有在问题中提到这一点。
谢谢。
答案1
为了吃掉问题,我不得不把它放在括号里。如果没有指定键,则会出现所有问题。
\documentclass{exam}
\usepackage{xstring}% overkill
\newcommand{\SKey}{,,}% default
\newcommand{\selectkey}[1]% #1 = key to be used to select questions
{\xdef\SKey{,#1,}}
\newcommand{\selectquestion}[2]% #1 = comma delimited list of keys, #2 is the question (in braces)
{\IfStrEq{\SKey}{,,}{\question #2}{\IfSubStr{,#1,}{\SKey}{\question #2}{}}}
\begin{document}
\selectkey{2002}% only print questions with 2002 in list
\begin{questions}
\selectquestion{2002,1996}{This is question 1.}
\selectquestion{1996}{This is question 2.}
\selectquestion{2002}{This is question 3.}
\end{questions}
\end{document}
修改的解决方案允许选择多个键。
\documentclass{exam}
\usepackage{xstring}
\newcommand{\SKey}{}% default
\newif\ifkeyfound
\newcommand{\selectkeys}[1]% #1 = comma delimited list of keys to be used
{\xdef\SKey{#1}}
\newcommand{\keysearch}[2]% sets \ifkeyfound for any matching keywords in two comma delimited lists
{\bgroup% use local definitions, recursive
\StrCount{#1}{,}[\num]%
\ifnum\num=0\relax
\IfSubStr{,#2,}{,#1,}{\global\keyfoundtrue}{\global\keyfoundfalse}%
\else
\StrCut{#1}{,}{\test}{\temp}%
\IfSubStr{,#2,}{,\test,}{\global\keyfoundtrue}{\keysearch{\temp}{#2}}%
\fi
\egroup}
\newcommand{\selectquestion}[2]% #1 = comma delimited list of keys, #2 is the question (in braces)
{\IfStrEq{\SKey}{}{\question #2}{% else
\keysearch{\SKey}{#1}%
\ifkeyfound{\question #2}\fi}%
}
\begin{document}
\selectkeys{1997,2002}% only print questions with 1997 or 2002 in list
\begin{questions}
\selectquestion{2002,1996}{This is question 1.}
\selectquestion{1996}{This is question 2.}
\selectquestion{2002}{This is question 3.}
\selectquestion{1997}{This is question 4.}
\end{questions}
\end{document}
注意:不能将xstring
例程放在里面\loop
,因此使用递归搜索。