# Create table with for loop (\Numpoint loops) and formula

Im trying to get a table added to my exam tex file that shows the grade earned based on all possible amounts of scorable points.

I am running intwo two issues:

1. The amount of \numpoints of the {exam} class should be the amount of loops of the table (after all: points scored will be between 0 and the max amount of points). I cannot use the \numpoints command that is builtin {exam} documentclass in the forloop however without getting errors. (In the MWE I manually entered the \numpoints amount)

2. I cannot use for loop to generate lines of the table.

\documentclass[addpoints,answers]{exam}
\usepackage{tikz}
\usepackage{pgffor}
\usepackage{xfp}

%Workaround for not being able to use exam's builtin \numpoints in the formula (bonus points for workaround)

\newcommand{\manualpoints}{10}

\begin{document}
\begin{questions}
\question[2] A Question
\question[2] A Question
\question[2] A Question
\question[2] A Question
\question[2] A Question
%nb. 10 points earnable
\end{questions}

\foreach \n in {0,...,12 }{ \n \quad \quad  \pgfmathparse{2*\n + \manualpoints }\pgfmathresult  \par }
\hfill \break
\begin{tabular}{|c|c|}
\textbf{score}&\textbf{Grade}\\
the above loop & in this table
\end{tabular}

\end{document}

• Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
– Community Bot
Jan 21, 2022 at 11:49
• Welcome to TeX.SE. It is not clear exactly what you want to do. A solution would be to conditionally build the table contents before the actual tabular. It would be helpful if you composed a MWE including \documentclass and the appropriate packages that provides a small mock up of what you desire. Jan 21, 2022 at 17:36

## 1 Answer

Here is an example of building up a table where the number of rows is variable and the entry in the row is computed via the \Formula macro defined as

#1*\NWeight/\Nunpoints


where #1 is the row number. This yields:

### Code:

\documentclass{article}
\usepackage{tikz}
\usepackage{booktabs}

%% This is based on:
%%     https://tex.stackexchange.com/a/165153/4301
%%
\makeatletter
\newcommand*{\@MyTempTableTokens}{}%
\newtoks\@tabtoks
%%% assignments to \@tabtoks must be global, because they are done in \foreach
\newcommand\AddTableTokens[1]{\global\@tabtoks\expandafter{\the\@tabtoks#1}}
\newcommand\eAddTableTokens[1]{%
%% https://tex.stackexchange.com/a/175573/4301
\protected@edef\@MyTempTableTokens{#1}%
\expandafter\AddTableTokens\expandafter{\@MyTempTableTokens}%
}
%%% variable should always be operated on either always locally or always globally
\newcommand*\ResetTableTokens{\global\@tabtoks{}}
\newcommand*\PrintTableTokens{\the\@tabtoks}
\makeatother

\newcommand*{\NWeight}{1.7}
\newcommand*{\Nunpoints}{10}
\newcommand*{\Formula}[1]{%
\pgfmathparse{#1*\NWeight/\Nunpoints}
\pgfmathprintnumber[fixed, precision=2, fixed zerofill]{\pgfmathresult}%
}

\begin{document}
% Build Required Table
\ResetTableTokens%
\foreach \Entry in {1,...,\Nunpoints} {%
\eAddTableTokens{\Entry & \noexpand\Formula{\Entry} \\}%
}%
\begin{tabular}{cc}
\toprule
$n$ & Value \\
\cmidrule(r){1-1}
\cmidrule(l){2-2}
\PrintTableTokens
\bottomrule
\end{tabular}%
\end{document}

• Thanks a ton peter, exactly what I was looking for. The only challenge I have left is if I can use the exam class' \numpoints for the amount of loops without manually filling the amount in a \newcommand. Jan 21, 2022 at 18:40
• Your welcome. That change should be easily doable. If you get stuck please post a new question on how to get that value directly. Jan 21, 2022 at 18:59