[Tex/LaTex] Drawing a memory layout diagram with Tikz

diagramsmemorytikz-pgf

I'd like to draw a memory layout diagram in Tikz that looks a bit like the following:
enter image description here
Note that the original was an SVG file, and conversion to PNG causes the weird [Not supported by viewer] errors, not sure what that's about.

Anyway, how can I recreate that diagram in Tikz, but without the numbered stages, and with the sections starting at the very left?

I tried to modify this answer to near it to the posted image, but I don't know enough Tikz to get it quite there.

Best Answer

Something like this:

enter image description here This is done using tikz but to hide the technicalities I have defined a macro \MemoryLayout that accepts a coma separated list of x-coordinates/colours/labels so that the image above was produced by:

\MemoryLayout{
  12/blue!10/A,
  21/orange!20/B,
  30/red!30/C,
  39/yellow!30/D,
  48/green!30/E,
  62/white/\relax
}

Use a label of \relax when you do not want a label.

All that this macro does is loop over the input data to draw the picture using standard tikz commands. Here is the full code:

\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{decorations.pathreplacing}

\newcommand\MemoryLayout[1]{
  \begin{tikzpicture}[scale=0.3]
     \draw[thick](0,0)--++(0,3)node[above]{$0$};
     \foreach \pt/\col/\lab [remember=\pt as \tp (initially 0)] in {#1} {
       \foreach \a in {\tp,...,\pt-1} {
          \draw[fill=\col](-\a,0) rectangle ++(-1,2);
       }
       \draw[thick](-\pt,0)--++(0,3)node[above]{$\pt$};
       \if\lab\relax\relax\else
         \draw[thick,decorate, decoration={brace,amplitude=4mm}]
            (-\tp,-0.2)--node[below=4mm]{\lab} (-\pt,-0.2);
       \fi
     }
  \end{tikzpicture}
}

\begin{document}

  \MemoryLayout{
    12/blue!10/A,
    21/orange!20/B,
    30/red!30/C,
    39/yellow!30/D,
    48/green!30/E,
    62/white/\relax
  }

\end{document}

To remove the numbers just comment out the two \draw[thick]...; commands.