I'm trying to create a table using the foreach loops made available in the pgffor
package. Using the following code, I am able to generate the right contents, but not aligned properly.
\begin{tabular}{c}
\foreach \i in {0,...,7}{
\foreach \j in {0,...,7}{
(\i,\j)
}
}
\end{tabular}
However, as soon as I try to add line endings (see below), ..
\begin{tabular}{c}
\foreach \i in {0,...,7}{
\foreach \j in {0,...,7}{
(\i,\j) \\
}
}
\end{tabular}
I get the following error:
! Missing \endgroup inserted.
<inserted text>
\endgroup
l.237 }
I don't see what I'm doing wrong. Can anyone point it out to me?
Best Answer
Add the contents of the table to a macro before you typeset it:
The code above uses
etoolbox
's\appto<macro>{<stuff>}
which adds<stuff>
to the macro<macro>
. Or rather it uses its brothers\gappto
which adds<stuff>
globally (this is important because\foreach
performs its loop inside a group) and\xappto
which adds<stuff>
globally and also expands<stuff>
before its added to<macro>
. The latter is important because otherwise(\i,\j)
would be added multiple times but outside the loop\i
and\j
have another meaning – you'd get lots of “(ı,ȷ)”.