# Create variable for changing node-names in tikz

I was trying to draw multiple rectangular nodes side by side using

\begin{tikzpicture}[scale=0.675]
\def\size{0.625cm}
\node[rectangle,draw,line width=0.6pt,minimum size=\size,fill=cyan!50!black] (elem_0) at (0,0) {$x_0$};
\def\anchor{elem_0.east};
\foreach \txt in {x,y,z} {
\foreach \idx in {1,2,3} {
\node[rectangle,draw,anchor=west,line width=0.6pt,minimum size=\size,fill=cyan!50!black] (elem_\txt_\idx) at (\anchor) {$\txt_\idx$};

\def\anchor{elem_\txt_\idx.east};
}

\node[anchor=west] (separator_\txt) at (elem_\txt_3.east) {$...$};
\def\anchor{separator_\txt.east};
}
\end{tikzpicture}


but unfortunately it seems that the \def\anchor is not updated inside the foreach-loop.

Is there any other way how to do this without unrolling the loops?

• it helps if you provide a test file not just a fragment but tikz for-each puts each item in a local group, you can probably use \xdef instead of \def to be global and to expand \txt Feb 26 at 9:43
• Thank you! I wasn't aware of xdef - it works perfectly Feb 26 at 10:01

tikz \for-each puts each item in a local group, you can probably use \xdef instead of \def to be global and to expand \txt and \idx before making the definition.