4

enter image description here

Let me give you an example of how I started to solve this problem (but without success):

\documentclass[a4paper,12pt]{article}
\usepackage{caption}
\usepackage{colortbl}
\usepackage{ifthen}

\begin{document}
    \begin{table}[ht]
        \caption{Example table with different initial numbers and equal step}
        \centering
        \begin{tabular}{|l|l|l|l|}
            \hline
            A & B & C & D \\
            \hline
            \newcounter{currentA}
            \newcounter{currentB}
            \newcounter{currentC}
            \newcounter{currentD}
            
            \setcounter{currentA}{1}
            \setcounter{currentB}{10}
            \setcounter{currentC}{100}
            \setcounter{currentD}{1000}
            
            \whiledo{\value{currentA} < 21}{
                \thecurrentA &
                \thecurrentB &
                \thecurrentC &
                \thecurrentD \\
                \hline
                \stepcounter{currentA}
                \stepcounter{currentB}
                \stepcounter{currentC}
                \stepcounter{currentD}
            }
        \end{tabular}
    \end{table}
\end{document}
1
  • Could you be interested in a LuaLaTeX-based solution? Please advise.
    – Mico
    Dec 3, 2023 at 8:44

3 Answers 3

2

I define a \makerows command taking three arguments: the first argument is the number of rows; the second one is the list of starting points; the final argument is the list of steps.

The row building function is repeated as many times as specified. At each step, the current sequence of values is stored in the appropriate way; then the new one is computed by adding the steps.

\documentclass[a4paper,12pt]{article}

\ExplSyntaxOn
\NewDocumentCommand{\makerows}{mmm}
 {% #1 = number of rows, #2 = list of initial points, #3 = list of steps
  \tl_clear:N \l__anton_rows_tl
  % store the steps
  \seq_set_from_clist:Nn \l__anton_row_steps_seq { #3 }
  % initialize
  \seq_set_from_clist:Nn \l__anton_row_current_seq { #2 }
  % build the rows
  \prg_replicate:nn { #1 } { \__anton_row: }
  % deliver them
  \tl_use:N \l__anton_rows_tl
 }

\tl_new:N \l__anton_rows_tl
\seq_new:N \l__anton_row_previous_seq
\seq_new:N \l__anton_row_current_seq
\seq_new:N \l__anton_row_steps_seq

\cs_new_protected:Nn \__anton_row:
 {
  % build the current row
  \tl_put_right:Ne \l__anton_rows_tl { \seq_use:Nn \l__anton_row_current_seq { & } }
  \tl_put_right:Nn \l__anton_rows_tl { \\ }
  % compute the next one
  \seq_set_eq:NN \l__anton_row_previous_seq \l__anton_row_current_seq
  \seq_clear:N \l__anton_row_current_seq
  \seq_map_indexed_inline:Nn \l__anton_row_steps_seq
   {
    \seq_put_right:Ne \l__anton_row_current_seq
     {
      \fp_eval:n { ##2 + \seq_item:Nn \l__anton_row_previous_seq { ##1 } }
     }
   }
 }
\ExplSyntaxOff

\begin{document}

\begin{table}[htp]
\centering

\begin{tabular}{|r|r|r|r|}
\hline
\multicolumn{1}{|c|}{A} & 
\multicolumn{1}{c|}{B} &
\multicolumn{1}{c|}{C} & 
\multicolumn{1}{c|}{D} \\
\hline
\makerows{10}{1,0.1,0.2,10}{1,0.01,0.2,2}
\hline
\end{tabular}

\caption{Example table with different initial numbers and different steps}

\end{table}

\end{document}

enter image description here

4

[Edited to respond to followup in comments.]

It's basically an expansion problem. This probably doesn't tell you much; as far as I can figure out, pretty much everything is an expansion problem.

You want tabular to see the contents of the cells along with the & and \\ . So you need to assemble the contents first, expanding and incrementing the counters, and only then let tabular see the result all-at-once.

The easiest way to control expansion these days is to use expl3. Because this syntax is not really intended for use in documents as such, we define a command \ahtoh[<key-value list>] which takes an optional <key-value list>. This allows you to vary the starting values, number of loops and size of steps.

\documentclass[a4paper,12pt]{article}
\usepackage{caption}
% ateb: https://tex.stackexchange.com/a/702971/ i gwestiwn ahtoh: https://tex.stackexchange.com/q/702968/
\ExplSyntaxOn
\tl_new:N \l_ahtoh_fordos_tl
\keys_define:nn { ahtoh }
{
  a .int_set:N = \l_ahtoh_currenta_int,
  b .int_set:N = \l_ahtoh_currentb_int,
  c .int_set:N = \l_ahtoh_currentc_int,
  d .int_set:N = \l_ahtoh_currentd_int,
  loops .int_set:N = \l_ahtoh_loops_int,
  step .int_set:N = \l_ahtoh_step_int,
  a .initial:n = 1,
  b .initial:n = 10,
  c .initial:n = 100,
  d .initial:n = 1000,
  loops .initial:n = 20,
  step .initial:n = 1,  
}
\cs_generate_variant:Nn \int_add:Nn { NV }
\NewDocumentCommand \ahtohnos { O{} }
{
  \keys_set:nn { ahtoh } { #1 }
  \tl_clear:N \l_ahtoh_fordos_tl
  \int_step_inline:nn { \l_ahtoh_loops_int }
  {%
    \tl_put_right:Ne \l_ahtoh_fordos_tl {
      \int_to_arabic:n { \l_ahtoh_currenta_int } \noexpand&
      \int_to_arabic:n { \l_ahtoh_currentb_int } \noexpand&
      \int_to_arabic:n { \l_ahtoh_currentc_int } \noexpand&
      \int_to_arabic:n { \l_ahtoh_currentd_int } \noexpand\\
      \noexpand\hline 
    }
    \int_add:NV \l_ahtoh_currenta_int \l_ahtoh_step_int
    \int_add:NV \l_ahtoh_currentb_int \l_ahtoh_step_int
    \int_add:NV \l_ahtoh_currentc_int \l_ahtoh_step_int
    \int_add:NV \l_ahtoh_currentd_int \l_ahtoh_step_int
  }
  \l_ahtoh_fordos_tl
}
\ExplSyntaxOff
\begin{document}

\begin{table}[ht]
  \caption{Example  table with different initial numbers and equal step}
  \centering
  \begin{tabular}{|l|l|l|l|}
    \hline
    A & B & C & D \\
    \hline
    \ahtohnos
  \end{tabular}
  \begin{tabular}{|l|l|l|l|}
    \hline
    A & B & C & D \\
    \hline
    \ahtohnos[a=-10,b=-100,c=100,d=10,loops=20,step=-5]
  \end{tabular}
\end{table}
\end{document}

tables of stepped integers

If you only want a single table, you could dispense with the variables altogether and simply calculate 0+<loop number>, 9+<loop number> etc. But I'm assuming your real use-case is a bit more complex than the example in your question.

9
  • I can't run your code in MiKTex, TeXworks
    – Антон
    Dec 3, 2023 at 7:25
  • @Антон - When did you last update your MikTeX distribution?
    – Mico
    Dec 3, 2023 at 8:41
  • +1 for "[in TeX,] pretty much everything is an expansion problem". :-)
    – Mico
    Dec 3, 2023 at 9:23
  • I had the July version. Now I have installed a new version and everything is working! Thanks!!!
    – Антон
    Dec 3, 2023 at 11:39
  • Is it possible to make a step that is not equal to one? And the fractional step? For example 0.01 ?
    – Антон
    Dec 3, 2023 at 11:45
4

The same table as shown in egreg's answer can be created with OpTeX by following code:

\def\B{0.1} \def\C{0.2} \def\D{10}
\table{|c|c|c|c|}{\crl
  A & B & C & D \crl
\fornum 1..10 \do{
   #1 & \B & \C & \D 
   \xdef\B{\expr[2]{\B+.01}}% 
   \xdef\C{\expr[1]{\C+.2}}%
   \xdef\D{\expr[0]{\D+2}} \cr}
\crl}

\bye
2
  • No, it's not the same. You don't get automatically the number of columns.
    – egreg
    Dec 4, 2023 at 18:42
  • 1
    It is the same. I referenced the picture of the output, no the code.
    – wipet
    Dec 4, 2023 at 20:49

You must log in to answer this question.

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