# How could I typeset odd item or even item respectively in examination paper?

I want to write a tex file of an examination paper like

(Derivative function)
1. $3x-1$
2. $2x+1$
3. $4x^2+2x+3$
4. $7x^3-3x-6$
5. $\sin6x-7$
6. $\cos5x+2$


And then, I can set a logical variable \examtype, say, =0, typeset

Exam paper (A)
(Derivative function)
2. $2x+1$
4. $7x^3-3x-6$
6. $\cos5x+2$


otherwise, set \examtype=1, typeset

Exam paper (B)
(Derivative function)
1. $3x-1$
3. $4x^2+2x+3$
5. $\sin6x-7$


.

-
– Peter Grill Dec 3 '12 at 5:46
@Peter Thank you, though the answers in the link is too complicated over my need. And, I want the labels are typeset in "real" order. – fastbamra Dec 3 '12 at 6:17

Here is a version that provides a few options. It defines a MyExam environment which accepts two parameters: The first is the title, and the second is used to select which items you want printed.

## 1. odd or even:

You can select to print odd or even questions:

## 2. List of Questions:

You can print a specific questions either as a explicitly specified list 1,5,6 of questions, or a range of questions 1,...,5:

Or a combination of a list and range 1,...,4,6:

## Code:

\documentclass{article}
\usepackage{enumitem}
\usepackage{xstring}
\usepackage{etoolbox}
\usepackage{pgffor}
\usepackage{pgfmath}

\newlist{MyEnumerate}{enumerate}{1}
\setlist[MyEnumerate]{label={\arabic*.}}

\newcommand{\QuestionSelector}{}%
\newcounter{MyExamCounter}
\newtoggle{DisplayThisItem}%
\newcommand{\MyItem}[1]{%
\stepcounter{MyExamCounter}%
\global\togglefalse{DisplayThisItem}%
\pgfmathsetmacro{\Remainder}{mod(\arabic{MyExamCounter},2)}%
\IfStrEqCase{\QuestionSelector}{
{odd}{\IfEq{\Remainder}{1}{\global\toggletrue{DisplayThisItem}}{}}%
{even}{\IfEq{\Remainder}{0}{\global\toggletrue{DisplayThisItem}}{}}%
}[%
\edef\ListOfItems{\QuestionSelector}%
\foreach \x in \ListOfItems {%
\IfEq{\x}{\arabic{MyExamCounter}}{%
\global\toggletrue{DisplayThisItem}%
\breakforeach%
}{}%
}%
]%
\iftoggle{DisplayThisItem}{%
\begin{MyEnumerate}[series=MyExamList]%
\item[\arabic{MyExamCounter}.] #1%
\end{MyEnumerate}%
}{}%
}%

\newenvironment{MyExam}[2]{%
\setcounter{MyExamCounter}{0}%
\renewcommand*{\QuestionSelector}{#2}%
\par\noindent\textbf{#1}%
}{%
}

\newcommand*{\MyListOfQuestions}{%
\MyItem{$3x-1$}
\MyItem{$2x+1$}
\MyItem{$4x^2+2x+3$}
\MyItem{$7x^3-3x-6$}
\MyItem{$\sin6x-7$}
\MyItem{$\cos5x+2$}
}%
\begin{document}
\begin{minipage}[t]{0.45\linewidth}
\begin{MyExam}{Derivative function: Odd}{odd}
\MyListOfQuestions
\end{MyExam}
\end{minipage}%
\begin{minipage}[t]{0.45\linewidth}
\begin{MyExam}{Derivative function: Even}{even}
\MyListOfQuestions
\end{MyExam}
\end{minipage}%

\bigskip
\begin{minipage}[t]{0.45\linewidth}
\begin{MyExam}{Derivative function: 1,5,6}{1,5,6}
\MyListOfQuestions
\end{MyExam}
\end{minipage}%
\begin{minipage}[t]{0.45\linewidth}
\begin{MyExam}{Derivative function: 1,...,5}{1,...,5}
\MyListOfQuestions
\end{MyExam}
\end{minipage}

\bigskip
\begin{MyExam}{Derivative function: 1,...,4,6}{1,...,4,6}
\MyListOfQuestions
\end{MyExam}
\end{document}

-
Hi @Peter, I appreciate your generous help. And your answer is very good. There is only one question: I prefer using the orignal \item syntax instead of including the content of the items in double braces. It is also easy to change the sourse code. Would you have any advice on it? – fastbamra Dec 3 '12 at 7:38
@fastbamra: That is probably is not going to be as easy as it seems. See the related question I linked to as a comment to the question. BTW, you should use the full userid when using the @ syntax -- I was only notified of this as it is a comment on my answer, otherwise I would not have seen it. – Peter Grill Dec 3 '12 at 7:44
Oh sorry, I did not notice that. I find there is a package tagging can omit some items in itemize by choosing specific tag. Could it have some effect on it? – fastbamra Dec 3 '12 at 8:34
Thanks, I was not aware of the tagging package. However, it appears that that requires you to wrap each item within an environment (at least in the example I saw). I think that is going to be more work than using just \MyItem. Although I think that this can be down with some hacking of the code from the enumitem pacakge. – Peter Grill Dec 3 '12 at 9:38