Graphics, Figures & TablesTikz: Using matrix and foreach?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
sdaau
Posts: 26
Joined: Fri Feb 19, 2010 2:08 pm

Tikz: Using matrix and foreach?

Post by sdaau »

Hi all,

please take a look at the following code:

Code: Select all

\usetikzlibrary{shapes.arrows,chains,positioning}

\begin{tikzpicture}[font=\tt]
	% NOTE: matrix generates '! Missing } inserted.' for:
	% * ANY line-ends inside - make sure to comment empty lines!
	% * if it is completely empty (i.e. doesn't have any nodes)
	\matrix[draw=red]
	{
		% the individual nodes work: 
		%% \node[right] {AA} ; \\
		%% \node[right] {BB} ; \\
		% foreach, however doesn't work (! Missing } inserted.):
		\foreach \nn in {AA,BB} {\node[right] {\nn} ; \\};
	};
\end{tikzpicture}
Is it possible to use matrix with foreach at all??

Thanks in advance,

Cheers!

EDIT: In case my example doesn't conform to the MWE requirements - that is the only thing I have in a '.tikz' file; and then I use tikz2pdf to generate a pdf out of it...

EDIT2: Likely the same question, without a definitive answer, on a German forum: TikZ-Matrix in foreach („&“ und Num. too big) .:. goLaTeX - Mein LaTeX-Forum

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

coachbennett1981
Posts: 274
Joined: Fri Feb 05, 2010 10:15 pm

Re: Tikz: Using matrix and foreach?

Post by coachbennett1981 »

What exactly are you trying to draw? It is difficult without more information on this topic.
sdaau
Posts: 26
Joined: Fri Feb 19, 2010 2:08 pm

Tikz: Using matrix and foreach?

Post by sdaau »

Hi coachbennett1981,

Thanks for your response!
coachbennett1981 wrote:What exactly are you trying to draw? It is difficult without more information on this topic.
It is merely a syntax question - instead of typing a new line for each node, i.e.

Code: Select all

\node[right] {AA} ; \\
\node[right] {BB} ; \\
I'd like to use something like this:

Code: Select all

\foreach \nn in {AA,BB} {\node[right] {\nn} ; };
and then, I'd simply add a node from the "array" {AA,BB}... However, as with the above syntax, pdflatex crashes - I was wandering whether there is some syntax that would allow \foreach to work with \node - or whether it is in principle impossible??

Thanks again,
Cheers!
sdaau
Posts: 26
Joined: Fri Feb 19, 2010 2:08 pm

Tikz: Using matrix and foreach?

Post by sdaau »

Hi all,

Well, finally here is a syntax that does compile:

Code: Select all

\usetikzlibrary{shapes.arrows,chains,positioning}

\begin{tikzpicture}[font=\tt]
   % NOTE: matrix generates '! Missing } inserted.' for:
   % * ANY line-ends inside - make sure to comment empty lines!
   % * if it is completely empty (i.e. doesn't have any nodes)
   \matrix[draw=red]
   {
      % the individual nodes work:
      %% \node[right] {AA} ; \\
      %% \node[right] {BB} ; \\
      % this foreach passes - but line ends are not interpreted; so nodes overlap!
      \foreach \nn in {AA,BB} 
	    \node[right] {\nn} ; \\
   };  
\end{tikzpicture}
.. and as the comment says - the code compiles, nodes are rendered - however, the line ending (\\) is not interpreted, and so the nodes overlap.. Apparently, using the curly braces {} to encapsulate the \foreach argument was the problem...

So, it is possible syntactically to use \foreach in a tikz \matrix with nodes (apart from the line break problem)...

I thought also that it may have had to do with using the 'variable' \nn, as I found in the pgf manual:
Each assignment in a let operation starts with \p, usually followed by a <digit>: \x digit , \y digit , \p digit ...

Instead of writing \p1 = ..., we write \n2 = .... Here, “n” stands for “number” (while “p” stands for “point”).

... but apparently, that is not the problem here ...

Cheers!

EDIT Feb 28 2011: I think I found something close to an answer:

TikZ \foreach loop with macro-defined list - TeX - LaTeX - Stack Exchange
The \mymacro isn't expanded by the \foreach loop, but only afterwards. You need to remove the braces { } around the macro to make it work:
...
Note that while normally both arguments {\mymacro} and \mymacro are identical, \foreach seems to test for { and expands the argument if it isn't present.
...
As far as I remember this is an explicit feature of the \foreach macro.
...
Btw. the pgf manual says: Still in the easiest case, <list> is either a comma-separated list of values surrounded by curly braces or it is the name of a macro that contain such a list of values.
Post Reply