# A memory layout that has proportional size of block

I got this code from an web and it looks like this.

\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};

\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}

It seems the size of each block are not proportional. I meant I think 2 bytes should be twice bigger than the 1 byte block. Can we set the size of 1byte block in advance? Any help appreciates.

• Are the (optional) payload and (optional) padding 2-byte or 1-byte? Aug 15 at 21:02
• The nodes are as wide as their content. What should happen when the text is wider than the node should be? Aug 15 at 21:07

You can use the option text width so set a fixed width for all nodes and then create a twobit style that sets text width two twice that length.

If the text inside the node is too long, however, it will be split over more lines and the heigth of the node will become larger which won't look nice. So, you need to adjust the 1-bit width to fit the contents of the nodes.

Also, of course, the first box should actually be 16 times the width of a 1-byte box, which will make the whole diagram really wide. I am not sure whether this is what you intended.

\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};