It sounds counter intuitive. I have an exam sheet with n exercises. The maximum score that a student can have is scoreExi where i varies between 1 and n. For simplicity, I consider n known (say 3 here).
I want to display the total number of points on the first page (the sum of scoreEx1, scoreEx2, scoreEx3).
How to do that?
Here is my MWE but my problem is that I will affect the values of scoreEx1, scoreEx2, scoreEx3 in the middle of the document, i.e AFTER the first page where I want to total to display..
\documentclass[a4paper,11pt,fleqn]{article}
\newcounter{scoreEx1}
\newcounter{scoreEx2}
\newcounter{scoreEx3}
\newcounter{totalEx}
\setcounter{scoreEx1}{0}
\setcounter{scoreEx2}{0}
\setcounter{scoreEx3}{0}
\setcounter{totalEx}{0}
\begin{document}
\setcounter{scoreEx1}{5}
\setcounter{scoreEx2}{10}
\setcounter{scoreEx3}{15}
\addtocounter{totalEx}{\value{scoreEx1}}
\addtocounter{totalEx}{\value{scoreEx2}}
\addtocounter{totalEx}{\value{scoreEx3}}
The maximum score is : \textbf{\the\value{totalEx} points}
[
\end{document}
Normally, the line \setcounter{scoreEx1}{5} comes after the first page. I just gave something to start with.
I'm thinking of a double compilation, if ever it's possible to stock the value between both compilations. I have no idea!
Thanks in advance
Edit: by the way, if there's a smarter way to add the values of scoreExi, I'll appreciate you showing me how to do it. Thanks again
\refstepcounterto expose the counter to\label(and incrementing by one):maximum score: \ref{totalpoints} ... code to set counters ... \refstepcounter{totalEx}\label{totalpoints}. A single command to add up all the counters (and subtract one because of the increment in\refstepcounter):\setcounter{totalEx}{\numexpr\the\value{scoreEx1}+the\value{scoreEx2}+the\value{scoreEx3}-1\relax}. – Marijn Nov 16 '22 at 11:03