# node names and nested loops in TikZ

I'm trying to draw a (part of a) lattice. To achieve that I use two nested foreach loops, like this:

\foreach \x in {-1, 0, 1} {
\foreach \y in {0, 1, 2} {
\path node (\x.\y) at (\x+\y-1, \y-\x) {};
}
}


Anyway I'm not able to refer to the nodes I create in the code above. Specifically, if I try something like this:

\path[->] (0.0) edge [bend right = 30] (0.1);


I get an error:

ERROR: Package pgf Error: No shape named 0 is known.


So the question is: which name will TikZ accept? The names should correspond to the lattice points. So, in some way, they should contain \x and \y.

Trying to variate on the nodes name doesn't help either. For example, if I try this

\path node (X\x Y\y) at (\x+\y-1, \y-\x) {};


I still get the same error:

ERROR: Package pgf Error: No shape named X0 Y0 is known.

-
Ok, I used (N\x_\y) and it works. Still the question remains, what is the required format for node names? – Giacomo d'Antonio Sep 12 '11 at 16:14
Basically, you can use anything you want, with some exceptions (see my answer), that won't "confuse" pgf. – Gonzalo Medina Sep 12 '11 at 16:25

Try this:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[>=latex,scale=2]
\foreach \x in {-1, 0, 1} {
\foreach \y in {0, 1, 2} {
\path node (\x-\y) at (\x+\y-1, \y-\x) {};
}
}
\path[->] (0-0) edge [bend right = 30] (0-1);
\end{tikzpicture}

\end{document}


From the pgf documentation (Section 3.7 Naming Nodes ):

Names for nodes can be pretty arbitrary, but they should not contain commas, periods, parentheses, colons, and some other special characters. However, they can contain underscores and hyphens.

-

To add to Gonzalo's answer, the reason why periods specifically shouldn't be used in node names is because they already have a special meaning when dealing with nodes. When you name a node, as in \node (mynode) at (0,0) {hello}; then TikZ sets a set of "anchors" around the node. The format for referencing these anchors is nodename.anchor. So we have mynode.north and mynode.north east, and also angular anchors such as mynode.90 and mynode.-10. The parser looks for the . in the reference and when it sees something.something else then it interprets that as nodename = something and anchor = something else. This happens before it looks to see if nodename actually exists. Then it tests for existence, and if it doesn't find it then it bails out.

Commas are also pretty easy to explain as (x,y) is the usual syntax for coordinates, and x and y don't need to initially be just numbers.

Parentheses would probably also confuse the parser as they are often used for special syntaxes (especially with the calc library).

Colons are used in the polar coordinate syntax, so that explains why you should avoid them.

The reason that:

\path node (X\x Y\y) at (\x+\y-1, \y-\x) {};


doesn't work is something else. This is because TeX swallows spaces after a command. So the node name gets set to the expansion of X\x Y\y which is, for example, X-1Y0 as the space after \x is eaten up. To get an actual space, you should use the \space macro or put an "empty" token after the \x. Thus either X\x\space Y\y or X\x{} Y\y will do.

Here's a bit of code that might be useful for you.

\makeatletter
\tikzset{%
show node name/.code={%
\expandafter\def\expandafter\tikz@mode\expandafter{\tikz@mode\show\tikz@fig@name}%
}
}
\makeatother


If you put that in your preamble, it puts in a little bit of code that displays the node name in the log. Use it as \node[show node name] ....

-
Thanks, that was very helpful! I suppose though, I have to accept the other answer. Since it answers the actual question, whereas this is an extension. Anyway, I really appreciate your answer. – Giacomo d'Antonio Sep 12 '11 at 19:53
the_code >> tex-snippets.tex: Thanks Andrew for this useful bit of background information. The show node name style is just neat! – Daniel Sep 13 '11 at 7:38