6

Consider the following example:

\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}

output

Is it possible to generate all the rows in the table by using \multido or something similar?

1

1 Answer 1

5

You can construct the inner part of the table outside the tabular in the form of a macro, and then set it with the construction of the table by just calling the macro.

Improper expansion of "sensitive elements" (& and \\) are protected via \protected@xdef - a global expansion definition. With each iteration of \multido, a new row is added to the definition of \skyIDtable. Then, upon construction of the tabular, \skyIDtable's replacement text sets the rows.

enter image description here

\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}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .