Apparently this question has been asked a hundred times already. But I couldn't get it to work with any of the answers provided.
I want to have nested for loops inside a Tikz matrix. Something like:
\begin{tikzpicture}[>=latex]
\tikzstyle{every node}=[minimum size=3mm]
\tikzset{pre/.style={draw,fill=black}}
\matrix[matrix of math nodes,%
left delimiter = (,%
right delimiter = )] {%
\foreach \i in {0,..,10} {%
\foreach \j in {0,1} {%
\node[pre](pre\i\j){}; &
}
\\
}
}
\end{tikzpicture}
Thanks to this and a couple of other posts I know that this cannot work.
So, I tried to translate it accordingly. But I cannot even get it to work with a simple non-nested loop:
\begin{tikzpicture}[>=latex]
\tikzstyle{every node}=[minimum size=3mm]
\tikzset{pre/.style={draw,fill=black}}
\let\mymatrixcontent\empty
\foreach \i in {0,1,2,3,4} {%
\expandafter\gappto\expandafter\mymatrixcontent\expandafter{\i \&}%
}
\expandafter\gappto\expandafter\mymatrixcontent\expandafter{\\}%
\matrix[matrix of math nodes,%
nodes = {pre},%
left delimiter = (,%
right delimiter = ),
ampersand replacement=\&] {%
\mymatrixcontent
};
\end{tikzpicture}
Latex fails with the extremely helpful message
! Missing } inserted.
<inserted text>
}
l.345 \end{tikzpicture}
What am I doing wrong? Is there an easier way to accomplish this?
Cheers!


\x? – Lemming Jun 25 '12 at 17:29\node[pre](p\i\j){content}. So I inserted\noexpand\node[pre](p\i\j){content}in place ofpre-\i-\j. Unfortunately, LaTeX seems to loop infinitely long on that. When Ictrl-Cout, It is somewhere inside\pgf@matrix@singal@cell@end. How can I put a\nodecommand in there? – Lemming Jun 25 '12 at 17:55content? It will make all the entriescontent. Also I've usedmatrix of nodesoption so they are already nodes you don't need to add node to the syntax/ – percusse Jun 25 '12 at 19:54\nodecommand.contentwould most likely be empty, so that it's just a white, or grey box. Additionally, I would like to be able to connect some of those boxes with arrows. Hence, the labels(p\i\j). So far I've been more of a latex user, than a programmer. Do you know a good introduction on these things? – Lemming Jun 26 '12 at 11:17\gapptoin it. – Lemming Jun 26 '12 at 14:47etoolboxandtikz. Normally you don't need such tricks. – percusse Jun 26 '12 at 19:04