[Tex/LaTex] How to count the number of questions separately for different parts in exam class

countersexam

I am creating an exam with two parts, the first part is short questions and the second part is essay question, and I would like to automatically count the number of questions for the two parts separately. By default, the \numquestions displays the total number of questions in the document.

\documentclass{exam}

\begin{document}

\begin{questions}
\fullwidth{\Large \textbf{Short questions}}    
There are \numquestions\ short questions. %display the number of short questions.

\question
This is the first short question.
\question
This is the second short question.

\setcounter{question}{0}

\fullwidth{\Large \textbf{Essay questions}}
There are \numquestions\ essay questions. %display the number of essay questions.

\question
This is the first essay question.
\question
This is the second essay question.
\question
This is the third essay question.
\question
This is the fourth essay question.
\end{questions}
\end{document}

Best Answer

Here is a solution using the totcount package and redefining the \question command with \squestion for the short questions and \equestion for the essay questions.

\documentclass{exam}

\usepackage{totcount}

\newtotcounter{s}
\setcounter{s}{0}
\newtotcounter{e}
\setcounter{e}{0}
\newcommand{\squestion}[0]{\stepcounter{s}\question}
\newcommand{\equestion}[0]{\stepcounter{e}\question}


\begin{document}

\fullwidth{\Large \textbf{Short questions}}    
There are \total{s}\ short questions. %display the number of short questions.

\begin{questions}
\squestion
This is the first short question.
\squestion
This is the second short question.
\end{questions}

\setcounter{question}{0}

\fullwidth{\Large \textbf{Essay questions}}
There are \total{e}\ essay questions. %display the number of essay questions.

\begin{questions}
\equestion
This is the first essay question.
\equestion
This is the second essay question.
\equestion
This is the third essay question.
\equestion
This is the fourth essay question.
\end{questions}

For the record there is a total of \numquestions\ questions.

\end{document}

And here is the output:

Output

Note The \numquestions command hasn't been modified so that you can still use it if you need to have the total number of questions (both short ones and essay ones).