Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
4  
You may take a look at the exam class. –  Thorsten Donig Nov 21 '11 at 21:01

3 Answers 3

up vote 18 down vote accepted

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}
share|improve this answer

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}
share|improve this answer
5  
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}
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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