I am using exam class to typeset an exam. As MWE, my exam has an MCQ section and Short answer sections.
In the grade table, I want the MCQ section to display as one row of total marks (instead of 20-30 boxes of 1 point), and the Short section to be by question. Since there is no pre-defined command, I thought to write my own table generator:
\documentclass[addpoints]{exam}
\begin{document}
\newcounter{qnitr}
\setcounter{qnitr}{\firstqinrange{short}}
\begin{tabular}{|c|c|c|}
\hline
Qns & Points & Score \\hline
MCQ & \pointsinrange{mcq} & \\hline
\whiledo{ \not ( \theqnitr > \lastqinrange{short} ) }{%
\ifthenelse{ \not ( \theqnitr > \lastqinrange{short} ) }{%
\theqnitr & \pointsofquestion{\theqnitr} & \ \hline
}{blah}
\stepcounter{qnitr}
}
Total: & \numpoints & \\hline
\end{tabular}
\begin{questions}
\section{MCQ Section}
\begingradingrange{mcq}
\question[2]
MCQ Question 1
\question[2]
MCQ Question 2
\question[2]
MCQ Question 3
\endgradingrange{mcq}
\section{Short Answer Section}
\begingradingrange{short}
\question
This question has 3 parts.
\begin{parts}
\part[2]
Part 1.
\part[2]
Part 2.
\part[3]
Part 3
\end{parts}
\question
This question has 2 parts.
\begin{parts}
\part[5]
Part 1.
\part[4]
Part 2.
\end{parts}
\endgradingrange{short}
\end{questions}
\end{document}
What I want to achieve is a table like this:
However, \setcounter{qnitr}{\firstqinrange{short}} is giving me an error.
I've spent hours trying to look up expanding macros for use in \setcounter but I can't seem to find a solution. I learned that \setcounter is fragile and requires expansion to a number. But I've tried various combinations of \noexpand, \protect, \value and due to my lack of knowledge and understanding of LaTeX evaluation, I am just randomly trying.
I looked into the exam.cls to find clues on how the table is generated and found codes like
\edef\tbl@firstq{\csname range@\tbl@range @firstq\endcsname}%
\edef\tbl@lastq{\csname range@\tbl@range @lastq\endcsname}%
\let\first@pq@index=\tbl@firstq
\let\last@pq@index=\tbl@lastq
...
\setcounter{num@cols}{\tbl@lastq}%
\addtocounter{num@cols}{-\tbl@firstq}%
How come the exam class is able to set a counter to a command but when I try to do it, it keeps failing?


\setcounter's second argument, but the macro you're using there has to be expandable. I'd guess that\firstqinrangeisn't fully expandable, and hence you're getting these problems. – Skillmon Nov 08 '23 at 07:11\firstqinrangeneeds to be evaluated, but\setcounteris only doing expansion? In this case how can I evaluate it to a number, then use it in\setcounter? – welcomb Nov 08 '23 at 17:16