# How can I achieve relative positioning in tikz [duplicate]

Possible Duplicate:
Relative positioning of nodes in TikZ

I'm creating a tree diagram in tikz and I know that calculating each point would be silly. For that reason, I want to place things in relative position to each other.

So, here are two questions:

1. How can I define the second node in this simple example?

\begin{tikzpicture}
\node (a) at (0,0) {};
\node (b) at (2,1) {};
\end{tikzpicture}

2. How can I replace this line command with one that will have relative positioning (a and b are those created above)?

\draw (a) -- (0,1);
\draw (0,1) -- (b);


Please let me know if something is not clear.

-

## marked as duplicate by Joseph Wright♦Nov 4 '12 at 9:49

There is both default support and a great TikZ library called positioning just for this. See manual section 16.5.3 –  percusse Aug 30 '12 at 12:48
@Yotam: Instead of providing code snippets, you should use minimal complete example documents. That saves others the work of filling in the blanks, and it also makes it easier for you to test the correctness of your code (in the first example, you're missing the at keyword and you have a typo in \noe). –  Jake Aug 30 '12 at 12:50
@Jake, sorry. As this is not a technical problem, but rather looking for the right tool. I didn't think any one will try to compile this. –  Yotam Aug 30 '12 at 12:57
@Yotam: You're right, in this case it's not that important. Coming up with a couple of test cases is still usually a good idea. For this question, it might be a good idea to specify what exactly you mean by relative positioning: Should the center of node b be 2cm to the right and 1cm above the center of node a, or should their edges be that far apart (both is possible)? I would suggest to read the section percusse referred to, and then make your question more concrete if you get stuck. –  Jake Aug 30 '12 at 13:00

As has been mentioned in the comments, you can use the positioning library; here's a simple example illustrating the options available when using, for example, above left (the same diagram is repeated twice; the second time the on grid option is activated):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}
\draw[help lines,step=5mm,gray!20] (0,0) grid (4,3);
\node (a) at (0,0) {a};
\node[above right] (b) {b};
\node[above right = of a] (c) {c};
\node[above right = 2cm of a] (d) {d};
\node[above right = 2cm and 3cm of a] (e) {e};
\begin{scope}[xshift=5cm,on grid]
\draw[help lines,step=5mm,gray!20] (0,0) grid (4,3);
\node (a) at (0,0) {a};
\node[above right] (b) {b};
\node[above right = of a] (c) {c};
\node[above right = 2cm of a] (d) {d};
\node[above right = 2cm and 3cm of a] (e) {e};
\end{scope}
\end{tikzpicture}

\end{document}


Now a little example showing the node distance key and some options besides above left:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}
\draw[help lines,step=5mm,gray!20] (-4,-4) grid (4,3);
\node[draw] (a) at (0,0) {a};
\foreach \pos in {above,above right,right,below right,below,below left,left,above left}
\node[draw,\pos = of a] () {\pos};
\begin{scope}[yshift=8cm,node distance=2cm and 1cm]
\draw[help lines,step=5mm,gray!20] (-4,-4) grid (4,3);
\node[draw] (a) at (0,0) {a};
\foreach \pos in {above,above right,right,below right,below,below left,left,above left}
\node[draw,\pos = of a] () {\pos};
\end{scope}
\end{tikzpicture}

\end{document}


The pgfmanual explains some other options available through this library.

Referring now to the specific example on the original question, the following example shows the original code in which everything is done manually, and then the same diagram produced used the positioning library and some of the ideas mentioned above:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}
\node (a) at (0,0) {a};
\node (b) at (2,1) {b};
\draw (a) -- (0,1);
\draw (0,1) -- (b);
\begin{scope}[xshift=3cm,on grid]
\node (a) at (0,0) {a};
\node[above right= 1cm and 2cm of a] (b) {b};
\draw (a) |- (b);
\end{scope}
\end{tikzpicture}

\end{document}


-