# Automatically generate table with content from different environments

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}

• There are a few packages for exercises, such as xsim or exercises, maybe you could try these? They make it very easy to create overview tables. Jul 2, 2019 at 8:54

you can define a label per exercise that stores the points and grab them and the end:

\documentclass[DIN, xcolor=dvipsnames, pagenumber=false, twoside, fontsize=11pt, parskip=half]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage{comment}
\usepackage{array}

\newcounter{NumberExercises} % define exercise counter
\newcounter{PointsExercises} % define points counter
\newcounter{PointsTotal} % define total points counter
\def\NumEx{\stepcounter{NumberExercises}\arabic{NumberExercises}}
\newenvironment{exercise}[2]{\Large\textbf{\NumEx. Task: \normalfont #1 (#2 \ifnum #2=1 point\else points\fi)}%
\setcounter{PointsExercises}{#2}%
\refstepcounter{PointsExercises}%
\label{ExercisesPoints-\alph{NumberExercises}}%
}{}

% ================================= SOLUTIONS ===================================

\def\ifshow{1} % uncomment to always print solutions
\ifdefined\ifshow
\newenvironment{solution}{\textbf{Solution:}\\}{}
\else
\excludecomment{solution}
\fi

\makeatletter
\newcommand{\iflabelexists}[3]{\@ifundefined{r@#1}{#3}{#2}}
\makeatother

\begin{document}

\begin{exercise}{First Exercise}{12}

Calculate 2+2!

\begin{solution}
It's $\sqrt{16}$.
\end{solution}

\end{exercise}

\begin{exercise}{Second Exercise}{32}

Calculate 3+1!

\begin{solution}
It's $\sqrt[4]{256}$.
\end{solution}

\end{exercise}
\stepcounter{NumberExercises}
\begin{exercise}{Fourth Exercise}{11}

Calculate 2-1!

\begin{solution}
It's $\cos(2 \pi)$.
\end{solution}

\end{exercise}

\begin{center}
{
\newcounter{rowno}
\setcounter{rowno}{0}

\renewcommand{\arraystretch}{1.2}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\begin{tabular}{|P{2cm}|>{\stepcounter{rowno}%
\iflabelexists{ExercisesPoints-\alph{rowno}}%
{\ref{ExercisesPoints-\alph{rowno}}%
}{\relax}}P{2cm}|P{2cm}|}
\hline
& \multicolumn{2}{c|}{\#points} \\
Exercise & \multicolumn{1}{c|}{possible}        & achieved       \\ \hline \hline
1       &                &                \\ \hline
2       &                &                \\ \hline
3       &                &                \\ \hline
4       &                &                \\ \hline
5       &                &                \\ \hline \hline
Sum &       \multicolumn{1}{c|}{\arabic{PointsTotal}}        &                \\ \hline
\end{tabular}
}
\end{center}

\end{document}