# Hacking into (internal) references in exam class

So far, I have used my homebrewn style file to create the examination papers which have multiple sets of questions.

Recently, I switched to the exam class and immensely enjoying the additional power it provides. Now, in case of multiple sets, I am stuck at some point.

My setup, with all the paraphernalia striped, looks something like this. I use the macro \setcode which, along with other things, control the inclusion of external files and use of fonts at specific places.

\documentclass[addpoints]{exam}

\begin{document}

\def\setcode{1}
Total: \numpoints
\begin{questions}
\question[5] First question in Set 1.
\end{questions}

\def\setcode{2}
Total: \numpoints
\begin{questions}
\question[5] First question in Set 2.
\end{questions}

\end{document}


Now, the problem with the above scenario is, internals like \numpoints are incorrect. As is evidenced by the output and the warning message regarding multiply defined labels.

I found out that the labels are no more handled using \labels. Rather, these are written directly into the .aux file. See this excerpt from exam.cls.

This was previously done with \pageref commands. When I stopped using \pageref for this (in order to make this compatible with hyperref.sty), this stuff was created:

\gdef commands for exam@lastpage, exam@numpoints, exam@numbonuspoints, exam@numquestions, exam@numparts, exam@numsubparts and exam@numsubsubparts are written to the .aux file via \AtEndDocument.

I understand that if I could hack my \setcode into the above internals, my problem would have been solved.

Do you think you could suggest a way for this?

I know that a completely new setup, say something like this, perhaps would solve my problem. But the other side of the coin is, I already have a big setup from more than a decade, which will have to be completely reworked in order for the new setup to work.

-
My first impression: I wouldn't do that if I were you. The class is quite well designed for smaller elements (parts, subparts, subsubparts), but not the other way around. Even if you touch the code and add prefix where needed, everything is stored in the aux file and that would require additional rewritting of the code, a simple setting of the parameters wouldn't help. I would prepare one TeX file on a per student basis (generated from the database somehow, perhaps), that is a sort of workaround for this class. Or I would try the eqexam package (AcroTeX bundle) if it fits my needs better. – Malipivo Mar 28 '14 at 12:55
@Malipivo The eqexam looks like a promising one, and perhaps will solve my problem, again, with a major reworking of my present setup. So, I will take your first advice, use my setup, fitted into exam class. At the same time, I will want keep the uses of internal counters minimal. – Masroor Mar 28 '14 at 13:13
I am thinking about two scenarios: if there is a way of storing those key counters by ourselves (let's say a series of \labels or \lets), then there would be no need for that aux file at all, or, if there is a way of loading and saving the aux file intentionally on-the-fly (that's usually unrecommended way). – Malipivo Mar 28 '14 at 13:26
@Malipivo I was thinking of the first way and was looking into the cls file to identify the corresponding labels, so that I can append the set code to the labels. I hit a wall when I read the portion excerpted in my original question. – Masroor Mar 28 '14 at 13:54
I have started with chasing the counters. :-) \def\catchme{exam@numquestions: \thenumquestions\par exam@numparts: \thenumparts\par exam@numsubparts: \thenumsubparts\par exam@numsubsubparts: \thenumsubsubparts} \catchme – Malipivo Mar 28 '14 at 14:19

An experiment (partly solved based on comments)

I am not done, but I am presenting my findings for the other solvers to save some time. Below is my patch/style file catching and clearing most of the counters. Unresolved problems are:

• Points and bonus points of an individual question, if they are needed. \pointsofq@ and \bonuspointsofq@ of question i, ii etc.
• Also basic staff like last page of the exam is missing. It only requires \label with a unique marker, e.g. a counter of exams.
• How to unload the content of the \AtEndDocument{} command without touching the exam.cls file, e.g. by deleting that portion of code. The reason is that we dislike that warning about multiply defined labels.
• To actually catch the counters by \labels for instance and loading them back by \ref or \pageref. This experiment is listing the values but we cannot use them at the beginning or in the middle of the actual exam.
• Are there any more counters in that class? :-)

This is the mal-patch.sty file with two commands, the \catchme is calling for \clearme automatically, but I tried to separate these two tasks from each other.

\makeatletter

%\newcounter{pageof@pagepoints}
%\newcounter{pageof@pagebonuspoints}

\def\clearme{
\setcounter{numquestions}{0}
\setcounter{numparts}{0}
\setcounter{numsubparts}{0}
\setcounter{numsubsubparts}{0}
%
\set@hlfcntr{numpoints}{0}
\set@hlfcntr{numbonuspoints}{0}
\set@hlfcntr{pointsof@thisquestion}{0}
\set@hlfcntr{bonuspointsof@thisquestion}{0}
%
\set@hlfcntr{@pagepoints}{0}
\set@hlfcntr{@pagebonuspoints}{0}
\setcounter{pageof@pagepoints}{0}
\setcounter{pageof@pagebonuspoints}{0}
%
\set@hlfcntr{latest@points}{0}
\set@hlfcntr{latest@bonuspoints}{0}
}% End of \clearme...

\def\catchme{\indent\par
numquestions: \thenumquestions\par
numparts: \thenumparts\par
numsubparts: \thenumsubparts\par
numsubsubparts: \thenumsubsubparts\par
%
numpoints: \prtaux@hlfcntr{numpoints}\par
numbonuspoints: \prtaux@hlfcntr{numbonuspoints}\par
pointsofthisquestion: \prtaux@hlfcntr{pointsof@thisquestion}\par
bonuspointsofthisquestion: \prtaux@hlfcntr{bonuspointsof@thisquestion}\par
%
pagepoints: \prtaux@hlfcntr{@pagepoints}\par
pagebonuspoints: \prtaux@hlfcntr{@pagebonuspoints}\par
pageofpagepoints: \thepageof@pagepoints\par
pageofpagebonuspoints: \thepageof@pagebonuspoints\par
%
latestpoints: \prtaux@hlfcntr{latest@points}\par
latestbonuspoints: \prtaux@hlfcntr{latest@bonuspoints}\par
\clearme % before another set of questions...
}% End of \catchme...

\makeatother


And this is how to use it so far from the mal-exam.tex file:

%! *latex mal-exam.tex
\usepackage{mal-patch}

\begin{document}
\def\setcode{1}
\begin{questions}
\question[5] First question in Set \setcode.
\end{questions}
\catchme
%%% Next set of questions...
\def\setcode{2}
\begin{questions}
\question[5] First question in Set \setcode.
\end{questions}
\catchme
\end{document}


-