[Tex/LaTex] Determining total points in a section with exam class

exam

In an exam I am writing, I have a short answer section and a multiple choice section. Each of these sections should have questions starting at 1. As pointed out in this question, if you want to do this and avoid warnings about name collisions of question labels you need to modify the question labels to include the section name; I have done this using \xpatchcmd as shown in the example below. The patch is successful (verified by looking at the .aux file and lack of warning messages).

The problem is that I do not want the grade table organized by page or grade number, but rather by section. Since there does not appear to be a built in way to create a grade table in this way, I am doing it manually by using \begingraderange, \endgraderange, and \pointsinrange in a custom table. The issue that I am having is that the \pointsinrange totals are not accurate. I think it is because there is still some sort of name collision beyond the question labels that happens when summing the points in the ranges; I think it has to do with the \newlabel commands in the .aux files (marked with <=== below), where the unique question labels are defined but the value for each of these are the same.

When I run the following code, in the grade table I get that both the Short Answer and Multiple Choice have 2 total points, but it should be 10 and 4, respectively. I get the correct value for the overall total points (14). Any thoughts on how I can get this to work properly while also being able to reset the question number in each section to 1?

\documentclass[11pt,addpoints]{exam}

% The following patches avoid name collisions with questions
% of the same number in different sections by adding the section
% name to the question label.
\usepackage{xpatch}
\xpatchcmd{\questions}{question@\arabic{question}}
                      {question@\arabic{section}@\arabic{question}}{}{}
\xpatchcmd{\parts}{part@\arabic{question}@\arabic{partno}}
                  {part@\arabic{section}@\arabic{question}@\arabic{partno}}{}{}
\xpatchcmd{\first@questionobject}{question\arabic{question}}
                                 {question\arabic{section}@\arabic{question}}{}{}
\xpatchcmd{\addquestionobject}{question\arabic{question}}
                              {question\arabic{section}@\arabic{question}}{}{}
\xpatchcmd{\questionobject@pluspagecheck}{question\arabic{question}}
                                         {question\arabic{section}@\arabic{question}}{}{}

\begin{document}

\section{Short Answer}
\begin{questions}
\begingradingrange{section1}
\question[5] First Question
\question[5] Second Question
\endgradingrange{section1}
\end{questions}

\section{Multiple Choice}
\begin{questions}
\begingradingrange{section2}
\question[2] First Question
\question[2] Second Question
\endgradingrange{section2}
\end{questions}

\section{Extra Credit}
\begin{questions}
\bonusquestion[5] Bonus
\end{questions}

\begin{center}
\begin{tabular}{|r|c|c|}
 \hline
 \multicolumn{3}{|c|}{\textbf{Exam Score}}                           \\ \hline
 \textbf{Section} & \textbf{Points}                 & \textbf{Score} \\ \hline
 Short Answer     & \pointsinrange{section1}        &                \\ \hline
 Multiple Choice  & \pointsinrange{section2}        &                \\ \hline
 Bonus            & \numbonuspoints                 &                \\ \hline
 Total            & \numpoints                      &                \\ \hline
\end{tabular}
\end{center}

\end{document}

Excerpt of the .aux file:

...
\PgInfo{question@1@2}{1}
\PgInfo{question1@2@object1}{1}
\newlabel{question@1@2}{{2}{1}}  <=== Value same as question@2@2
...
\PgInfo{question@2@2}{1}
\PgInfo{question2@2@object1}{1}
\newlabel{question@2@2}{{2}{1}}  <=== Value same as question@1@2

Best Answer

This is another solution that uses a fake counter for printing fake question numbers.

First of all, we define the counter (which gets reset at each \section) and the format for titled questions and titled bonus questions:

\newcounter{fakecount}[section]
\qformat{\thefakecount. (\thepoints) \thequestiontitle\hfill}
\bonusqformat{\thefakecount. (\thebonuspoints) \thequestiontitle\hfill}

At the beginning of questions we redefine \titledquestion and \bonustitledquestion to increment the fake counter:

\let\oldtitledquestion\titledquestion
\renewcommand{\titledquestion}{\stepcounter{fakecount}\oldtitledquestion}
\let\oldbonustitledquestion\bonustitledquestion
\renewcommand{\bonustitledquestion}{\stepcounter{fakecount}\oldbonustitledquestion}

At this point, you only have to use \titledquestion instead of \question and \bonustitledquestion instead of \bonusquestion to let it work.

MWE (notice the use of \leavevmode before EnvFullwidth environments to avoid unwanted behaviors...)

\documentclass[11pt,addpoints]{exam}
\usepackage{lipsum}
\newcounter{fakecount}[section]
\qformat{\thefakecount. (\thepoints) \thequestiontitle\hfill}
\bonusqformat{\thefakecount. (\thebonuspoints) \thequestiontitle\hfill}

\begin{document}

\begin{questions}
\let\oldtitledquestion\titledquestion
\renewcommand{\titledquestion}{\stepcounter{fakecount}\oldtitledquestion}
\let\oldbonustitledquestion\bonustitledquestion
\renewcommand{\bonustitledquestion}{\stepcounter{fakecount}\oldbonustitledquestion}

\begin{EnvFullwidth}
\section{Short Answer}
\lipsum[2]
\end{EnvFullwidth}

\begingradingrange{section1}
\titledquestion{First Question}[5]
\titledquestion{Second Question}[5]
\endgradingrange{section1}

\leavevmode
\begin{EnvFullwidth}
\section{Multiple Choice}
\lipsum[2]
\end{EnvFullwidth}

\begingradingrange{section2}
\titledquestion{First Question}[2]
\titledquestion{Second Question}[2]
\endgradingrange{section2}

\leavevmode
\begin{EnvFullwidth}
\section{Extra Credit}
\lipsum[2]
\end{EnvFullwidth}

\bonustitledquestion{Bonus}[5]
\end{questions}

\begin{center}
\begin{tabular}{|r|c|c|}
 \hline
 \multicolumn{3}{|c|}{\textbf{Exam Score}}                           \\ \hline
 \textbf{Section} & \textbf{Points}                 & \textbf{Score} \\ \hline
 Short Answer     & \pointsinrange{section1}        &                \\ \hline
 Multiple Choice  & \pointsinrange{section2}        &                \\ \hline
 Bonus            & \numbonuspoints                 &                \\ \hline
 Total            & \numpoints                      &                \\ \hline
\end{tabular}
\end{center}

\end{document} 

Output:

enter image description here

Related Question