# How to determine minimum width of rectangle with distance between two nodes

From similar questions and given examples about how to calculate distances between two points in the TikZ manual, I was not able to figure out how to extract from them a solution for my (probably simpler) need. See, not exactly a MWE, below:

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

\begin{document}
\begin{tikzpicture}[node distance=7mm and 0mm,
N/.style 2 args = {name=n#1,
minimum width=#2mm,
shape=rectangle, draw, fill=white, align=center,
minimum height=9mm, inner sep=1mm, outer sep=0mm,
text width =\pgfkeysvalueof{/pgf/minimum width} - 2*\pgfkeysvalueof{/pgf/inner xsep}
},
]\small\sffamily
\node[N={1}{22},label=above:22mm]                    {sinhronizacija};
\node[N={2}{13},label=above:13mm,right=of n1]        {SFD};
%---
\node[N={3}{13},label=above:13mm,right=of n2]        {signal};
\node[N={4}{13},label=above:13mm,right=of n3]        {storitev};
\node[N={5}{13},label=above:13mm,right=of n4]        {dol\v{z}na};
\node[N={6}{13},label=above:13mm,right=of n5]        {FSM};
%---
\node[N={7}{77},label=above:77mm,right=of n6]        {PSDU};
%-------
\dimendef\mynodewidth=0
\pgfmathparse{22+5*13+77}
\pgfmathsetlength{\mynodewidth}{\pgfmathresult mm}
\node[N={8}{},minimum width=\mynodewidth,
below right=of n1.south west] {(22+5*13+77) mm = 164 mm = distance between n1.west and n7.west};
%-------
\end{tikzpicture}


It works, but I'd like to have a more general solution, where I do not need to manually set up a sum of minimum width of nodes of interest, and where \nodewidth is obtained from the distance between given two nodes, i.e.: the first is at the leftmost border of a set of nodes and the second one on the rightmost border. something like

\nodewidth = <macro for distance calculation between>(ni.west,nj.east)

• Now i see that i have an error in my question, so I edit it and correct my error. but now disappear received unswear and edits of my question given (I assume) by moderator. please, be tolerant with me, I'm very new here ... – Zarko Dec 19 '13 at 15:50
• yes, it is. i'm very gratefull for it. it gives me a clue, how this legth work. thank to Jessy again – Zarko Dec 19 '13 at 17:16
• – Qrrbrbirlbel Dec 19 '13 at 17:41
• Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – jub0bs Jan 12 '14 at 11:48

This attempt uses let and veclen to determine the distance between n1 and n7, then incorporates your node N definition, which requires two inputs.

To demonstrate, the original 4-line-code starting from 'dimedef' is marked out and replaced by the \path command this solution provided. This attempt generates the same output from the OP in which the length 164 is given. Code:

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

\begin{document}
\begin{tikzpicture}[node distance=7mm and 0mm,
N/.style 2 args = {name=n#1,
minimum width=#2mm,
shape=rectangle, draw, fill=white, align=center,
minimum height=9mm, inner sep=1mm, outer sep=0mm,
text width =\pgfkeysvalueof{/pgf/minimum width} - 2*\pgfkeysvalueof{/pgf/inner xsep}
},
]\small\sffamily
\node[N={1}{22},label=above:22mm]                    {sinhronizacija};
\node[N={2}{13},label=above:13mm,right=of n1]        {SFD};
%---
\node[N={3}{13},label=above:13mm,right=of n2]        {signal};
\node[N={4}{13},label=above:13mm,right=of n3]        {storitev};
\node[N={5}{13},label=above:13mm,right=of n4]        {dol\v{z}na};
\node[N={6}{13},label=above:13mm,right=of n5]        {FSM};
%---
\node[N={7}{77},label=above:77mm,right=of n6]        {PSDU};
%-------
%    \dimendef\mynodewidth=0
%    \pgfmathparse{22+5*13+77}
%    \pgfmathsetlength{\mynodewidth}{\pgfmathresult mm}
%    \node[N={8}{},minimum width=\mynodewidth,
%    below right=of n1.south west] {(22+5*13+77) mm = 164 mm = distance between n1.west and n7.west};
%-------
\path let \p1=(n1.south west),\p2=(n7.south east) ,\n1={veclen(\x2-\x1,\y2-\y1)}
in (n1)--(n7)node[draw,N={8}{},minimum width=\n1, below right=of n1.south west]
{(22+5*13+77) mm = 164 mm = distance between n1.west and n7.west};
%-------
\end{tikzpicture}
\end{document}

• Thank you very much. Now I see, haw is length calculated and considered in node. On this basis i try to minimize the suggested code as follows: \path let \p1=(n1.west), \p2=(n7.east) , \n1={veclen(\x2-\x1,\y2-\y1)} in node[N={8}{},minimum width=\n1,below right=of n1.south west] {(22+5*13+77) mm = 164 mm = distance between n1.west and n7.east}; and it also work. Now I have further partially related questions ... – Zarko Dec 19 '13 at 16:54
• Yes, the reduction will work because of rectangle shape of the node. – Jesse Dec 19 '13 at 17:06