TransWikia.com

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

TeX - LaTeX Asked on November 19, 2021

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}

2 Answers

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

Answered by Zarko on November 19, 2021

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

Answered by Ignasi on November 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP