Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 7 down vote accepted

The nested version looks like:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\let\mymatrixcontent\empty
\newcommand{\row}{%
  \foreach \j in {1,...,10}{
    \foreach \i in {1,2} {%
      \begingroup\edef\x{\endgroup
         \noexpand\gappto\noexpand\mymatrixcontent{ pre-\i-\j \&}}\x
      }%
    \gappto\mymatrixcontent{\\}%
  }
}
\row

\begin{tikzpicture}
\tikzset{every node/.style={minimum size=3mm},
  pre/.style={draw,fill=yellow}}

  \matrix (a) [ampersand replacement=\&,matrix of math nodes, nodes={pre}]{
    \mymatrixcontent
  };
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
Apparently I got the row-column labels in the wrong order –  percusse Jun 19 '12 at 14:12
    
Thanks, that is exactly what I was looking for. This is the first time that I got to tinker with expandafter/noexpand and all the like. Do you know a good introduction on that topic? I don't quite get how the begin-/endgroup part works. The idea is to not overwrite any already existing \x? –  Lemming Jun 25 '12 at 17:29
    
Okay, it is almost what I'm looking for. I would like to set tikz-node-labels. \node[pre](p\i\j){content}. So I inserted \noexpand\node[pre](p\i\j){content} in place of pre-\i-\j. Unfortunately, LaTeX seems to loop infinitely long on that. When I ctrl-C out, It is somewhere inside \pgf@matrix@singal@cell@end. How can I put a \node command in there? –  Lemming Jun 25 '12 at 17:55
    
@Lemming What is content? It will make all the entries content. Also I've used matrix of nodes option so they are already nodes you don't need to add node to the syntax/ –  percusse Jun 25 '12 at 19:54
    
Sorry, I should have been more clear on what I want here. The purpose is to have different node types within that matrix. So I would have, say, five rows of one type and then two rows of another type. Hence, the explicit \node command. content would 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

You shouldn't expand the \\ that you add to your matrix content. If you use \gappto\mymatrixcontent{\\}% (instead of \expandafter\gappto\expandafter\mymatrixcontent\expandafter{\\}%), your example works:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{etoolbox}


\begin{document}
\begin{tikzpicture}[>=latex]
  \tikzstyle{every node}=[minimum size=3mm]
  \tikzset{pre/.style={draw,fill=black,text=white}}
  \let\mymatrixcontent\empty
  \foreach \i in {0,1,2,3,4} {%
    \expandafter\gappto\expandafter\mymatrixcontent\expandafter{\i \&}%
  }
  \gappto\mymatrixcontent{\\}%

  \matrix[matrix of math nodes,%
      nodes = {pre},%
      left delimiter = (,%
      right delimiter = ),
      ampersand replacement=\&] {%
    \mymatrixcontent
  };
\end{tikzpicture}
\end{document} 
share|improve this answer
    
Thanks, that did the trick for the single line version. –  Lemming Jun 25 '12 at 17:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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