# Using command to compute sum?

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.

-
You may take a look at the exam class. –  Thorsten Donig Nov 21 '11 at 21:01

This is actually very simple using a normal LaTeX counter:

\documentclass{article}

\newcounter{points}
\newcommand*{\totalpoints}{\thepoints}% Or simply use \thepoints directly.

\begin{document}

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

Max points: \totalpoints{}

\end{document}

-

I'm not sure if this is interesting at all, and it's complete overkill. It is just a very simple example of using Lua code in a LaTeX document.

Compile with lualatex.

\documentclass{article}
\usepackage{luacode}
\newcommand\points[1]{%
\luaexec{
if a == nil then
a = #1
else
a = a + #1
end
}}
\newcommand\totalpoints{\luaexec{tex.sprint(a)}}

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

Max points: \totalpoints{}
\end{document}

-
Examples like this will gain new LuaTeX fans. Really. –  egreg Nov 21 '11 at 22:28

The exam class provides the command \numpoints which will calculate the total points for you.

See the documentation for full details- a MWE follows

\documentclass[addpoints]{exam}

\begin{document}

%\pointsdroppedatright          % try uncommenting this line
%\pointpoints{point}{points}

\cfoot{\footnotesize Page \thepage {} of \numpages}
\lfoot{\footnotesize TOTAL POINTS : \numpoints\ }

This exam has \numquestions\ questions, for a total of \numpoints\ points. The number of points for each  part is given in brackets.

\begin{questions}
%+++++++++++++++++++++++++
%   QUESTION
%+++++++++++++++++++++++++
\question[10] What is your favourite fruit?

\vspace{\fill}

%+++++++++++++++++++++++++
%   QUESTION
%+++++++++++++++++++++++++
\question[23] What is the answer?

\vspace{\fill}
\droppoints

\end{questions}
\end{document}

-
This is nice because it allows you to state the number of points before the questions. However, I have a bit too much other markup and environments to conform to the exam template, so I picked the simple solution. –  noio Nov 24 '11 at 12:20
@Noio: No problem! It's well worth investigating the exam class for future projects :) –  cmhughes Nov 24 '11 at 16:08