[Tex/LaTex] Using command to compute sum

calculationsmacros

I must admit this sounds like an easy question.

I'm creating a grading model for a homework assignment, and I'm adding the maximum amount of points to each question. Is there a way to have LaTeX print out the sum of these (integer) points automatically?

\points{3} Solve equation [...]
\points{5} Give proof of [...]

Max points: \totalpoints{}

That would then print out "Max points: 8" at the end of the document.

Best Answer

This is actually very simple using a normal LaTeX counter:

\documentclass{article}

\newcounter{points}
\newcommand*{\points}[1]{\addtocounter{points}{#1}}
\newcommand*{\totalpoints}{\thepoints}% Or simply use `\thepoints` directly.

\begin{document}

\points{3} Solve equation [...]
\points{5} Give proof of [...]

Max points: \totalpoints{}

\end{document}
Related Question