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.

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.