# Exam class: Macro for computing subtotal marks

I have a question (code given below) which has subparts. Part (a) is further divided into 3 parts: i, ii and iii, whereas Part (b) does not contain any subparts. The total points awarded to this question is 15 of which Part (a) carries 11 marks and Part (b) 4 marks.

There exists the macro \totalpoints in the exam class package that gives me the 11 + 4 = 15 points for the whole question. However there is no macro to compute the number of points (marks) for Part (a) only. I would like a macro that also sums the marks for Part (a) i.e. 2 + 6 + 3 for the subparts to give 11. As the code is today, I have to manually write \part[11] i.e. I myself must manually do the addition of 2 + 6 + 3. I would like a macro that I can just use as \part[\mymacro] that will be equivalent to \part[11].

\totalpoints should also recognise the macro so that I still get 11 + 4 = 15 as the total points for the question. The macro should perhaps just be visible to one subquestion of a whole question so that it can be used in other subquestions of other questions to count the respective sums overthere.

\documentclass[addpoints,12pt]{exam}

\begin{document}

\pointpoints{mark}{marks}

\begin{questions}

\qformat{Question: \thequestion \dotfill {\bfseries \totalpoints\ marks}}
\question

\begin{parts}

\boxedpoints
\part[11]
\noboxedpoints

Some background words about the first question.\\[-6.5mm]

\begin{subparts}

\subpart[2] Show that John is a boy.

\subpart[6] Show that Jane is a girl.

\subpart[3] Show that Janice is a monkey.

\end{subparts}

\vspace{5mm}

\boxedpoints
\part[4]
\noboxedpoints

My second question is here and it is worth 4 points. There are no subparts.

\end{parts}

\end{questions}

\end{document}


Based on the answer proposed by @John Kormylo in order for the macro to cater for half points as well, the correct output is not obtained. See output below:

We do get 9 1/2 marks as the required sum for the subpart total subtotal. However there are three extras 1/2 inserted between Part (ii) and Part (iii). Moreover now \totalpoints is ignoring the 1/2 marks. The aux file is shown below:

\relax

\gdef\subtotal@i@i{9\half}

\gdef\pointsofq@i{13}

\gdef\bonuspointsofq@i{0}

\PgInfo{question@1}{1}

\PgInfo{question1@object1}{1}

\newlabel{question@1}{{1}{1}}

\PgInfo{part@1@1}{1}

\PgInfo{question1@object2}{1}

\newlabel{part@1@1}{{a}{1}}

\PgInfo{subpart@1@1@1}{1}

\PgInfo{question1@object3}{1}

\newlabel{subpart@1@1@1}{{i}{1}}

\newlabel{firstpoints@onpage@1}{{i}{1}}

\PgInfo{subpart@1@1@2}{1}

\PgInfo{question1@object4}{1}

\newlabel{subpart@1@1@2}{{ii}{1}}

\PgInfo{subpart@1@1@3}{1}

\PgInfo{question1@object5}{1}

\newlabel{subpart@1@1@3}{{iii}{1}}

\PgInfo{part@1@2}{1}

\PgInfo{question1@object6}{1}

\newlabel{part@1@2}{{b}{1}}

\gdef\exam@lastpage{1}

\gdef\exam@lastcoverpage{0}

\gdef\exam@totalpages{1}

\gdef\exam@numpoints{13}

\gdef\exam@numbonuspoints{0}

\gdef\exam@numquestions{1}

\gdef\exam@numparts{2}

\gdef\exam@numsubparts{3}

\gdef\exam@numsubsubparts{0}

\gdef\pointsonpage@i{13}

\gdef\lastpage@withpoints{1}

\gdef\lastpage@withbonuspoints{0}


Compilation is done with TeXLive 2014 using the dvi > ps > pdf workflow.

I added a \subtotal macro which is question and part dependent. It works by storing a number on the aux file, same as \totalpoints (more of less).

Interestingly, this number is ignored by \addpoints.

\documentclass[addpoints,12pt]{exam}

\makeatletter
\newcommand{\subtotal}{\@ifundefined{subtotal@\roman{question}@\roman{partno}}%
{}%
{\csname subtotal@\roman{question}@\roman{partno}\endcsname}}

\new@hlfcntr{part@subtotal}

\let\normal@subparts=\subparts
\let\endnormal@subparts=\endsubparts
\renewenvironment{subparts}% no arguments?
{\set@hlfcntr{part@subtotal}{0}%
\normal@subparts
\let\normal@subpart=\@doitem
\let\@doitem=\my@subpart
}{%
\endnormal@subparts
\ifnum\c@part@subtotal>0
\immediate\write\@auxout{\string\gdef\string\subtotal@\roman{question}@\roman{partno}%
{\prtaux@hlfcntr{part@subtotal}}}%
\fi
}

\newcommand{\my@subpart}[1][\@empty]{%
\ifx\@empty#1\relax
\expandafter\normal@subpart
\else
\ifx\@empty\@details
\else
\fi
\expandafter\normal@subpart\expandafter[\expandafter#1\expandafter]%
\fi
}%
\makeatother

\begin{document}

\pointpoints{mark}{marks}

\begin{questions}

\qformat{Question: \thequestion \dotfill {\bfseries \totalpoints\ marks}}
\question

\begin{parts}

\boxedpoints
\part[\subtotal]

Some background words about the first question.\\[-6.5mm]

\begin{subparts}
\noboxedpoints
\subpart[2] Show that John is a boy.

\subpart[6] Show that Jane is a girl.

\subpart[3 \half] Show that Janice is a monkey.

\end{subparts}

\vspace{5mm}

\boxedpoints
\part[4]
\noboxedpoints

My second question is here and it is worth 4 points. There are no subparts.

\end{parts}

\end{questions}

\end{document}

• Your code works fine. However if the marks in \subpart is not an integer such as 3 but something as 3\half or 3.5:then the macro fails in giving the correct sum. The Exam class recognises the format 3\half for points with fractional parts. – user11206 Apr 6 '18 at 5:15
• I was hoping to avoid the hlfcntr protocol. – John Kormylo Apr 6 '18 at 14:53
• Is there any fix? At times I need to have half marks as well. Thank you. – user11206 Apr 6 '18 at 16:34
• I already editted the solution so use hlfcntr. – John Kormylo Apr 6 '18 at 17:11
• I see some changes in the code but I don't understand how to use hlfcntr. Do I still have to use \subtotal? – user11206 Apr 6 '18 at 17:26