请考虑以下示例:
\documentclass{article}
\usepackage{booktabs}
\usepackage[locale = DE]{siunitx}
%\usepackage{multido}
\ExplSyntaxOn
\cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff
\DeclareSIUnit\kroner{kr.}
\newcommand*\skyld[1]{
\calc{((1+\rente)^(#1)*(\rente*\start-\afbetaling)+\afbetaling)/\rente}
}
\begin{document}
\def\start{612600}
\def\afbetaling{68199}
\def\rente{0.02}
\begin{tabular}{
S[table-format = 2]
S[table-format = 6.2, round-integer-to-decimal, round-mode = places, round-precision = 2]
}
\toprule
{$n$} & {$X(n)$} \\
{---} & {\si{\kroner}} \\
\midrule
% \multido{\i = 0+1}{11}{\i & \skyld{\i}\\} <-- is it possible to use something like this?
0 & \skyld{ 0} \\
1 & \skyld{ 1} \\
2 & \skyld{ 2} \\
3 & \skyld{ 3} \\
4 & \skyld{ 4} \\
5 & \skyld{ 5} \\
6 & \skyld{ 6} \\
7 & \skyld{ 7} \\
8 & \skyld{ 8} \\
9 & \skyld{ 9} \\
10 & \skyld{10} \\
\bottomrule
\end{tabular}
\end{document}
\multido
是否可以使用或类似方法生成表中的所有行?
答案1
您可以以宏的形式在外部构造表的内部部分tabular
,然后只需调用宏即可将其设置为表的构造。
通过全局扩展定义保护“敏感元素”(&
和)的不当扩展。每次迭代时,都会将新行添加到定义中。然后,在构造时,的替换文本将设置行。\\
\protected@xdef
\multido
\skyIDtable
tabular
\skyIDtable
\documentclass{article}
\usepackage{booktabs,multido}
\usepackage[locale = DE]{siunitx}
\ExplSyntaxOn
\cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff
\DeclareSIUnit\kroner{kr.}
\newcommand*\skyld[1]{
\calc{((1+\rente)^(#1)*(\rente*\start-\afbetaling)+\afbetaling)/\rente}
}
\begin{document}
\newcommand{\start}{612600}
\newcommand{\afbetaling}{68199}
\newcommand{\rente}{0.02}
\makeatletter
\newcommand{\skyIDtable}{}% Empty table
\multido{\i = 0+1}{11}{\protected@xdef\skyIDtable{\skyIDtable \i & \skyld{\i}\\}}% <-- Construct rows for table
\makeatother
\begin{tabular}{
S[table-format = 2]
S[table-format = 6.2, round-integer-to-decimal, round-mode = places, round-precision = 2]
}
\toprule
{$n$} & {$X(n)$} \\
{---} & {\si{\kroner}} \\
\midrule
\skyIDtable% Set table rows
\bottomrule
\end{tabular}
\end{document}