This question comes up pretty often when people want to build tables with empty columns or similarly building up some active characters inside a macro hoping to expand them at the right time right place.
Obviously, things don't work out as expected thanks to the intimate bowel movements of TeX (since we have mouth and stomach already). I've been asked this question in many different forms a lot of times and I think it has enough potential to be a duplicate source, hence the question. I also have a hacky solution but I think our wizards can make things more structured.
Consider the following toy example which has enough germs in it:
TikZ has a \matrix
macro that acts like a tabular
environment, that is, it looks for a row and a column separator and forms a graphical object. It doesn't have to be related to TikZ (see below)
\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\tikz\matrix[execute at empty cell=\node{$\bullet$};]{\themacrotobedefined};
\end{document}
or you can consider
\documentclass{standalone}
\begin{document}
\begin{tabular}{cccc}\themacrotobedefined\end{tabular}
\end{document}
The macro to be defined, \themacrotobedefined
should hold the character stream
&&&\\&&&\\&&&\\
such that we have four columns, three rows (numbers are random choose what you wish). The question is as general as you wish, you can include math chars etc. But just to make it a bit more problematic, please use the following
\foreach\x{1,2,3}{
\foreach\y{1,2,3}{
...
}
}
so that the macro is build up sequentially at each spin (again foreach is optional do/while is also OK). The main goal is how to append sensitive items to macros without expanding them prematurely. plain TeX/LaTeX/ConteXt bring it on.
We have pretty high number of questions which touch this issue tangentially but never head-on so I think it is not a duplicate (unless we find it).
\foreach
or just any double nested loop? As one of the standard answers to this is to use an expandable loop construct, so not that.&
and appends more&
and after a given number of it appends double backslash including recursion is fine for me. The more methods the merrier since I have the hope that we can direct to this question as a reference later.