1

Desired output: a table with row number and a function of that (say, row number plus one).

1 2
2 3
... ...
10 11

Attempt:

Here's my MWE adapted from Foreach inside a TikZ matrix.

\documentclass[tikz,preview]{standalone}
\usetikzlibrary{matrix}
\usepackage{etoolbox}

\begin{document} \pgfkeys{/pgf/number format/.cd,fixed,int detect,precision=2} \begin{tikzpicture} \let\mymatrixcontent\empty \foreach \myc in {1,...,10}{% \pgfmathparse{\myc+1} \xappto\mymatrixcontent{\expandonce{\myc &}} \xappto\mymatrixcontent{\expandonce{\pgfmathresult\}} } \matrix [matrix of nodes,ampersand replacement=&] { \mymatrixcontent }; \end{tikzpicture} \end{document}

latex forloop matrix

Question: how to drop the decimal part of the right column?

I tried using \pgfkeys{/pgf/number format/.cd,int detect,precision=2} from TikZ's Manual on Number Printing, but that changed nothing. Enclosing \pgfmathresult with \pgfmathprintnumber{} gives unexpected results.

latex forloop matrix error

1 Answers1

1

I'm not sure what your aim is. Here's a fairly general method for outputting values depending on the row index.

\documentclass{article}
\usepackage{siunitx}

\ExplSyntaxOn

\NewDocumentCommand{\formulatable}{mmm} {% #1 = number of rows % #2 = format of the column % #3 = formula to typeset \gnusupporter_formulatable:nnn { #1 } { #2 } { #3 } }

\cs_new:Nn __gnusupporter_formulatable_do:n {} \cs_new:Nn __gnusupporter_formulatable_cycle:n { #1 & __gnusupporter_formulatable_do:n { #1 } \ }

\cs_new_protected:Nn \gnusupporter_formulatable:nnn { \cs_set:Nn __gnusupporter_formulatable_do:n { \fp_eval:n { #3 } } \begin{tabular}{rS[table-format=#2]} \int_step_function:nN { #1 } __gnusupporter_formulatable_cycle:n \end{tabular} }

\ExplSyntaxOff

\begin{document}

\formulatable{10}{2.0}{#1+1} \qquad \formulatable{10}{1.6}{round(exp(#1/10),6)} \qquad \formulatable{10}{1.4}{round(sind(#1),4)}

\end{document}

enter image description here

The second argument should be adjusted once you know the amount of digits in the second column.

We can add also a starting point, so we can typeset the table of sines for angles in degrees from 1 to 45, which would have been very helpful to Ptolemy.

\documentclass{article}
\usepackage{siunitx}

\ExplSyntaxOn

\NewDocumentCommand{\formulatable}{mO{1}mm} {% #1 = number of rows % #2 = starting point % #3 = format of the column % #4 = formula to typeset \gnusupporter_formulatable:nnnn { #1 } { #2 } { #3 } { #4 } }

\cs_new:Nn __gnusupporter_formulatable_do:n {} \cs_new:Nn __gnusupporter_formulatable_cycle:n { #1 & __gnusupporter_formulatable_do:n { #1 } \ }

\cs_new_protected:Nn \gnusupporter_formulatable:nnnn { \cs_set:Nn __gnusupporter_formulatable_do:n { \fp_eval:n { #4 } } \begin{tabular}{rS[table-format=#3]} \int_step_function:nnN { #2 } { #1+#2-1 } __gnusupporter_formulatable_cycle:n \end{tabular} }

\ExplSyntaxOff

\begin{document}

\formulatable{15}{1.4}{round(sind(#1),4)} \quad \formulatable{15}[16]{1.4}{round(sind(#1),4)} \quad \formulatable{15}[31]{1.4}{round(sind(#1),4)}

\end{document}

enter image description here

egreg
  • 1,121,712