# Incomplete \iffalse with \pgfmathsetmacro

I want to generate a table with the \foreach command such that the entries of the table depend on the row and column. I wanted to test the following code:

\documentclass{article}
\usepackage{pgffor,etoolbox}
\newcommand*\tas[2]{
\pgfmathsetmacro{\sat}{#1+#2}
\sat
}
\newcommand*\mytablecontents{}
\foreach \i in {1,...,7}{
\foreach \j in {1,...,7}{
\xappto\mytablecontents{\tas{\i}{\j}}
\gappto\mytablecontents{&}
}
\gappto\mytablecontents{\\}
}
\begin{document}
\begin{tabular}{c|cc|ccc|cc}
\mytablecontents
\end{tabular}
\end{document}


This gives the error "Incomplete \iffalse". I read that the reason for this error is the use of \pgfmathsetmacro. Does anyone know how to handle this?

\pgfmathsetmacro is not expandable, so it is not “evaluated” in an \edef (which is what \xappto does, in the variant \xdef).

You can first compute the sum, storing it into a macro, then append the expanded version:

\documentclass{article}
\usepackage{pgffor,etoolbox}

\newcommand*\tas[2]{%
\pgfmathsetmacro{\sat}{#1+#2}%
}
\newcommand*\mytablecontents{}
\foreach \i in {1,...,7}{%
\foreach \j in {1,...,7}{%
\tas{\i}{\j}%
\xappto\mytablecontents{\sat &}
}%
\gappto\mytablecontents{\\}
}

\begin{document}

\begin{tabular}{c|cc|ccc|cc}
\mytablecontents
\end{tabular}

\end{document}


Alternatively, you can use expandable arithmetic: expl3 provides it, with xfp and \fpeval:

\documentclass{article}
\usepackage{pgffor,etoolbox,xfp}

\newcommand*\mytablecontents{}
\foreach \i in {1,...,7}{%
\foreach \j in {1,...,7}{%
\xappto\mytablecontents{\fpeval{\i+\j}&}
}%
\gappto\mytablecontents{\\}
}

\begin{document}

\begin{tabular}{c|cc|ccc|cc}
\mytablecontents
\end{tabular}

\end{document}


Just to show the capabilities (not all covered by pgfmath is available, though), if you change the \xappto call into

\xappto\mytablecontents{\fpeval{round(\i*sqrt(\j),2)}&}


the output would be

The same result with full expl3 code:

\documentclass{article}
\usepackage{expl3}

\ExplSyntaxOn
\tl_clear:N \l_tmpa_tl
\int_step_inline:nnnn { 1 } { 1 } { 7 }
{ % outer cycle: #1
\int_step_inline:nnnn { 1 } { 1 } { 7 }
{ % inner cycle: ##1
\tl_put_right:Nx \l_tmpa_tl { \fp_eval:n {round(#1*sqrt(##1),2)} & }
}
\tl_put_right:Nn \l_tmpa_tl { \\ }
}
\tl_set_eq:NN \mytablecontents \l_tmpa_tl
\ExplSyntaxOff

\begin{document}

\begin{tabular}{c|cc|ccc|cc}
\mytablecontents
\end{tabular}

\end{document}


Note that \tl_put_right:Nx is the analog of \eappto (x stands for expanded) and \tl_put_right:Nn is the analog of \appto. No need for global definitions, as \int_step_inline:nnnn does not use grouping of cycles: the current index is directly available as #1 (or ##1 in the inner loop).

To avoid using that newcommand, you can calculate the same result using the option [evaluate = \j as \c using int(\i+\j)] of the inner \foreach loop.

\documentclass{article}
\usepackage{pgffor,etoolbox}

\newcommand*\mytablecontents{}
\foreach \i in {0,...,7}{
\foreach \j [evaluate = \j as \c using int(\i+\j)] in {0,...,7}{
\xappto\mytablecontents{\c}
\gappto\mytablecontents{&}
}
\gappto\mytablecontents{\\}
}

\begin{document}

\begin{tabular}{c|cc|ccc|cc}
\mytablecontents
\end{tabular}

\end{document}


You can also use:

\newcommand*\mytablecontents{}
\foreach \i in {0,...,7}{
\foreach \j in {0,...,7}{
\pgfmathtruncatemacro\c{\i+\j}
\xappto\mytablecontents{\c}
\gappto\mytablecontents{&}
}
\gappto\mytablecontents{\\}%
}


\xappto\mytablecontents{\tas{\i}{\j}}


assumes that \tas{.}{.} is expandable. However, since it contains an assignment (\pgfmathsetmacro), it is not. Therefore, you'll have to evaluate (expand) the expression first before inclusing it as part of your appendage to \mytablecontents. Since the expression is fairly straight-forward (the addition of two numbers), you can just use \number\numexpr#1+#2:

\documentclass{article}

\usepackage{pgffor,etoolbox}

\newcommand*\tas[2]{%
\number\numexpr#1+#2\relax
}

\newcommand*\mytablecontents{}
\foreach \i in {1,...,7}{
\foreach \j in {1,...,7}{
\xappto\mytablecontents{\tas{\i}{\j}}
\gappto\mytablecontents{&}
}
\gappto\mytablecontents{\\}
}

\begin{document}

\begin{tabular}{c|cc|ccc|cc}
\mytablecontents
\end{tabular}

\end{document}


Expandable calculation is easily possible via the LaTeX2e interface for \fp_eval from LaTeX3 in the xfp package:

\usepackage{pgffor,xfp,etoolbox}

\newcommand*\mytablecontents{}
\foreach \i in {1,...,7}{
\foreach \j in {1,...,7}{
\xappto\mytablecontents{\fpeval{\i+\j}}
\gappto\mytablecontents{&}
}
\gappto\mytablecontents{\\}
}