# TikZ - vary node positioning depending on shape size / anchors

My general question would be how to ensure a minimum x distance between the east anchor of one shape and the west anchor of another (assume the shapes are y-aligned if it helps), or similarly a minimum y distance between north/south anchors.

My more particular need is for a flowchart where text boxes (rectangles) can have fairly wide variation in text content, and I wish the spacing between them to be no less than some value, by pushing around the central nodes if needed. The borders of the rectangles themselves have a few width options but the problem is that the (central) node position ignores how wide the rectangle is, which can cause overlapping of rectangles (and mess up the arrows between them too).

TikZ: position a node depending on width of text? might hint to a solution for my particular need by calling pgf to measure the size of the text but (1) I failed to tailor it to my needs, (2) it seems to work only for simple text labels (not general "measure your anchor position" stuff, and not even automatic text flow measurement), and (3) it is from 2011 so maybe TikZ has changed since.

Example: (node distance options fairly irrelevant to the behavior at hand, but it makes the problem appear for more realistic text lengths)

\documentclass[tikz]{standalone}

\begin{document}

\tikzstyle{block} = [draw, rectangle]

\begin{tikzpicture}[node distance=3cm]
\node [block] (A) {A};
\node [block, right of=A] (B) {B};
\node [block, right of=B] (C) {C};
\draw[->] (A) edge node[above] {AB} (B);
\draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\begin{tikzpicture}[node distance=3cm]
\node [block] (A) {A};
\node [block, right of=A] (B) {Text in B};
\node [block, right of=B] (C) {C};
\draw[->] (A) edge node[above] {AB} (B);
\draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\begin{tikzpicture}[node distance=3cm]
\node [block] (A) {A};
\node [block, right of=A] (B) {Very long text in position B};
\node [block, right of=B] (C) {C};
\draw[->] (A) edge node[above] {AB} (B);
\draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\begin{tikzpicture}[node distance=3cm]
\node [block] (A) {A};
\node [block, right of=A] (B) {Very very long text in position B};
\node [block, right of=B] (C) {C};
\draw[->] (A) edge node[above] {AB} (B);
\draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\end{document}


You can use eqparbox to achieve this effect. Please use the postioning library and note that \tikzstyle is deprecated.

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\usepackage{eqparbox}
\newbox\eqnodebox
\tikzset{equal size/.style={execute at begin
node={\setbox\eqnodebox=\hbox\bgroup},
execute at end node={\egroup\eqmakebox[#1][c]{\copy\eqnodebox}}},
equal size/.default=A,}
\begin{document}

\tikzset{block/.style=draw, rectangle}

\begin{tikzpicture}[node distance=2cm]
\node [block] (A) {A};
\node [block, right=of A,equal size=B] (B) {B};
\node [block, right=of B] (C) {C};
\draw[->] (A) edge node[above] {AB} (B);
\draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\begin{tikzpicture}[node distance=2cm]
\node [block] (A) {A};
\node [block, right=of A,equal size=B] (B) {Text in B};
\node [block, right=of B] (C) {C};
\draw[->] (A) edge node[above] {AB} (B);
\draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\begin{tikzpicture}[node distance=2cm]
\node [block] (A) {A};
\node [block, right=of A,equal size=B] (B) {Very long text in position B};
\node [block, right=of B] (C) {C};
\draw[->] (A) edge node[above] {AB} (B);
\draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\begin{tikzpicture}[node distance=2cm]
\node [block] (A) {A};
\node [block, right=of A,equal size=B] (B) {Very very long text in position B};
\node [block, right=of B] (C) {C};
\draw[->] (A) edge node[above] {AB} (B);
\draw[->] (B) edge node[above] {BC} (C);
\end{tikzpicture}

\end{document}


• Well, that is not what I asked (I do not want the boxes to have the same size but to move around to avoid overlapping), but I am still accepting because it contains the ingredients to make my (simpler) question work: use positioning and right=of X (instead of right of=X). Basically RTFM section 17.5.3 "advanced placement options".
– JMU
Feb 11, 2020 at 16:03