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

examfor loopforeachtables

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}

Best 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:

enter image description here

References:

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}
Related Question