从类似的问题以及 TikZ 手册中给出的关于如何计算两点之间距离的示例来看,我无法弄清楚如何从中提取出满足我(可能更简单)需求的解决方案。请参见下面的(不完全是 MWE):
\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}
它可以工作,但我想要一个更通用的解决方案,我不需要手动设置感兴趣的节点的最小宽度的总和,并且\nodewidth
从给定两个节点之间的距离获得,即:第一个位于一组节点的最左边的边界,第二个位于最右边的边界。就像
\nodewidth = <macro for distance calculation between>(ni.west,nj.east)
答案1
此次尝试使用let
和veclen
来确定 n1 和 n7 之间的距离,然后结合节点 N 定义,这需要两个输入。
为了演示,从“dimedef”开始的原始 4 行代码被标记出来并由此\path
解决方案提供的命令替换。此尝试生成与 OP 相同的输出,其中给出了长度 164。
代码:
\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}