# How to measure accurately the distance between TikZ nodes centers when using relative placement?

I want to measure accurately the distance between TikZ nodes centers when using relative placement. My answer is around 1.55 cm but this is ballpark.

Below is my code:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary {positioning}
\begin{document}

\begin{tikzpicture}
[place/.style={circle,draw=blue!50,fill=blue!20,thick,
inner sep=0pt,minimum size=6mm},
transition/.style={rectangle,draw=black!50,fill=black!20,thick,
inner sep=0pt,minimum size=4mm}]

\node[place] (centeral node) {};
\node[transition] (right node) [right=of centeral node] {};

\draw [|-|] (centeral node.center) -- ++(1.55,0);

\end{tikzpicture}

\end{document}
• You can probably use the let path operation (sorry, commuting now, I can't try an answer). But this question smells of an XY problem... For what do you need the distance? Commented Jul 22, 2020 at 7:00
• Thanks. Knowing the distance, we can better design our figures comprising more complicated nodes.
– Aria
Commented Jul 22, 2020 at 7:02
• Does this help: Compute the x distance between two nodes
– Ross
Commented Jul 22, 2020 at 7:25
• Enable the on grid option from the positioning library (p.242, pgfmanual, v3.1.5.b) then use right=2cm of centeral node to put the center of your new node at exactly 2cm of centeral node center. Commented Jul 22, 2020 at 8:11

As I don't understand the question I decided to write two answers.

If questions is how to exactly place a node at a certain distance of another node, it's easy to do it if you know the angle from initial node or it's possible to decompose the distance in its horizontal+vertical movement.

As an example, in following code, right node.center is placed at exactly 1.55cm from centeral node.center. There is no need to measure the distance between them.

Also another place.center is placed at an exact distance of right node.center but following an 60 degrees angle. Again the distance is know.

But if the question is how to measure the distance between centeral node.center and another place.center which is unkonwn, then calc library can help. This is the third example.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary {positioning, calc}
\begin{document}

\begin{tikzpicture}
[place/.style={circle,draw=blue!50,fill=blue!20,thick,
inner sep=0pt,minimum size=6mm},
transition/.style={rectangle,draw=black!50,fill=black!20,thick,
inner sep=0pt,minimum size=4mm}]

\node[place] (centeral node) {};
\node[transition] (right node) [right=1.55 of centeral node] {};
\draw [|-|] (centeral node.center) -- (right node.center);

\path (right node.center) --++(60:2.13) node[place] (another place) {};
\draw[|-|] (right node.center) -- ++(60:2.13);

\draw[latex-latex] let \p1 = ($(another place.center)-(centeral node.center)$), \n1={veclen(\x1,\y1)} in (centeral node.center) --  node[above, sloped] {\n1} (another place.center);
\end{tikzpicture}

\end{document}

• The question is to measure the distance between the central node and the node on the right, using [right=of central node]. The 1.55 is my guess but not accurate.
– Aria
Commented Jul 22, 2020 at 9:10
• @Aria The problem with right=of central node is that the default distance (usually 1cm) is measured between corresponding anchors, therefore if you don't know node's size, you don't know the distance between centers. In this case you can use calc library to measure it, like in my example. But you can add option on grid to force that distances are measured between nodes' centers. this way you don't need to measure anything. Commented Jul 22, 2020 at 10:41
• @Aria Take a look at section 17.5.3 in tikz documentation and will understand my comments. Commented Jul 22, 2020 at 10:42
• Distance between of your nodes is equal to default value node distance between right and left side borders of nodes, i.e. between (centeral node.east) and (right node.west).
• For convenience of writing of the distance between their centers I increase this distance to 22mm
• Distance is calculated on the similar way as in @Ignasi answer (+1) with help of the calc TikZ library:
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{calc,
positioning,
quotes}

\begin{document}
\begin{tikzpicture}[
node distance = 22mm,
place/.style = {circle, draw=blue!50,thick, fill=blue!20,
inner sep=0pt,minimum size=6mm},
transition/.style = {draw, thick, fill=black!20,
inner sep=0pt,minimum size=4mm},
every edge quotes/.style = {auto, font=\scriptsize, inner sep=1pt}
]
\node[place] (centeral node) {};
\node[transition] (right node) [right=of centeral node] {};
%
\draw[|-|]
let \p1 = ($(right node.center) - (centeral node.center)$) in
(centeral node.center) %
to ["\x1",
"\pgfmathparse{scalar(\x1*0.35145980pt)} \pgfmathresult\,mm" '] % 1pt ~ 0.35145980mm
(right node.center);
\end{tikzpicture}
\end{document}

Addendum; Using default value for node distance which is 10mm (or 1 cm if you prefer), decreasing edge labels font size and and increasing its inner sep for better readable of measured values, adding 10mm long red line between nodes, to show default value of the node distance, and set outer sep=0pt that it not influence to nodes distance, the above MWE become:

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

\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}[
place/.style = {circle, draw=blue!50,thick, fill=blue!20,
inner sep=0pt, outer sep=0pt, minimum size=6mm},
transition/.style = {draw, thick, fill=black!20,
inner sep=0pt, outer sep=0pt, minimum size=4mm},
every edge quotes/.style = {auto, font=\tiny, inner sep=5pt}
]
\node[place] (centeral node) {};
\node[transition] (right node) [right=of centeral node] {};
%
\draw[|-|]
let \p1 = ($(right node.center) - (centeral node.center)$) in
(centeral node.center) %
to ["\x1",
"\pgfmathparse{scalar(\x1*0.351459803pt)}\pgfmathresult\,mm" '] % 1pt = 0.35145980mm
(right node.center);
\draw[red] (centeral node.east) -- ++ (10mm,0);% for show default distance between nodes
\end{tikzpicture}
\end{document}

It yields to:

Value 14,9998 mm ~ 15mm is equal to:

<default node distance> + (1/2)<minimum size of left node (=3mm)>+ (1/2)<minimum size of right node (=2mm)>

• There is a calculation error since 1 mm measures about 3 pt. Commented Jul 22, 2020 at 11:30
• @AndréC, you are right. I will correct answer accordingly asap Commented Jul 22, 2020 at 11:38
• @ Zarko; Thanks. In my code what is the default value of node distance? That is my question. I do not want to change the default to 22 mm.
– Aria
Commented Jul 22, 2020 at 15:36
• @Aria, than remove code line node distance = 22mm from tikzpicure options.. However, in this case the number will overlap with nodes borders, so as I said in answer, I increase distance for convenience of number reading. Commented Jul 22, 2020 at 15:40
• Thank you so much @ Zarko. Your answer and @Ignasi both address my question. Since Ignasi replied first I put his as the answer. But your answer also was right to the point. Sorry it is not possible to accept 2 answers. But Zarko I highly appreciate your great support.
– Aria
Commented Jul 22, 2020 at 18:08