1

I want to be able to control the distance between the edges of nodes in TikZ. In the below example, I want the left edge of "Node B" to be exactly 5 cm from the right edge of Node A. However, right of=A, node distance=5cm sets the distance between the centres of the nodes.

enter image description here

\documentclass[tikz,border=10pt]{standalone}

\usetikzlibrary{arrows, arrows.meta, backgrounds, calc, intersections, decorations.markings, decorations.pathreplacing, positioning, shapes}

\begin{document}

\begin{tikzpicture}

\draw node[draw, rectangle, text centered] (A) {Node A};

\draw node[draw, rectangle, right of=A, node distance=5cm, text centered] (B) {Node B};

\begin{scope}[>=latex] \draw[<->] ($(A.east)$) -- ($(B.west)$) node[midway, above] {\tiny{I want this distance to be 5 cm}}; \draw[<->] ($(A)+(0mm,-5mm)$) -- ($(B)+(0mm,-5mm)$) node[midway, below] {\tiny{not this distance}}; \end{scope}

\end{tikzpicture}

\end{document}

I found this related answer but the solution (i.e., using "right = of A" instead of "right of = A") doesn't work for me: Set node distance between borders in tikz

TheDon
  • 755
  • 2
    Why does it not? Did you load the positioning library and set the node distance to 5cm before you have used right=of A? (Or implicitly: right=5cm of A.) Usually the node distance is set on an outer scope and not on the node itself. – Qrrbrbirlbel Oct 20 '23 at 17:37
  • I didn't know that the order mattered. I was using right=of A, node distance=5cm. It works now. Thanks for also suggesting the short notation, which I didn't know. – TheDon Oct 20 '23 at 18:08

1 Answers1

1

Your code example is better to write on the following way:

\documentclass[tikz, border=10pt]{standalone}

\usetikzlibrary{arrows, arrows.meta, backgrounds, calc, decorations.markings, decorations.pathreplacing, intersections, positioning, shapes}

\begin{document} \begin{tikzpicture}[> = latex, node distance = 50mm, ]

\draw node[draw] (A) {Node A}; % default shape is rectangle \draw node[draw, right=of A] (B) {Node B}; % used is positioning package sybntay

\draw[<->] (A) -- node[above, font=\tiny] {I want this distance to be 5 cm} (B); \draw[<->] ($(A.south)+(0mm,-5mm)$) -- node[below, font=\tiny] {not this distance} ($(B.south)+(0mm,-5mm)$); \end{tikzpicture} \end{document}

enter image description here

  • As @Qrrbrbirlbel mentioned in his comment, node distance is better to define outside of nodes. Above is defined as tikzpicture option, however you can define it by \tikzset before nodes or even globally in document preamble.
  • At drawing nodes the for default shape is assumed rectangle.
  • If you not prescribe size of the node, its border is drawn around its content consider default value of inner sep, so in such cases defining text=centered has not sense. BTW, text=centered is obsolete syntax (with some rare exceptions), instead it you should use align=center.
  • For distance between border of nodes you should use positioning syntax. In your case right=of A (or right= 5cm od A, if you not like to use globally defined `node distance).
  • For more about tikz you need to read its documentation, at list first tutorial in part I and part III: TikZ ist kein Zeichenprogramm (complete documentation is huge (1321 pages).
  • And BTW again, in your document example (MWE: Minimal Working Example) is sufficient to load only arrows, calc and positioning libraries.
Zarko
  • 296,517