2

Is it possible to apply multiple labels to a question in exam class and print questions marked with specific labels only?

for example :

\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}

Now while generating PDF output, is it possible to include only the questions marked with the label \label{1996}?

Edit: Please make it flexible so as to allow as many labels as user wants to print. My bad that I did not mention it in the question.

Thanks.

2
  • 2
    \labels with the same name are not allowed. Also, if you remove or add a question then the numbers will be different. Would something like \myquestion{2002,1996} do? Dec 25, 2016 at 16:19
  • Yes, no problem in using this.
    – cosmicraga
    Dec 26, 2016 at 14:20

1 Answer 1

5

In order to eat the question, I had to put it inside braces. If no key is specified, all questions will appear.

\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}

Revised solution allowing multiple keys to be selected.

\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}

Note: cannot put xstring routines inside a \loop, so used a recursive search instead.

3
  • \selectkey{2002}% only print questions with 2002 in list How to put more than one labels in this list for printing?
    – cosmicraga
    Dec 26, 2016 at 14:11
  • Very close to what I needed. Please make it flexible so as to allow as many labels as user wants to print. My bad that I did not mention it in the question.
    – cosmicraga
    Dec 26, 2016 at 14:16
  • Will it be possible at least to get the selected .tex content dumped in a separate file ? So that i can fulfill the task manually by repeating the above code several times?
    – cosmicraga
    Dec 26, 2016 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .