2

I want to draw a matrix inside foreach but the compiling is aborted due to some errors. This is my code:

\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,1}{
    \node[xshift=\x em,yshift=\x em]{\x};
    \matrix[xshift=\x em,yshift=\x em]{
        \node{A};&\node{B};\\
        \node{C};&\node{D};\\
    };
}
\end{tikzpicture}
\end{document}

What's wrong with this code? The node inside the foreach is no problem but the matrix is.

  • 1
    What is the error? take a look at these questions also. https://tex.stackexchange.com/questions/285654/why-is-my-matrix-path-failing-when-put-inside-another-macro?rq=1 https://tex.stackexchange.com/questions/1111/problem-with-defining-shortcuts-for-tikz-matrices https://tex.stackexchange.com/questions/300122/matrix-node-inside-foreach-loop-give-error-when-multiple-columns/ – AJN Apr 21 '16 at 11:56

1 Answers1

4

As mentioned in the links (@ArunDebray @LoopSpace @StefanKottwitz)in my comments above, the ampersand replacement option when passed to matrix node results in a compilable document.

matrix inside tiks foreach

\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,5}{
    \node[xshift=\x em,yshift=\x em]{\x};
    \matrix[xshift=\x em,yshift=\x em,ampersand replacement=\&]{
        \node{A};\&\node{B};\\
        \node{C};\&\node{D};\\
    };
}
\end{tikzpicture}
\end{document}
AJN
  • 932