5

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. enter image description here

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}

Aria
  • 1,523

2 Answers2

7

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}

enter image description here

Ignasi
  • 136,588
  • 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 Jul 22 '20 at 09: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. – Ignasi Jul 22 '20 at 10:41
  • @Aria Take a look at section 17.5.3 in tikz documentation and will understand my comments. – Ignasi Jul 22 '20 at 10:42
5
  • 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}

enter image description here

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:

enter image description here

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)>

Zarko
  • 296,517
  • There is a calculation error since 1 mm measures about 3 pt. – AndréC Jul 22 '20 at 11:30
  • @AndréC, you are right. I will correct answer accordingly asap – Zarko Jul 22 '20 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 Jul 22 '20 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. – Zarko Jul 22 '20 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 Jul 22 '20 at 18:08