3

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

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.

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.