如何根据计算结果制作循环生成的表?

如何根据计算结果制作循环生成的表?

作为一名荷兰教师,我的评分标准从 1.0 到 10.0。成绩的计算取决于三个变量:N 项;学生的分数;考试总分。

我希望我的考试能够自动生成一个评分表,该表由四个独立的表组成,每个表包含评分标准的特定部分:[1,0;3,2] 和 [3,3;5,4] 和 [5,5;7,7] 和 [7,8;10,0]。

如果 N-term=1,0 且总分(包括零分)可以被四整除,则会产生四个长度相同的表格。在许多其他情况下则不会。请参见下面的示例。

这是 N 项为 0,0 且总共有 47 个点的示例:

在此处输入图片描述

到目前为止,我已经得到了一个可以完成大部分任务的代码。然而问题是,每个分隔表的长度都是在代码中手动确定的。我真的希望这也能自动进行。

一位以前的同学编写了代码;对于大部分内容我都不确定它是如何工作的。

表格的制作采用foreach循环,使用\i的 表示考生的分数,表格的结尾取决于一定分数对应的成绩。

所以也许 a\foreach \i in {0,...,\GetNumPoints} \while 1,0 =< \NormFormule =< 3.2或类似的东西效果更好。但我不知道该怎么做。

有什么建议么?

\documentclass[addpoints]{exam}

\usepackage{tikz}

%-----------------------------------------
% The Nterm determines which grade correspends to which grade; this is normally a number between 0,0 and 2,0
%-----------------------------------------
\newcommand{\Nterm}{1.0}

%-----------------------------------------
% This is some prework that needs to be done
%-----------------------------------------
\makeatletter
   \providecommand{\exam@numpoints}{1}%
   \newcommand{\GetNumPoints}{\exam@numpoints}%
    \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]{% 
        \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*{\NormFormule}[1]{%
    \pgfmathparse{
        \ifdim \Nterm pt < 1 pt
            max(
                9 * ( #1 / \GetNumPoints ) + \Nterm ,
                1 + ( #1 * ( 9 / \GetNumPoints ) * 0.5 ),
                10 - ( \GetNumPoints - #1 ) * ( 9 / \GetNumPoints ) * 2
            )
        \else
            min(
                9 * ( #1 / \GetNumPoints ) + \Nterm ,
                1 + ( #1 * ( 9 / \GetNumPoints ) * 2 ),
                10 - ( \GetNumPoints - #1 ) * ( 9 / \GetNumPoints ) * 0.5
            )       
        \fi
    }
    \pgfmathprintnumber[fixed, precision=1, fixed zerofill, use comma]{\pgfmathresult}}


%-----------------------------------------
% This is the command for making the table; which works fine. Except for needing to manually define the length of the specific tables
%-----------------------------------------
\newcommand{\maketable}{\bigskip
\Huge Grading Table\bigskip\\\normalsize
    \ResetTableTokens
    \foreach \i in {0,...,14}   %THIS IS WHERE I GOT STUCK!
    {\eAddTableTokens{\i & \noexpand\NormFormule{\i} \\}}{}                 
        \begin{minipage}[t]{.23\textwidth}\centering$(1{,}0-3{,}2)$\\       
            \begin{tabular}{|c|c|}
                \hline
                \textbf{score}&\textbf{grade}\\\hline\centering
                \PrintTableTokens
                \hline
            \end{tabular}
        \end{minipage}  
    \ResetTableTokens
    \foreach \i in {15,...,29} {\eAddTableTokens{\i & \noexpand\NormFormule{\i} \\}}
        \begin{minipage}[t]{.23\textwidth}\centering$(3{,}3-5{,}4)$\\
            \begin{tabular}{|c|c|}
                \hline
                \textbf{score}&\textbf{grade}\\\hline\centering
                \PrintTableTokens
                \hline
            \end{tabular}
        \end{minipage}          
    \ResetTableTokens
    \foreach \i in {30,...,44} {\eAddTableTokens{\i & \noexpand\NormFormule{\i} \\}}
        \begin{minipage}[t]{.23\textwidth}\centering$(5{,}5-7{,}7)$\\
            \begin{tabular}{|c|c|}
                \hline
                \textbf{score}&\textbf{grade}\\\hline\centering
                \PrintTableTokens
                \hline
            \end{tabular}
        \end{minipage}      
    \ResetTableTokens
    \foreach \i in {45,...,\GetNumPoints} {\eAddTableTokens{\i & \noexpand\NormFormule{\i} \\}}
        \begin{minipage}[t]{.23\textwidth}\centering$(7{,}8-10{,}0)$\\
            \begin{tabular}{|c|c|}
                \hline
                \textbf{score}&\textbf{grade}\\\hline\centering
                \PrintTableTokens
                \hline
            \end{tabular}
        \end{minipage}
}


%-----------------------------------------
% And here the actual exam begins
%-----------------------------------------

\begin{document}

\begin{questions}
    \question[60]
    The content of this question is irrelevant.
\end{questions}

\maketable

\end{document}

答案1

我使用 expl3 语法重写了整个代码,因为我认为从长远来看这更容易维护。而且,这样事情变得容易一些。我使用该siunitx包来格式化数字。

我认为此代码就是您想要的:它输出四列所需的评分标准。列的长度取决于您可以获得的分数。

\documentclass[addpoints]{exam}
% use siunitx to format numbers
\usepackage{siunitx}
\sisetup{
    round-mode = places,
    round-precision = 1,
    output-decimal-marker = {,}
}

\ExplSyntaxOn

% variable that stores the amount of exam points
\fp_new:N \g_kenadams_exampoints_exampoints_fp
\fp_gset:Nn \g_kenadams_exampoints_exampoints_fp { 1.0 }

% variable that stores the nterm
\fp_new:N \g_kenadams_exampoints_nterm_fp
\fp_gset:Nn \g_kenadams_exampoints_nterm_fp { 1.0 }
% command to change the nterm
\NewDocumentCommand{\SetPointsNterm}{ m }{
    \fp_gset:Nn \g_kenadams_exampoints_nterm_fp { #1 }
}

\makeatletter
% internal command to retrieve the current amount of exam points
% set by the exam class
\cs_new:Npn \kenadams_exampoints_update: {
    \cs_if_exist:cT { exam@numpoints } {
        \fp_gset:Nn \g_kenadams_exampoints_exampoints_fp { \exam@numpoints }
    }
}
\makeatother

% internal command to calculate a score from a given point value
% the logic is taken over from the original code
\cs_new:Npn \kenadams_exampoints_calculatescore:n #1 {
    \fp_eval:n {
        round( \g_kenadams_exampoints_nterm_fp < 1 ? 
            max(
                9 * ( #1 / \g_kenadams_exampoints_exampoints_fp ) + \g_kenadams_exampoints_nterm_fp ,
                1 + ( #1 * ( 9 / \g_kenadams_exampoints_exampoints_fp ) * 0.5 ),
                10 - ( \g_kenadams_exampoints_exampoints_fp - #1 ) * ( 9 / \g_kenadams_exampoints_exampoints_fp ) * 2
            )
        : 
            min(
                9 * ( #1 / \g_kenadams_exampoints_exampoints_fp ) + \g_kenadams_exampoints_nterm_fp ,
                1 + ( #1 * ( 9 / \g_kenadams_exampoints_exampoints_fp ) * 2 ),
                10 - ( \g_kenadams_exampoints_exampoints_fp - #1 ) * ( 9 / \g_kenadams_exampoints_exampoints_fp ) * 0.5
            ) 
        , 1 , 1 )
    }
}

% internal variable (counter) that stores the current table row 
\int_new:N \g_kenadams_exampoints_tablerow_int
% internal command to create a part of the set of tables
\cs_new:Npn \kenadams_exampoints_tablepart:nnn #1 #2 {
    % clearing temporary sequence variable
    \seq_clear:N \l_tmpa_seq 
    % stepwise increase table row number as long as 
    % the calculated score from this value is less than #2
    \fp_do_until:nNnn { \kenadams_exampoints_calculatescore:n { \g_kenadams_exampoints_tablerow_int } } > { #2 } {
        % add formatted table row to sequence variable
        \seq_put_right:Nx \l_tmpa_seq { 
            \int_use:N \g_kenadams_exampoints_tablerow_int & 
            \num { \kenadams_exampoints_calculatescore:n { \g_kenadams_exampoints_tablerow_int } } 
        }
        \int_gincr:N \g_kenadams_exampoints_tablerow_int
    }
    
    \begin{minipage}[t]{.23\textwidth} \centering (\num{#1}--\num{#2}) \\       
        \begin{tabular}{ | c | c | }
            \hline
            \textbf{score} & \textbf{grade} \\ \hline
            % insert the values stored in the sequence variable
            % and add \\ after each item
            \seq_use:Nn \l_tmpa_seq { \\ } \\
            \hline
        \end{tabular}
    \end{minipage}
}

% command to print the grading table
\NewDocumentCommand{\MakePointsTable}{ }{
    % retrieve current amount of exam points
    \kenadams_exampoints_update:
    % reset table row counter
    \int_gzero:N \g_kenadams_exampoints_tablerow_int

    \bigskip
    \Huge Grading ~ Table 
    \bigskip \par
    \normalsize
    
    % typeset the four parts of the grading table
    \kenadams_exampoints_tablepart:nnn { 1.0 } { 3.2 }
    \kenadams_exampoints_tablepart:nnn { 3.3 } { 5.4 } 
    \kenadams_exampoints_tablepart:nnn { 5.5 } { 7.7 }
    \kenadams_exampoints_tablepart:nnn { 7.8 } { 10.0 }
}

\ExplSyntaxOff

%-----------------------------------------
% The Nterm determines which grade correspends to which grade; this is normally a number between 0,0 and 2,0
%-----------------------------------------
\SetPointsNterm{1.0}

\begin{document}

\begin{questions}
    \question[60]
    The content of this question is irrelevant.
\end{questions}

\MakePointsTable

\end{document}

在此处输入图片描述


因为\question[47]你会得到:

在此处输入图片描述


对于\question[47]\SetPointsNterm{0.0}您将获得:

在此处输入图片描述

相关内容