# Why doesn't relative coordinates in TikZ work with this example?

I was working on a figure, and for some reason relative coordinates have stopped working (it did before). I removed all code down to a bare minimum, and they still don't work. I don't know what I've done wrong so I'll just include it here:

\documentclass[english]{article}

\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{fit}
\usetikzlibrary{backgrounds}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
% Nodes
\node at (0, 0) (input) {i};
\node at ++(2, 0) (p1) {a};
\node at ++(2, 0) (p2) {b};
\end{tikzpicture}
\end{document}


In this example, a and b are positioned on top of each other instead of next to each other. Why?

-

Because they are relative to the same coordinate (0,0). If you replace the last line in the tikzpicture environment with \node at ++(4, 0) (p2) {b}; it moves. Or if you want to define node positions relative to other nodes you can use.

\begin{tikzpicture}
\node at (0, 0) (input) {i};
\node at ($(input)+(2,0)$) (p1) {a};
\node at ($(input)+(4,0)$) (p2) {b};
\end{tikzpicture}


while having \usetikzlibrary{calc} in your preamble.

As Jake says, the ++(...) syntax works within a TikZ command and is relative to the last saved position. At the start of the line, the coordinate is set to (0,0) so each of your relative coordinates is relative to (0,0). To make each relative to the last, either use explicit coordinates/nodes as above or combine your statements into one:

\begin{tikzpicture}
\node {i} node at ++(2,0) (p1) {a} node at ++(4,0) (p2) {b};
\end{tikzpicture}


Note that the first node doesn't need an explicit coordinate (via at) since it is placed at (0,0) which is the starting position of the path.

-
Your answer is completely correct but a bit sparse on details! I was about to write an answer to this question when your answer was posted, so I'd rather just edit the extra details in to your answer. Would that be okay? –  Andrew Stacey Sep 26 '11 at 13:05
@AndrewStacey Sure. I would be more than happy. –  percusse Sep 26 '11 at 13:06
Done: The bit in yours that Jake was missing was the fact that the relative coordinates are relative to something: the initial position of the path. –  Andrew Stacey Sep 26 '11 at 13:12
@AndrewStacey : Thank you for the clarification. I can delete this if you want to make further elaboration. Let me know :) –  percusse Sep 26 '11 at 13:30

... or (continuing @percusse's & @Jake's answer), since you are loading the positioning TikZ library anyway, you can write:

\begin{tikzpicture}[node distance=2cm, on grid]
% Nodes
\node at (0, 0) (input) {i};
\node (p1) [right=of input] {a};
\node (p2) [right=of p1] {b};
\end{tikzpicture}


This makes the code much simpler to understand and you can change the distance between the nodes later on, by modifying only one number. You can even have 'groups' of nodes that are spaced differently, if you place within a scope environment.

Depending what your actual application is, you may want to take a look at the petri TikZ library (TikZ 2.10 manual - pp.394 - 398).

-
The ++ operator only works within one \draw, \fill, \path or \node command. To get the example to work as expected, use \path ++(2, 0) node (p1) {a} ++(2, 0) node (p2) {b};