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:
\iis not interpreted as a varible inside\expandafter{}- 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 \&\\
};

\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\foreachloop set a macro to0(\def\mySum{0}), inside the loop use\pgfmathparse{\mySum+\i}\global\let\mysum\pgfmathresult.(By the way, your\exercisepointsmacro needs two more%, after the opening braces{.) – Qrrbrbirlbel Apr 26 '13 at 20:11%many times behind the{, but never understood why. Where can I find the reason? – Dave Apr 26 '13 at 21:17int()or\pgfmathtruncatemacro. In this case,\pgfmathparse{int(\mySum+\i)}is probably easier. The\letis necessary either way. – Qrrbrbirlbel Apr 26 '13 at 21:56