I am using a very simple custom environment to create exercises and their solutions. Each exercise yields a individual number of points. The number of points is defined in this way:
% First argument: title, Second argument: number of points
\begin{exercise}{First Exercise}{12}
Calculate 2+2!
\begin{solution}
It's $\sqrt{16}$.
\end{solution}
\end{exercise}
I now want to create a table giving an overview about all exercises. One column is considered to show to number of points.
\begin{center}
{
\renewcommand{\arraystretch}{1.2}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\begin{tabular}{|P{2cm}|P{2cm}|P{2cm}|}
\hline
& \multicolumn{2}{c|}{\#points} \\
Exercise & possible & achieved \\ \hline \hline
1 & 12 & \\ \hline
2 & & \\ \hline
3 & & \\ \hline
4 & & \\ \hline
5 & & \\ \hline \hline
Sum & 90 & \\ \hline
\end{tabular}
}
\end{center}
Until now everything is static, meaning that any change of an exercise requires a manual change in the table. For instance, if the first exercise now yields 20 points instead of 12, I would need to manually adjust the table.
Is it possible to automatically generate the above table by collecting all exercises and their number of points? The layout of the table does not change.
Here is a full code example:
\documentclass[DIN, xcolor=dvipsnames, pagenumber=false, twoside, fontsize=11pt, parskip=half]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{comment}
\usepackage{array}
\newcounter{NumberExercises} % define exercise counter
\def\NumEx{\stepcounter{NumberExercises}\arabic{NumberExercises}}
\newenvironment{exercise}[2]{\Large\textbf{\NumEx. Task: \normalfont #1 (#2 \ifnum #2=1 point\else points\fi)}}{}
% ================================= SOLUTIONS ===================================
\def\ifshow{1} % uncomment to always print solutions
\ifdefined\ifshow
\newenvironment{solution}{\textbf{Solution:}\\}{}
\else
\excludecomment{solution}
\fi
\begin{document}
\begin{exercise}{First Exercise}{12}
Calculate 2+2!
\begin{solution}
It's $\sqrt{16}$.
\end{solution}
\end{exercise}
\begin{center}
{
\renewcommand{\arraystretch}{1.2}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\begin{tabular}{|P{2cm}|P{2cm}|P{2cm}|}
\hline
& \multicolumn{2}{c|}{\#points} \\
Exercise & possible & achieved \\ \hline \hline
1 & 12 & \\ \hline
2 & & \\ \hline
3 & & \\ \hline
4 & & \\ \hline
5 & & \\ \hline \hline
Sum & 90 & \\ \hline
\end{tabular}
}
\end{center}
\end{document}
xsim
orexercises
, maybe you could try these? They make it very easy to create overview tables.