[Tex/LaTex] Tikz diagram with stacks and box

arrowsboxestikz-pgf

I'm trying to make the diagram like this one

in Tikz. I can draw the stacks by adapting this example and the box. But how can I put the description around the boxes.

Also how to add the two arrows between the boxes?

Best Answer

You can use the label option for already existing nodes or you can use new \nodes; in the example below I used both this approaches:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,arrows,calc}

\begin{document}

\begin{tikzpicture}[stack/.style={
  rectangle split, rectangle split parts=5, draw, anchor=center},
  myarrow/.style={single arrow, draw=none}]

\node [stack] (ini)  {$a=0$\nodepart{two}$b=10$%
   \nodepart{three}$c=100$\nodepart{four}$d=-10$\nodepart{five}$\cdots$};

\node [draw,rectangle,align=left,right=of ini,label=above:{Computer Program}] (mid)
  {instruction 0;\\ instruction 1;\\$\ldots$\\instruction $n$;};

\node [stack,right=of mid] (fin) {$a=10$\nodepart{two}$b=100$%
  \nodepart{three}$c=-10$\nodepart{four}$d=110$\nodepart{five}$\cdots$};

\node [above=of ini,anchor=north,align=left] {Initial values of\\variables};
\node [above=of fin,anchor=north,align=left] {Final values of\\variables};

\node [myarrow,draw,anchor=west] at ($(ini.east)+(2.5pt,0)$) {\phantom{te}} ;
\node [myarrow,draw,anchor=west] at ($(mid.east)+(2.5pt,0)$) {\phantom{te}} ;

\end{tikzpicture}

\end{document}

enter image description here

A little additional example just to illustrate the two mentioned methods to place labels next to nodes (the second example requires the positioning library):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[mynode/.style={draw, anchor=center}]

\node [mynode,label={label}] at (0,0)  {test node};
\node [mynode,label=below:{label}] at (0,-2)  {test node};
\node [mynode,label=west:{label}] at (0,-4)  {test node};
\node [mynode,label=east:{label}] at (0,-6)  {test node};
\node [mynode,label=east:{label1},label=west:{label2}] at (0,-8)  {test node};

\node [mynode] at (6,0) (a) {test node};
\node [above=2mm of a] {label1};
\node [below right=2mm of a] {label2};
\node [below left=4mm of a] {label3};
\end{tikzpicture}

\end{document}

enter image description here