# Relative node positioning with calc library

Hi I'm trying to place same nodes relative to each other. I'm trying to place a node in the middle between two other nodes, but also half a cm to the right.

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{tikzpicture}
\node (p4) {$\hat{4}$} ;
\node[below=1cm of p4] (p3) {$3$} ;
\node[right=3cm of p4] (p1) {$\hat{1}$} ;
\node[below=1cm of p1] (p2) {$2$} ;
\node[below right={1/sqrt(2)}cm of p4] (v1) {$v_1$} ;
\end{tikzpicture}


I don't get why this isn't working... I get the following error

Package PGF Math Error: Unknown operator c' orcm' (in '{1/sqrt(2)}cm ').

• Try below right=1cm/sqrt(2) of p4 (or below right=1/sqrt(2)*1cm of p4)... and use the positioning TikZ library. – Paul Gaborit Aug 30 at 13:29

You must:

• Add the positioning library.
• Use \pgfmathparse to calculate the 1/sqrt(2) expresion.
• Use \pgfmathresult where is the result of calculation.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}
\begin{document}
\begin{tikzpicture}
\node (p4) {$\hat{4}$} ;
\node[below=1cm of p4] (p3) {$3$} ;
\node[right=3cm of p4] (p1) {$\hat{1}$} ;
\node[below=1cm of p1] (p2) {$2$} ;
\pgfmathparse{1/sqrt(2)}
\node[below right=\pgfmathresult cm of p4] (v1) {$v_1$} ;
\end{tikzpicture}
\end{document}


Or if you don't use some unit like cm, you don't need \pgfmathparse.

## Edit

To avoid problems with \pgfmathparse{} and \pgfmathresult you can use your own macro with \pgfmathsetmacro{...}. This change avoids the problem showed by @percusse.

\begin{tikzpicture}
\node (p4) {$\hat{4}$} ;
\node[below=1cm of p4] (p3) {$3$} ;
\node[right=3cm of p4] (p1) {$\hat{1}$} ;
\node[below=1cm of p1] (p2) {$2$} ;
\pgfmathsetmacro{\myroot}{1/sqrt(2)} % <-
\node[line width=3mm,below right=\myroot cm of p4] (v1) {$v_1$} ;
\end{tikzpicture}


Although this is not relevant to this specific problem, it may be useful for other cases.

• Use [line width=3mm,below right=\pgfmathresult cm of p4] as the option and you'll see a nice problem with this. Use pgfmathresult as soon as possible before any other math computation intervenes. – percusse Aug 21 '14 at 11:45
• @percusse Ah, i see, do you have an explanation for this? it seems better don't put units and to avoid using of \pgfmathresult. – skpblack Aug 21 '14 at 14:11
• I tried to explain here a bit tex.stackexchange.com/a/191252/3235 TikZ can understand the math as in the other answer but you need to multiply it by 1cm or 1mm or 1pt at the end. So you don't need \pgfmathparse at all. – percusse Aug 21 '14 at 14:15

You have to use the positioning-library of tikz to position nodes relative to each other.

Try the follwing:

\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{calc, positioning}

\begin{document}
\begin{tikzpicture}
\node (p4) {$\hat{4}$};
\node[below=1cm of p4] (p3) {$3$};
\node[right=3cm of p4] (p1) {$\hat{1}$};
\node[below=1cm of p1] (p2) {$2$};
\node[below right={1/sqrt(2)} of p4] (v1) {$v_1$};
\end{tikzpicture}
\end{document}


Update: If you want to use another unit than cm (in, pt, ...)

The above code uses tikz's default of 1cm as base unit. If you want to use another base unit for your picture you can set this in the tikzpicture's options. This way you don't need to set the unit after the calculation of sqrt() and inhibit the error.

\begin{tikzpicture}[x=1in, y=1in]
\node[below=1cm] ... % use of cm
\node[below=1]   ... % use of global unit (e.g. in)
...


It is possible to do analytic geometry in tikz. That is, add a polar/rectangular displacement and add two coordinates/vectors.

\documentclass[border=3mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\newdimen\x
\newdimen\y
\newcommand*{\GetXY}[1]{\path (#1); \pgfgetlastxy{\x}{\y};}%

\begin{document}

% ======================================= V
\begin{tikzpicture}
% absolute
\node at ($(1, -2) + (0.5, 1)$) (pX) {X};
\node at ($(2, 1) - (1.5, 1)$) (pY) {Y};

% polar coordinates wrt pA
\node at (4, 1) (pA) {A};
\node at ($(pA) + (30:1)$) (pAa) {Aa};
\node at ($(pA) + (-30:1)$) (pAb) {Ab};

% rectangular coordinates
\node at (1, 3) (pB) {B};
\node at ($(pB) + (-1,2)$) (pBa) {Ba};
\node at ($(pB) - (-1,2)$) (pBb) {Bb};

% relative
\node at ($(pA) + (pB)$) (pABp) {ABp};
\node at ($(pA) - (pB)$) (pABm) {ABm};

% with x and y
\node at (-1, -1) (pC) {C};
\GetXY{$(pC)$};
\node at ($(\x, -\y)$) (pCa) {Ca};

% helpers below (feel free to commend out)
% axis
\node at (0, 0) (pO) {O};
\draw[->] (pO) -- (1, 0);
\draw[->] (pO) -- (0, 1);
%
\draw[dashed] (pA) -- (pAa);
\draw[dashed] (pA) -- (pAb);
\draw[dashed] (pB) -- (pBa);
\draw[dashed] (pB) -- (pBb);
\draw[dashed] (pA) -- (pABp);
\draw[dashed] (pA) -- (pABm);
\draw[dashed] (pC) -- (pCa);
\end{tikzpicture}
% ======================================= A

\end{document}