We have a cooperate design template for exams. The disadvantage is, that we have to count the point for each task manually. My idea was to this automatically with counters.

What I am looking for:

\task{Java}
Hello World \points{2}
for-each \points{1}

Bla bla \points{2}
Blub blub \points{4}

Foo foo \points{2}
bar bar \points{2}


I want to have a document which looks as follows after compiling:

# Java (3 Points)

Hello World (2 Points)

for-each (1 Point)

# C++ (6 Points)

Bla bla (2 Points)

Blub blub (4 Points)

# Latex (4 Points)

Foo foo (2 Points)

bar bar (2 Points)

My idea was to create a counter within the \task-command, and to use it with th \addtocounter-command within the \points-command. Actually, I did not manage to change the name for each counter dynamically.

Here is a MWE extracted from our template, were we are counting the points manually in the \taskcommand:

\documentclass[12pt,oneside,a4paper,final]{scrartcl}

\newcommand{\points}[1]{
(#1~Points)
}

\begin{flushleft}
\end{flushleft}
{\Large \textbf{#1}}
\leavevmode\hfill ({#2}~Points)\\
}

\begin{document}

Hello World \points{2}

for-each \points{1}

Bla bla \points{2}

Blub blub \points{4}

Foo foo \points{2}

bar bar \points{2}

\end{document}

• That's one of the features provided by the excellent exsheets package. – jub0bs Jun 27 '14 at 10:30
• @Jubobs not quite: exsheets doesn't provide a possibility to add points within the environment and have the sum printed in the title automatically. Until now you have to sum them up yourself. v0.14 will provide a possibility but it is not quite ready to be published (in a few days...) – cgnieder Jun 27 '14 at 10:47
• @Jubobs I stand corrected: there is a possibility already – cgnieder Jun 27 '14 at 10:53
• Now the question remains: can exsheets be used with the template (that we know nothing about...) – cgnieder Jun 27 '14 at 11:07
• You'll need two compilation passes unless you are happy with providing the various parts of a task as arguments so they can be processed and then typeset... – Bordaigorl Jun 27 '14 at 12:05

It looks like that user cslstr and I had the same idea ;-) I used a changed command from a former question of mine to write a label to the .aux file and read it later one using \nameref*.

The closing of a task would be much easier, if it would be inside an environment, however.

\documentclass[12pt,oneside,a4paper,final]{scrartcl}
\usepackage{nameref}

\newcommand{\points}[1]{
(#1~Points)
}

\makeatletter
\newcommand{\WriteCreditsToFile}[1]{%
\immediate\write\@auxout{%
}% End of writing to AUX file
}%
\makeatother

\AtEndDocument{% For the last one only
}%

%

\begin{flushleft}
\end{flushleft}
{\Large \textbf{#1}}
}

\begin{document}

Hello World \points{2}

for-each \points{1}

Bla bla \points{2}

Blub blub \points{4}

Foo foo \points{2}

bar bar \points{2}

\end{document}


Edit Updated version with 0.5 credits also possible and rudimentary \listofcredits

\documentclass[12pt,oneside,a4paper,final]{scrartcl}
\usepackage{nameref}
\usepackage{calculator}%
\usepackage{calc}%
\usepackage{totcount}%
\usepackage{forloop}

\xdef\TotalCredits{0}%
\xdef\SubCredits{0}%

\newcommand{\points}[2]{
\item #1   (#2~Points)
}%

\makeatletter
\newcommand{\WriteCreditsToFile}[1]{%
\immediate\write\@auxout{%
}% End of writing to AUX file
}%
\makeatother
%

\makeatletter
\AtEndDocument{%
\immediate\write\@auxout{%
\string\newlabel{totalcredits}{{\thesection}{\thepage}{\TotalCredits}{}}
}
}
\makeatother

\xdef\SubCredits{0}%
\xdef\CreditsSoFar{0}%
\begin{flushleft}
\end{flushleft}
{\Large \textbf{#1}}
\begin{itemize} % Just for 'better' typography
}{% Close it
\end{itemize}
% Count all credits for the summary%
\xdef\TotalCredits{\CreditsSoFar}%
}%

\makeatletter%

\newcounter{loop@@counter}%

\newcommand{\listofcredits}{%
\begin{tabular}{lll}
Task number & Total credits & Achieved\tabularnewline
\hline
Task \number\value{loop@@counter} & \nameref*{credits::\number\value{loop@@counter}} & \tabularnewline%
} %
& \tabularnewline
\hline
\multicolumn{1}{r}{Total:} & \nameref*{totalcredits} & \tabularnewline%
\end{tabular}
}%
\makeatother

\begin{document}

\listofcredits
\newpage

\points{Hello World}{2.5}
\points{for-each}{2}

\points{Bla bla}{2}
\points{blub blub}{4}

\points{Foo}{2}
\points{bar}{2}

\points{Write a Fortran 54 code to  \LaTeX ;-)}{100}
\points{Write a Babbage machine  code to replace \LaTeX ;-)}{1000}

\end{document}


Edit New version

Since \nameref*{} is not expandable, another approach must be used for summing up etc. The new version writes the total credits per task into an external file which is read afterwards again to complete the total task credits at the top position and for the \listoftask commands.

The code

• works if all tasks are written to the \jobname.credits file
• works if there are true numbers and not mathematical expressions for credits

• requires two runs

• does not work for section wise task numbering

• fails if another task is added somewhere, because the writing of \jobname.credits is done only during the first run. In this case remove \jobname.aux and \jobname.credits, then run again twice.

Of course, I could not check everything and the output formatting etc. is just a proposition, where I borrowed code from the OP answer, which was a direct reaction on my first version, so I think, this is ok.

\documentclass[paper=a4,12pt]{scrbook}

\usepackage{calculator}
\usepackage{morewrites}

\usepackage{etoolbox}%
\usepackage{xcolor}%
\usepackage{calc}%
\usepackage{forloop}%
\usepackage{totcount}%
\usepackage{ifthen}%
\usepackage{tikz}%

\newwrite\CreditsFileHandle%

\newcounter{NumberOfLaTeXRuns}%
\newcommand{\WriteFileThreshold}{2}%

\newcommand{\CounterValue}[1]{%
\ifltxcounter{#1}{%
\number\value{#1}}{%
#1}%
}%

\xdef\tempsum{0}%
\xdef\othersum{0}%

\newcommand{\sumup}[1]{%
\xdef\tempsum{\othersum}%
}%

\makeatletter

\newcommand{\WriteToFile}[2][\CreditsFileHandle]{%
\immediate\write#1{%
#2%
}%
}%

\immediate\closein#1%
}%

\newcommand{\CloseForWriting}[1][\CreditsFileHandle]{%
\immediate\closeout#1%
}%

\immediate\openin#1=#2%
}%

\newcommand{\OpenForWriting}[2][\CreditsFileHandle]{%
\immediate\openout#1=#2%
}%

\xdef\@@TotalCreditsValue{1}%

\newcommand{\DisplayQuestionAndCredits}[2]{%
\item #1 (\DisplayCredits{#2})%
}%

\newcommand{\CreditsName}{point}%
\newcommand{\CreditsNamePlural}{points}%

\newcommand{\DisplayCredits}[1]{%
\pgfmathequal{#1}{1}%
\ifnumequal{\pgfmathresult}{1}{
#1 \CreditsName%
}{%
#1 \CreditsNamePlural%
}%%
}%

#2\sumup{#2}%
}%

}%

\ifnumequal{\CounterValue{NumberOfLaTeXRuns}}{\WriteFileThreshold}{%
}{}%
\begin{flushleft}
\hfill  \fcolorbox{black}{yellow}{\DisplayCredits{\@@TotalCreditsValue}}%
\end{flushleft}%
\begin{itemize}
}{%
\end{itemize}
\ifnumless{\CounterValue{NumberOfLaTeXRuns}}{\WriteFileThreshold}{%
%Do only sum up when not in final output mode
\sumuplist{\CreditsSumList}%
\WriteToFile{\tempsum}%
}{% No, the number of runs is greater than zero, so ... do nothing
}%
\xdef\CreditsSumList{}%
}%

\newcommand{\sumuplist}[1]{%
\xdef\tempsum{0}%
\forlistloop{\sumup}{#1}%
}%

\newcounter{last@@counter}%
\newcounter{loop@@counter}%

\newcommand{\mk}[1]{%
}%

\gdef\CurrentSumValue{0.0}%
\IfFileExists{\jobname.credits}{%
\CloseForWriting%
\begin{center}

\vspace{\baselineskip} %
\begin{tabular}{|c||r|c|c|}
\hline
Task number & Total credits & Achieved credits\\
\hline
\hline
\CounterValue{loop@@counter} & %
\ReadCreditValueWithSumUp{\CurrentSumValue}% Do not delete that % character before!
& %
\ifnum\number\value{loop@@counter}<\CounterValue{last@@counter}%  %\number\value{last@@counter}
\tabularnewline\hline
\fi%
\xdef\CurrentSumValue{0}%
}%
\tabularnewline%
\hline%
\hline\hline%
\multicolumn{1}{|l||}{Total:} & \tempsum & \tabularnewline%
\hline%
\end{tabular}%
\end{center}%
\newpage% Well, later an \clearpage?
% Now close the handle than open it again for reading total values in the individual tasks to complete
%the values.
}{}%
}%

\AtBeginDocument{%
\refstepcounter{NumberOfLaTeXRuns}%
\ifnumequal{\CounterValue{NumberOfLaTeXRuns}}{\WriteFileThreshold}{%
\CloseForWriting%
}{%
\OpenForWriting{\jobname.credits}%
}%
}%

\AtEndDocument{%
\immediate\closeout\CreditsFileHandle%
\ifnumequal{\CounterValue{NumberOfLaTeXRuns}}{\WriteFileThreshold}{%
\setcounter{NumberOfLaTeXRuns}{1}}{}%
\immediate\write\@auxout{%
\string\setcounter{NumberOfLaTeXRuns}{\number\value{NumberOfLaTeXRuns}}%
}%
}%
\makeatother

\begin{document}

\DisplayQuestionAndCredits{Hello World in Java}{10}%

\DisplayQuestionAndCredits{Virtual methods}{10}%

\DisplayQuestionAndCredits{Reproduce this document ;-)}{2000}%
\DisplayQuestionAndCredits{Are there better sites than TeX.SX?}{10000.14159}%

\DisplayQuestionAndCredits{Write a Fortran program better than \LaTeXe }{100}
\DisplayQuestionAndCredits{Write code for a Babbage machine with objects}{200}

\end{document}


• I did something like this (with an environment) 5 min ago. (see comment under my question and under cslstr's answer) – Dennis Jun 27 '14 at 12:21
• Well, it was not working then? I am just working on the environment version... – user31729 Jun 27 '14 at 12:23
• It looks like my env-version works :) – Dennis Jun 27 '14 at 12:24
• @Dennis: So all efforts by us were useless ;-) – user31729 Jun 27 '14 at 12:25
• Not really, I just found a way (quite similar to yours) in the same moment. You're versions are "similar", but different. I am still learning from you guys! – Dennis Jun 27 '14 at 12:26

With a current version of exsheets the package provides a possibility for this. exsheets has a command \addpoints that prints and saves the points for the current {question} environment. The sum of points of a question is available as a question property named points that can be retrieved via \GetQuestionProperty{<property>}{<question ID>}

In combination with exsheets' possibility to create custom headings instances and the command \CurrentQuestionID it is possible to define a heading type that prints the sum of the points saved with \addpoints in the {question} body.

\documentclass{article}
\usepackage{exsheets}

% write the sum of points after the title of the question:
title-post-code =
\bfseries~(\GetQuestionProperty{points}{\CurrentQuestionID} Points) ,
% print the title:
attach = { main[l,vc]title[l,vc](0pt,0pt) }
}

% use the newly defined heading instance:
\SetupExSheets{
points/name = Point/s
}

\begin{document}

\begin{question}[name=Java]
\begin{enumerate}
\end{enumerate}
\end{question}

\begin{question}[name=C++]
\begin{enumerate}
\end{enumerate}
\end{question}

\begin{question}[name=\LaTeX]
\begin{enumerate}
\end{enumerate}
\end{question}

\end{document}


The spacing looks a bit weird but that could be adjusted, e.g., by adding below = .5\baselineskip to the definition of the headings instance.

With an even more recent version of exsheets (v0.14 2014/06/28, it's already on CTAN and in TeX Live 2014) it is possible with a little bit of effort to define a grading table. The trick is to use question properties. For this I use the properties subtitle, counter and points that are already defined by exsheets. The commands \GetQuestionProperty{<property>}{<question id>} and \ForEachQuestion{<code where #1 refers to the question id>} I loop through all questions and retrieve the needed data:

\documentclass{article}
\usepackage{exsheets}[2014/06/28]

% a custom headings instance that uses the subtitle' property:
subtitle-format = \bfseries ,
subtitle-post-code =
\bfseries~(\GetQuestionProperty{points}{\CurrentQuestionID} Points) ,
join = {
title[r,B]number[l,B](.333em,0pt) ;
title[r,B]subtitle[l,B](.333em,0pt)
} ,
attach = { main[l,vc]title[l,vc](0pt,0pt) } ,
below  = .5\baselineskip
}

% use the new instance, other settings:
\SetupExSheets{
points/name = Point/s
}

% define a grading table that retrieves subtitles and points:
\begin{tabular}{|l|*{3}{c|}c|}\hline
\ForEachQuestion{%
\GetQuestionProperty{counter}{##1}
\GetQuestionProperty{subtitle}{##1}\iflastquestion{}{&}%
}
& Total \\ \hline
Points   &
\ForEachQuestion{\GetQuestionProperty{points}{##1}\iflastquestion{}{&}} &
\pointssum* \\ \hline
Reached  &
\ForEachQuestion{\iflastquestion{}{&}} &
\\ \hline
\end{tabular}%
}

\begin{document}

\begin{center}
\end{center}

\begin{question}[subtitle=Java,ID=Java]
\begin{enumerate}
\end{enumerate}
\end{question}

\begin{question}[subtitle=C++,ID=C++]
\begin{enumerate}
\end{enumerate}
\end{question}

\begin{question}[subtitle=\LaTeX,ID=LaTeX]
\begin{enumerate}
\end{enumerate}
\end{question}


This answer leverages the existing \labeling capabilities to write the totals out to the aux file. Then the label value can be used with \ref{} where needed.

As mentioned in the comments, two passes will be required for it to work properly.

Also note, \AtEndDocument is used to call the final \closetask command. This could maybe cause problems if you don't ever use \task... this could be either put into a conditional with a flag that is set on first use of \task, or \task could be turned into an environment to use \begin{task}...\end{task}. Using an environment may actually make more sense semantically, though sub-tasks would not be supported out of the box.

\documentclass[12pt,oneside,a4paper,final]{scrartcl}

% Create a new counter to keep track of points for each task

% Create \pointslabel{}{} for saving the points total as a label
\makeatletter
\newcommand{\pointslabel}[2]{%
\protected@write \@auxout {}{\string \newlabel {#1}{{#2}{\thepage}{#2}{#1}{}} }%
\hypertarget{#1}{#2}
}}{%
\newcommand{\pointslabel}[2]{%
\protected@write \@auxout {}{\string \newlabel {#1}{{#2}{}}}}}
\makeatother

\usepackage{chngcntr}

\newcommand{\points}[1]{
(#1~Points)
}

\begin{flushleft}
\end{flushleft}
{\Large \textbf{#1}}
% Addition of points with \ref to label
}

\begin{document}

Hello World \points{2}

for-each \points{1}

Bla bla \points{2}

Blub blub \points{4}

Foo foo \points{2}

bar bar \points{2}

\end{document}


This improved version is based on the answer by Christian Huper:

• it has a more fancy table to print the list of tasks (fixed line wrapping in the table)
• it prints "1 point" instead of "1 points"

\documentclass[12pt,oneside,a4paper,final]{scrartcl}
\usepackage{nameref}
\usepackage{calculator}%
\usepackage{calc}%
\usepackage{totcount}%
\usepackage{forloop}
\usepackage{tikz}

\xdef\TotalCredits{0}%
\xdef\SubCredits{0}%

\newcommand{\printpoints}[1]{#1~\pgfmathequal{#1}{1}\ifnum\pgfmathresult=1 point\else points\fi}

\newcommand{\points}[2]{
\item #1 (\printpoints{#2})
}%

\makeatletter
\newcommand{\WriteCreditsToFile}[1]{%
\immediate\write\@auxout{%
}% End of writing to AUX file
}%
\makeatother
%

\makeatletter
\AtEndDocument{%
\immediate\write\@auxout{%
\string\newlabel{totalcredits}{{\thesection}{\thepage}{\TotalCredits}{}}
}
}
\makeatother

\xdef\SubCredits{0}%
\xdef\CreditsSoFar{0}%
\begin{flushleft}
\end{flushleft}
{\Large \textbf{#1}}
\begin{itemize} % Just for 'better' typography
}{% Close it
\end{itemize}
% Count all credits for the summary%
\xdef\TotalCredits{\CreditsSoFar}%
}%

\makeatletter%

\newcounter{loop@@counter}%

\newcounter{last@@counter}%

\begin{center}
\begin{tabular}{|c||r|c|c|}
\hline
Task number & Total credits & Achieved credits\\
\hline
\hline
\number\value{loop@@counter} & \nameref*{credits::\number\value{loop@@counter}} Points &
\ifnum\number\value{loop@@counter}<\number\value{last@@counter}
\tabularnewline\hline
\fi
}
\tabularnewline
\hline
\hline\hline
Total: & \nameref*{totalcredits} Points & \tabularnewline%
\hline
\end{tabular}
\end{center}
}%

\makeatother

\begin{document}

\newpage

\points{Hello World}{1}

\points{for-each}{0.5}
\points{blub blub}{0.5}


• Sorry about the delay, but you mentioned an issue with \nameref? – user31729 Jul 10 '14 at 12:16
• Now I know what you mean: The problem is that \nameref* is not expandable, in this case \pgfmathresult contains the command \nameref* but not the reference value. I am working on different solution, but it takes time! – user31729 Jul 12 '14 at 15:14