# Automated table for excercise points in tikz

I'm a computer science student. And I write most of my excercises in LaTeX. So I often have to create a table for excercise points, with different numbers of excercises and different amounts of points for each excercise.

Therefor I want to define a command to do this very quickliy. The idea is to provide a command \excercisepoints{4,5,3} which would print a table table containing space for 3 excercises. Where the first gives 4 points, the seccond 5 points, and so on.

As well I want a row for the sum of points. Because I really like TikZ I decided to do this in TikZ.

By using these three questions (1, 2, 3) I came to this:

\documentclass[class=article]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{etoolbox}
\usetikzlibrary{matrix}
\tikzset{
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={
rectangle,
draw=black,
align=center
},
minimum height=1.5em,
text depth=0.5ex,
text height=2ex,
nodes in empty cells,
%%
row 1/.style={
nodes={
fill=black,
text=white,
font=\bfseries
}
}
}
}
\begin{document}
\newcommand{\excercisepoints}[1]{
\let\mymatrixcontent\empty
\foreach[count=\c] \i in {#1} {
\expandafter\gappto\expandafter\mymatrixcontent\expandafter{\c \& \i \& \\}%
}
\begin{tikzpicture}
\matrix (first) [ampersand replacement=\&, table,text width=6em]
{
Excercise \& Maximal \& Achieved\\
\mymatrixcontent
$\sum$ \& \&\\
};
\end{tikzpicture}}
\excercisepoints{2,4,3}
\end{document}


This yields to this result:

So there are two problems:

1. \i is not interpreted as a varible inside \expandafter{}
2. I don't know how to sum up the numbers. I already tried this with tikz. But I just found commands for summing up dimensions, not natural numbers.

EDIT:

For anybody watching these later, this is the working macro:

\RequirePackage{tikz}
\RequirePackage{etoolbox}
\usetikzlibrary{matrix}
\tikzset{
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={
rectangle,
draw=black,
align=center
},
minimum height=1.5em,
text depth=0.5ex,
text height=2ex,
nodes in empty cells,
%%
row 1/.style={
nodes={
fill=black,
text=white,
font=\bfseries
}
}
}
}
\newcommand{\punkteliste}[1]{%
\let\mymatrixcontent\empty
\def\mySum{0}
\foreach[count=\c] \i in {#1} {%
\xappto\mymatrixcontent{\c \noexpand\& \i \noexpand\& \noexpand\\}%
\pgfmathparse{int(\mySum+\i)}\global\let\mySum\pgfmathresult
}
\begin{tikzpicture}
\matrix (first) [ampersand replacement=\&, table,text width=6em]
{
Excercise \& Maximal \& Achieved\\
\mymatrixcontent
$\sum$ \& \mySum \&\\
};

-
Does \xappto\mymatrixcontent{\c \noexpand\& \i \noexpand\& \noexpand\\}% do what you want? For the sum, you can either use PGF math or (La)TeX count(er)s, if it's just summarizing integers. For PGF math: Before the \foreach loop set a macro to 0 (\def\mySum{0}), inside the loop use \pgfmathparse{\mySum+\i}\global\let\mysum\pgfmathresult.(By the way, your \exercisepoints macro needs two more %, after the opening braces {.) –  Qrrbrbirlbel Apr 26 '13 at 20:11
@Qrrbrbirlbel Thanks to you. My first problem is solved and the seccond neraly. The last remaining problem is, that for the above example 9.0 instead of 9 is the result. If you can solve this last problem and explain why this works (and mine not), then I would accept this as an answer. Aswell I saw the %many times behind the {, but never understood why. Where can I find the reason? –  Dave Apr 26 '13 at 21:17
Ah, yes, you will need to use either int() or \pgfmathtruncatemacro. In this case, \pgfmathparse{int(\mySum+\i)} is probably easier. The \let is necessary either way. –  Qrrbrbirlbel Apr 26 '13 at 21:56
@Qrrbrbirlbel Yes, this is exactly what I wanted. Thank you very much. Anyway, I would really like to learn more about the usage of \let, \def, ... And other things which are usefull when defining commands in LaTeX. Is there any good tutorial? –  Dave Apr 26 '13 at 21:59

In addition to my comments, there is also the /pgf/number format/int detect option for \pgfmathprintnumber.

## Code

\documentclass[tikz]{standalone}
\usepackage{etoolbox}
\usetikzlibrary{matrix}
\tikzset{
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={
rectangle,
draw=green,
text width=6em,
minimum width=6em+0*\pgfkeysvalueof{/pgf/inner xsep},
align=center
},
minimum height=1.5em,
text depth=0.5ex,
text height=2ex,
nodes in empty cells,
%%
row 1/.style={
nodes={
fill=black,
text=white,
%                font=\bfseries
}
}
}
}

\newcommand{\excercisepoints}[1]{%
\let\mymatrixcontent\empty
\def\mySum{0}%
\foreach[count=\c] \i in {#1} {%
\xappto\mymatrixcontent{\c \noexpand\& \i \noexpand\& \noexpand\\}%
\pgfmathparse{\mySum+\i}
\global\let\mySum\pgfmathresult
}%
\begin{tikzpicture}
\matrix (first) [ampersand replacement=\&, table]
{
\bfseries Excercise \& \bfseries Maximal \& \bfseries Achieved\\
\mymatrixcontent
$\sum$ \& \pgfmathprintnumber[int detect]{\mySum} \&\\
};
\end{tikzpicture}}
\begin{document}
\excercisepoints{2,4,3}
\end{document}


-