[Tex/LaTex] Adding marks of different questions in exam class

exam

I have generated the following document using exam class.
I would like to add the marks for each of Questions 2, 3 and 4 and write the total either next to underneath the Section II heading.
I know how to get the total marks for all questions, just not a total for questions within a particular section.
enter image description here

Best Answer

Well, you cannot count the marks per section directly, only per page. So I used a counter and add the question points to that counter. Three things: you cannot use half points, you have to update the counter after each question and you have to reset the counter before typesetting the next section.

\documentclass[a4paper,addpoints]{exam}

\renewcommand{\thepartno}{\alph{partno}}
\renewcommand{\partlabel}{(\thepartno)}

\marksnotpoints

\pointsinrightmargin

\newcounter{sectionpoints}\setcounter{sectionpoints}{0}

%% This will remove the points being typeset after the question number and part letter.
%% Use \droppointsatend to typeset points at the end of the current line.
%% If \droppointsatend is called at the question level, the points will
%% be prepended with a \dotfill, otherwise the points will be prepended
%% with a \hfill.
\makeatletter
\@qformatfalse
\def\droppointsatend{%
  \def\level@of@question{question}%
  \leavevmode\unskip\nobreak%
  \ifx\level@of@question\@queslevel%
    \dotfill(\mbox{\totalpoints\ \points})%
  \else%
    \hfill\mbox{[\@points]}%
  \fi%
  \par
%% Make sure we drop the points at the right
\pointsdroppedatright
}

%% Get the total points from the current question as an integer
\def\valuepointsofquestion{\@ifundefined{pointsofq@\romannumeral \arabic{question}}%
  {0}%
  {\csname pointsofq@\romannumeral \arabic{question}\endcsname}%
}%
\makeatother

\begin{document}

\begin{questions}

\uplevel{\textbf{Section I}}
\question Some text \droppointsatend
This question is worth \totalpoints\ \points

\begin{parts}
\part[5] Some text \droppointsatend 
\part[5] Some text \droppoints % \droppointsatend
\part[5] Some text \droppoints % \droppointsatend
\part[5] Some text \droppoints % \droppointsatend
\end{parts}

\addtocounter{sectionpoints}{\valuepointsofquestion}

\question[10] Compute $\displaystyle \int_0^1 \!x^2\,\mathrm{d}x $ \droppointsatend
This question is worth \totalpoints\ \points

\addtocounter{sectionpoints}{\valuepointsofquestion}

\uplevel{The total \points\ for this section is \thesectionpoints.}

\uplevel{\textbf{Section II}}
\setcounter{sectionpoints}{0}

\question[15] Compute $\displaystyle \int_0^\infty\!\mathrm{e}^{-x^2}\,\mathrm{d}x$ \droppointsatend
\addtocounter{sectionpoints}{\valuepointsofquestion}

\question Some text \droppointsatend
\addtocounter{sectionpoints}{\valuepointsofquestion}

\begin{parts}
\part[5] Some text \droppoints % \droppointsatend 
\part[5] Some text \droppoints % \droppointsatend
\part[5] Some text \droppoints % \droppointsatend
\part[5] Some text \droppoints % \droppointsatend
\end{parts}

\uplevel{The total \points\ for this section is \thesectionpoints.}

\end{questions}

\bigskip
\noindent Total to earn: \numpoints\ \points

\end{document}

Screenshot of the code

Related Question