使用 TikZ 绘制数据包布局

使用 TikZ 绘制数据包布局

我是 TikZ 的新手,我很难弄清楚如何创建类似于此的图像。

在此处输入图片描述

到目前为止,我已经能够创建所有矩形,但有两件事我无法弄清楚如何做:

  1. 我想用B symbols中心的椭圆替换该区域以显示连续性,并且
  2. 除了坐标之外,是否有一种优雅的方法来指定标签的定位,以便在矩形区域周围移动时移动相关的标签?

答案1

这是一种可能性(我不确定中心区域所需的布局):

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{calc,positioning,shapes,decorations.pathreplacing}

% the styles for short and long nodes
\tikzset{
short/.style={draw,rectangle,text height=3pt,text depth=13pt,
  text width=7pt,align=center,fill=gray!30},
long/.style={short,text width=1.5cm}
}

\begin{document}

% the short nodes \shnode{<label>}{<right of>}{<text>}
\def\shnode#1#2#3{%
  \node[short,right=of #1] (#2) {\rotatebox{270}{#3}}}

% the long nodes \lnode{<label>}{<right of>}
\def\lnode#1#2{%
  \node[long,right=of #1] (#2) {}}

\noindent\begin{tikzpicture}[node distance=-\pgflinewidth]

\node[short,fill=black] (a) {};
\shnode{a}{b}{src};
\shnode{b}{c}{dst};
\shnode{c}{d}{len};
\lnode{d}{e};
\lnode{e}{f};
\node[long,draw=none,fill=none,right=of f,text height=0pt,text depth=0pt,text width=1cm] (g) {$\ldots$};
\node[long,right=of g,label=center:$b$ bits] (h) {};
\lnode{h}{i};
\shnode{i}{j}{len};
\shnode{j}{k}{dst};
\shnode{k}{l}{src};
\node[short,fill=black,right=of l] (m) {};

\node[above right=1cm of a] (ppre) {Packet preamble};
\node[above left=1cm of m] (ppos) {Packet postamble};

\draw[decorate,decoration={brace,raise=2pt}] (b.north west) -- node[above=4pt] {Header} (d.north east);
\draw[decorate,decoration={brace,raise=2pt}] (j.north west) -- node[above=4pt] {Trailer} (l.north east);
\draw[decorate,decoration={brace,raise=2pt}] (h.north west) -- node[above=4pt] {Codeword} (h.north east);
\draw (ppre.west) -- +(-20pt,0pt) -- (a.north);
\draw (ppos.east) -- +(20pt,0pt) -- (m.north);
\draw (a.south east) -- +(0,-1.2cm);
\draw (m.south west) -- +(0,-1.2cm);
\draw[<->] ( $ (a.south east) +(0,-0.6cm) $ ) -- node[fill=white] {$S/B$ Codewords} ( $ (m.south west) +(0,-0.6cm) $ );
\draw[,dotted] (f.north east) -- (h.north west);
\draw[dotted] (f.south east) -- (h.south west);

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容