2

Is it possible to add two unrelated variables that hold an integer value?

For example, I have this MWE

\documentclass[addpoints]{exam}
\usepackage{enumitem}
\begin{document}
    
\begin{enumerate}[label=\textbf{\arabic*}\hspace{4mm}]
    \item this is one
    \item this is two
    \item this is three
    
\label{lst:num} % this will give me the number of bullet points I have (in this case its 3)
\end{enumerate}

\begin{questions}

\question[2] this is one
\question[3] this is two 
\question[4] this is three

\end{questions}


Total = \numpoints + \ref{label}  % 9+3 (from our bullet point) = 12 (the result I want to obtain)
\end{document}

In the above example, I would like to add \numpoints and \ref{label} to produce the result 12. Is it possible?

1
  • \the\numexpr\numpoints + \ref{lst:num}\relax? Not a robust method, but indeed possible. Oct 29 '20 at 8:46
6

Here is a robust one:

  • the value of label lst:num is retrieved using \getrefnumber from refcount package and
  • the value of points is retrieved using \numpoints@exp.

Some explanations:

  • The outer most logic is to add two integers, hence we have \the\numexpr <num1>+<num2>\relax, where both <num1> and <num2> should be fully-expandable and leave an integer in input stream.
  • Here <num1> is the value printed by \numpoints, which is defined as
    \def\numpoints{\@ifundefined{exam@numpoints}%
      {\mbox{\normalfont\bfseries ??}}%
      \exam@numpoints
    } 
    
    and is not expandable when \exam@numpoints is undefined or equal to \relax. Hence an expandable variant \numpoints@exp is provided and it leaves 0 when \exam@numpoints is undefined.
  • Here <num2> is the value stored in label lst:num. Similarly, \ref{lst:num} is not expandable when label lst:num is not defined (for example in first run). \getrefnumber{<label>} is the expandable variant of \ref and it also returns 0 when <label> is not defined.

Update:

To use enumitem option label, two things are added:

  • enumitem option ref is used, which restores the plain format of the arabic "value" stored in an item label
  • a wrapper macro \getrefnumber@wrap{<label>} is provided, which strips one level of braces (added by enumitem) around the "returned value" of \getrefnumber.
\documentclass[addpoints]{exam}
\usepackage{enumitem}
\usepackage{refcount}

\makeatletter
\newcommand\getTotalPoints[1]{%
  \the\numexpr\numpoints@exp+\getrefnumber@wrap{#1}\relax
}

% expandable variant of \numpoints
\def\numpoints@exp{%
  \ifcsname exam@numpoints\endcsname\exam@numpoints\else0\fi
}

% strip pair of braces from result of \getrefnumber{<label>}
\def\getrefnumber@wrap#1{%
  \expandafter\getrefnumber@wrap@i\expanded{\getrefnumber{#1}}\@nil
}
\def\getrefnumber@wrap@i#1\@nil{#1}

\def\enit@reflabel#1#2{%
  \ifnum\enit@depth=\@ne\else % no level 0
    \advance\enit@depth\@ne
    \@namedef{p@\@enumctr}{}% Don't accumulate labels
    \advance\enit@depth\m@ne
  \fi
  \ifcase#2%
    \@namedef{the\@enumctr}{#1}%
  \else
    \enit@normlabel{\csname the\@enumctr\endcsname}{#1}%
  \fi}
\makeatother

%\usepackage{unravel}
\providecommand\unravel[1]{#1}
\providecommand\unravelsetup[1]{}
\unravelsetup{max-action=1000, max-input=1000, max-output=1000}


\begin{document}
  
\begin{enumerate}[label=\textbf{\arabic*}\hspace{4mm}, ref=\arabic*]
    \item this is one
    \item this is two
    \item this is three
    
    \label{lst:num} % this will give me the number of bullet points I have (in this case its 3)
\end{enumerate}

\begin{questions}
  \question[2] this is one
  \question[3] this is two 
  \question[4] this is three
\end{questions}

Total = \getTotalPoints{lst:num}
\end{document}

enter image description here

5
  • I noticed that it fails when I modify my``` \begin{enumerate}```. It keeps saying "Missing number, treated as zero. Total = \getTotalPoints{lst:num}" What does that mean?
    – Alan Jones
    Oct 29 '20 at 9:28
  • 1
    @AlanJones I've added some explanations to the answer. Oct 29 '20 at 9:34
  • 1
    @AlanJones Perhaps you can provide that modified example by editing the question. Oct 29 '20 at 9:38
  • I've added [label=\textbf{\arabic*}\hspace{4mm}] after the \begin{enumerate}.
    – Alan Jones
    Oct 29 '20 at 9:42
  • 1
    @AlanJones Answer updated. Oct 29 '20 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.