堆叠尺寸不等的矩形

堆叠尺寸不等的矩形

我正在尝试制作如下的数字:

我需要灵活的代码,可以很好地扩展到具有更多矩形的更复杂的图形(但最小的示例如上)。

这是我的初学者的解决方案,使用\usetikzlibrary{positioning}

\begin{tikzpicture}
\node[draw,rectangle,minimum width=7cm,minimum height=1.2cm] (n1) {(1)};
\node[draw,rectangle,minimum width=4cm,minimum height=0.9cm,above=0 of n1.north west,anchor=south west,node distance=0] (n21) {(2,1)};
\node[draw,rectangle,minimum width=3cm,minimum height=0.9cm,right=0 of n21.east,anchor=west,node distance=0] (n22) {(2,2)};
\end{tikzpicture}

这里有几个问题:

  • 相邻矩形之间的分隔线绘制两次(放大时可见)
  • 我必须手动注意 4+3=7,这样宽度才能相加。我更喜欢只指示垂直分隔符的相对位置的解决方案,即 4/7=0.571
  • 我需要minimum height在同一行上指示每个矩形
  • 大量使用定位键和锚点使得代码变得繁琐

我知道不同宽度的嵌套矩形。但是我需要矩形作为节点(而不是路径),因为我需要将文本放入其中。

在 TikZ 手册中,我没有找到任何允许我执行此操作的内置方法。因此,我将非常感谢您提供有关如何改进此代码的指示。

答案1

现在有一个TiKZ解决方案:

我假设一行中的所有矩形都具有相同的高度。每一行都声明为,scopey=row height根据下一行进行移位。每个节点都定义为其fit左下角和右上角的坐标,但只有 x 坐标会发生变化,因为左下角位于 ,y=0而右上角始终位于y=1。这样就不需要minimum widthminimum height定义。节点内的文本用label=center:...

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}[every fit/.style={inner sep=0pt, outer sep=0pt, draw}]

\begin{scope}[y=1.5cm]
\node [fit={(0,0) (7,1)}, label=center:{(1,1)}] {};
\end{scope}

\begin{scope}[yshift=1.5cm,y=1cm]
\node [fit={(0,0) (4,1)}, label=center:{(2,1)}] {};
\node [fit={(4,0) (7,1)}, label=center:{(2,2)}] {};
\end{scope}

\begin{scope}[yshift=2.5cm,y=0.8cm]
\node [fit={(0,0) (2,1)}, label=center:{(3,1)}] {};
\node [fit={(2,0) (5,1)}, label=center:{(3,2)}] {};
\node [fit={(5,0) (7,1)}, label=center:{(3,3)}] {};
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

摘要如下:

bytefield该包可帮助用户创建网络协议规范以及任何其他利用数据字段的内容的插图。

像你这样的简单例子可以用

\documentclass[border=2mm]{standalone}
\usepackage{bytefield}
\begin{document}
\bytefieldsetup{bitheight=1cm}
\begin{bytefield}[bitwidth=1cm]{7}
\bitbox{4}{(2,1)} & \bitbox{3}{(2,2)} \\\bytefieldsetup{bitheight=1.2cm}%
\wordbox{1}{(1,1)}
\end{bytefield}

\begin{bytefield}[bitwidth=1cm]{7}以位为单位声明最长矩形宽度(7)并以可选参数的形式声明位宽度。

之后每个框都用 声明\bitbox{box width in bits}{box contents}。整行框用 声明\wordbox{lines height}{box contents}

bitheight固定每行高度,但可以针对每行进行调整

在此处输入图片描述

bytefield从文档中摘取的更复杂的例子

\documentclass[border=2mm]{standalone}
\usepackage{bytefield}
\begin{document}
\begin{bytefield}[bitwidth=1.1em]{32}
\bitheader{0-31} \\
\begin{rightwordgroup}{RTP \\ Header}
\bitbox{2}{V=2} & \bitbox{1}{P} & \bitbox{1}{X}
& \bitbox{4}{CC} & \bitbox{1}{M} & \bitbox{7}{PT}
& \bitbox{16}{sequence number} \\
\bitbox{32}{timestamp}
\end{rightwordgroup} \\
\bitbox{32}{synchronization source (SSRC) identifier} \\
\wordbox[tlr]{1}{contributing source (CSRC) identifiers} \\
\wordbox[blr]{1}{$\cdots$} \\
\begin{rightwordgroup}{RTP \\ Payload}
\wordbox[tlr]{3}{MPEG-4 Visual stream (byte aligned)} \\
\bitbox[blr]{16}{}
& \bitbox{16}{\dots\emph{optional} RTP padding}
\end{rightwordgroup}
\end{bytefield}
\end{document}

在此处输入图片描述

相关内容