13

I'm looking for a simple answer, as I'm not a programmer. Everything I've found online about this is way too dense for my knowledge level.

I teach calculus, and every year the points for each question on the test varies a little. We have multiple professors who all contribute questions, and so the total changes each time. I'd like LaTeX to calculate this automatically so we can use a template for future tests.

Is there a way I can assign the points to each question and have LaTeX automatically give me a total? It seems like this should be child's play, but is apparently not. (I can't tell if that's because what I'm asking for is secretly incredibly difficult or because it's too simple and everyone just knows it automatically.)

I don't want to use some external program (I could just calculate by hand if I were going to do that), I just want to display a total at the top of the exam.

Here's the structure of what I'm imagining:

Total = \calltotal points.

\begin{enumerate}

\item [5 points]\addtototal{5} Here is the first question.

\item [6 points]\addtototal{6} Here is the second.

\end{enumerate}

Nothing more fancy than that! Any help appreciated.

3 Answers3

16

As long as you are just dealing with integer points, you can use a counter to sum them up. As you want to include the sum before they are actually added, the package totcount comes in handy:

\documentclass{article}

\usepackage{totcount}
\newtotcounter{totalpoints}
\setcounter{totalpoints}{0}

\begin{document}

Total points are \the\numexpr\totvalue{totalpoints}

\begin{enumerate}

\item [5 points]\addtocounter{totalpoints}{5} Here is the first question.

\item [6 points]\addtocounter{totalpoints}{6} Here is the second.

\end{enumerate}


\end{document}

enter image description here

  • Fantastic! Thank you so much. That's brilliant. It even lets me do a few nifty extras like subtract a few points (eg for bonus points). Cheers. – user122521 Jan 06 '17 at 20:31
  • Of course there exists a package for this. I don't even know why I still write stuff on my own. – YoungFrog Jan 07 '17 at 13:26
  • One can use total{totalpoints} instead of \the\numexpr\totvalue{totalpoints}, as suggested in https://tex.stackexchange.com/a/544464. – muzimuzhi Z May 15 '20 at 21:11
13

A little bit more fancy than requested by the O.P. (Sam Carter's or Torbjorn's solutions are good as well):

I changed the syntax of \item a little bit, by adding the 2nd optional argument <X> where X is a placeholder for the credit points for this task. Omitting <X> means 0 credits and is neither added nor shown.

\documentclass{article}
\usepackage{totcount}
\usepackage{marginnote}
\usepackage{xparse}

\makeatletter
\let\latex@@item\item

\RenewDocumentCommand{\item}{od<>}{%
  \IfValueTF{#1}{%
    \latex@@item[#1]
  }{%
    \latex@@item%
  }%
  \IfValueT{#2}{%
    \marginnote{\bfseries \raggedleft #2 P.}
    \addtocounter{totalcredits}{#2}%
  }%
}
\makeatother
\newtotcounter{totalcredits}

\begin{document}
There are \total{totalcredits} credits to gain here!

\begin{enumerate}
  \item<17> Foo
  \item<4>  Bar
  \item<1000> More foobar
  \item[A)]<-3> Even more foobar
\end{enumerate}


\end{document}

enter image description here

12

There are several packages and classes that deal with making exams and the like (see https://www.ctan.org/topic/exam). One such package is exsheets, which does something like what you're after, although it is a bit more fancy. You can easily add bonus points as well. Note that two compilations are needed for the total points to be printed, just as with cross references.

Note that exsheets has a lot more features than the very simple example below. You can for examples write up solutions, and print them where they're written, or gathered somewhere else in the document, or not at all. And there are a lot of ways of customizing things. Have a look at the manual, and also questions here on the site.

\documentclass{article}
\usepackage{exsheets}
\SetupExSheets[question]{type=exam}
\begin{document}
Total points are: \totalpoints 
\begin{question}{5+3} % 5 points and 3 bonus points
...
\end{question}

\begin{question}{15}
...
\end{question}

\begin{question}{3}
...
\end{question}
\end{document}

enter image description here

Torbjørn T.
  • 206,688