具有与块大小成比例的内存布局

具有与块大小成比例的内存布局

我从网上获得了这个代码,它看起来像这样。

\documentclass[border=10pt]{standalone} 
\usepackage{tikz}
\begin{tikzpicture}[node distance=0 cm,outer sep = 0pt,
        inner sep = 2pt,font=\scriptsize]
    \tikzset{field/.style={align=center,shape=rectangle,
    minimum height=0.7cm,draw}}
    \tikzset{darkfield/.style={fill=gray!10,align=center,shape=rectangle,
    minimum height=0.7cm,draw}}

    \node [field]                      (mac)     {16-byte\\MAC};
    \node [darkfield,right=of mac]     (tlen)    {2-byte\\Total length};
    \node [darkfield,right=of tlen]    (plen)    {2-byte\\Payload length};
    \node [darkfield,right=of plen]    (flags)   {1-byte\\Flags};
    \node [darkfield,right=of flags]   (payload) {(optional)\\Payload};
    \node [darkfield,right=of payload] (padding) {(optional)\\Padding};

    \draw [latex-latex] ($ (mac.south west) - (0,0.2) $) -- node [auto,swap]
    {Plain} ($ (mac.south east) - (0,0.2) $);
    \draw [latex-latex] ($ (tlen.south west) - (0,0.2) $) -- node [auto,swap]
    {Encrypted and authenticated} ($ (padding.south east) - (0,0.2) $);
\end{tikzpicture}
\end{document}

在此处输入图片描述

似乎每个块的大小不成比例。我的意思是,我认为 2 个字节应该比 1 个字节块大两倍。我们可以提前设置 1 个字节块的大小吗?任何帮助都值得感激。

答案1

您可以使用该选项text width为所有节点设置固定宽度,然后创建一个将长度设置为两倍的twobit样式。text width

但是,如果节点内的文本太长,它将被分成更多行,并且节点的高度将变得更大,这看起来不太好看。因此,您需要调整 1 位宽度以适合节点的内容。

当然,第一个框实际上应该是 1 字节框宽度的 16 倍,这会使整个图表非常宽。我不确定这是否是您想要的。

\documentclass[border=10pt]{standalone} 
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[
    node distance=0 cm, 
    font=\scriptsize,
    every node/.style={
        outer sep=0pt,
        inner sep=2pt,
    },
    field/.style={
        rectangle,
        draw,
        align=center, 
        minimum height=0.7cm,
        text width=3em
    },
    darkfield/.style={
        field, 
        fill=gray!10
    },
    twobit/.style={
        text width=6em
    }]

    \node [field]                               (mac)     {16-byte\\MAC};
    \node [darkfield, twobit, right=of mac]     (tlen)    {2-byte\\Total length};
    \node [darkfield, twobit, right=of tlen]    (plen)    {2-byte\\Payload length};
    \node [darkfield, right=of plen]            (flags)   {1-byte\\Flags};
    \node [darkfield, twobit, right=of flags]   (payload) {(optional)\\Payload};
    \node [darkfield, twobit, right=of payload] (padding) {(optional)\\Padding};

    \draw [latex-latex] ([yshift=-0.2cm] mac.south west) -- node [auto,swap]
    {Plain} ([yshift=-0.2cm] mac.south east);
    \draw [latex-latex] ([yshift=-0.2cm] tlen.south west) -- node [auto,swap]
    {Encrypted and authenticated} ([yshift=-0.2cm] padding.south east);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容