TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I declare coordinates in a block and access them from outside of the block?

  \foreach \x / \y in {
    1 /  3,
    2 /  8,
    3 / 14,
    \coordinate (N\x) at (\x,\y);
    \path[draw] (N\x) circle (1em);      %% ok
  \path[draw] (N1) -- (N3);              %% not ok
share|improve this question
One thing to note about the answers you've gotten: they've removed the last comma. Your loop executes four times; the last with \x and \y empty. – Loop Space Nov 25 '11 at 10:55
up vote 4 down vote accepted

Remove the spaces between your coordinates. i.e.

\foreach \x / \y in {1/3,2/8,3/14}


\foreach \x / \y in {

should work.


As Andrew pointed out, the % is necessary in the second code block to avoid additional spaces etc. getting appended to the intended 14. This is not necessary in the first code block as the 14 is followed immediately by a }. The first code block is probably the best way to do this for short lists.

The original code from the question uses a comma where the % is in the second block above, which causes the foreach to do an additional unwanted loop iteration with no content. This results in the creation of a coordinate called N at (1,1), which is neither wanted nor desirable.

share|improve this answer

As it is written in others answers, it's not a problem of global coordinatesbut a problem with wrong names of coordinates because blank are dangerous in the arguments of \foreach

In this special case, would be more concise : ( but in the general case, the line \coordinate (N\x) at (\x,\y);can be avoided)


  \foreach \y [count=\x from 1] in {3,8,14}
      \draw   (\x,\y) coordinate (N\x) circle (1em);
  \draw   (N1) -- (N3); 

share|improve this answer

You can do this without modification. The problem is that your coordinates are not named N1, N2, and N3 due to spurious spaces. Change it to:

  \foreach \x / \y in {1/3,2/8,3/14%
    \coordinate (N\x) at (\x,\y);
    \path[draw] (N\x) circle (1em);      %% ok
  \path[draw] (N1) -- (N3);

and you are fine.

As Andrew already mentioned as a comment to your question. The last , has to go. It runs through the loop again with \x and \y empty. This does actually draw an extra circle at (1,1) and defines the coordinate N. I am not completely sure why it gets drawn at (1,1) to be honest. I would expect it to default to (0,0) instead. Does anybody have an explanation for this?

Edit: I think I know why it occurs at (1,1). The coordinate system has a value for x and y and when you say something like (3,7) this means take 3 steps of size x in the x direction and 7 steps of size y in the y direction. This allows to write coordinates without units and change the 'scale' afterwards. These values for x and y default to 1 cm and not 0 cm such that writing (3,7) becomes (3 cm, 7 cm) instead of (0 cm, 0 cm) which would be far less usefull. When you leave them empty, the defaults for x and y are used.

share|improve this answer
Since this is all about spaces, it would be good to also remove the space after the 14 caused by the new line. Although in this case it makes no difference, in other cases it might. A % is probably simplest as it keeps the layout. – Loop Space Nov 25 '11 at 10:51
@AndrewStacey: good suggestion. I added the % to not interfere with the formatting. – Roelof Spijker Nov 25 '11 at 10:59
@wh1t3 you need to add \end{tikzpicture} at the end of your code – Alain Matthes Nov 25 '11 at 11:26
@Altermundus: thanks, must have not copied the entire thing by mistake :-) – Roelof Spijker Nov 25 '11 at 11:27
I got curious. At some point, the coordinate calculation calls \pgf@x = \pgftemp@x \pgf@xx. In this, \pgf@x is to be the x coordinate of the point. \pgf@xx is the "unit" of the current x vector (~28pt by default). Then \pgftemp@x is the result of sending the specified x coordinate through \pgfmathparse. In this case, that returns nothing, by which I mean an empty token, not a zero token. Thus the line reads \pgf@x = \pgf@xx and so \pgf@x ends up as ~28pt, or 1cm. – Loop Space Nov 25 '11 at 14:42

Your Answer


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.