![使用 for 循环(\Numpoint 循环)和公式创建表格](https://linux22.com/image/444929/%E4%BD%BF%E7%94%A8%20for%20%E5%BE%AA%E7%8E%AF%EF%BC%88%5CNumpoint%20%E5%BE%AA%E7%8E%AF%EF%BC%89%E5%92%8C%E5%85%AC%E5%BC%8F%E5%88%9B%E5%BB%BA%E8%A1%A8%E6%A0%BC.png)
我试图在我的考试 tex 文件中添加一个表格,该表格显示根据所有可能的可得分数计算出的成绩。
我遇到了两个问题:
{exam} 类的 \numpoints 数量应为表格的循环数量(毕竟:得分将介于 0 和最大分数之间)。但是,我无法在 forloop 中使用内置于 {exam} 文档类的 \numpoints 命令,否则会出现错误。(在 MWE 中,我手动输入了 \numpoints 数量)
我无法使用 for 循环来生成表格的行。
\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}
答案1
下面是一个构建表的示例,其中行数是可变的,并且行中的条目通过\Formula
定义为的宏来计算
#1*\NWeight/\Nunpoints
其中#1
是行号。结果为:
参考:
代码:
\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}