6

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?

1
  • Give a name to the main node, for example (a), then you can access the anchors with \node[above = 2mm of a.one] {description}; which puts the node on top of the first box of the main node (a)
    – percusse
    Apr 21, 2012 at 1:12

2 Answers 2

7

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

0
7

I made some modification from Gonzalo's answer. I remove two libraries calc and positioning and I modified some styles. I changed the order of the creation of the nodes. (stack) -> (Arrow)-> (stack) -> (Arrow)-> (stack)

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

\begin{document}


\begin{tikzpicture}[every text node part/.style={align=left},
                     stack/.style={rectangle split, 
                                   rectangle split parts = 5, 
                                   draw,text width=1.5cm},
                    myarrow/.style={single arrow,
                                    draw,
                                    right = 3pt,
                                    minimum size = 5ex}
                  ]
% we start
\node [stack] (ini)  {%
                      $a=0$     \nodepart{two}
                      $b=10$    \nodepart{three}
                      $c=100$   \nodepart{four}
                      $d=-10$   \nodepart{five}
                      $\cdots$};
% we add an arrow
\node [myarrow] (A) at (ini.east) {} ;  
% after the arrow, the main node at the end
\node [draw,rectangle,align=left,right=3pt] (mid) at (A.east)
            {instruction 0;\\ 
             instruction 1;\\
             $\ldots$\\
             instruction $n$;};
% we add another arrow
\node [myarrow] (B) at (mid.east) {} ;   
% after the arrow, the final node 
\node [stack,right=3pt] (fin) at (B.east)  {%
                                            $a=10$    \nodepart{two}
                                            $b=100$   \nodepart{three}
                                            $c=-10$   \nodepart{four}
                                            $d=110$   \nodepart{five}
                                            $\cdots$};
% labels : I don't like vey much `label= ` it's a shortcut but I think it's more powerful and flexible to use real nodes.
\node [above,align=left]  at (ini.north) {Initial values of\\
                                          variables};
\node [above,align=left]  at (fin.north) {Final values of\\
                                          variables};
\node [above]             at (mid.north) {Computer Program};   

\end{tikzpicture}
\end{document}

enter image description here

5
  • Thanks, I think something must be missing (maybe a library), I can't see the figure when I convert to pdf. Any idea?
    – web2013
    Apr 21, 2012 at 10:47
  • The code compiles fine but I work with pgf 2.1 cvs. What kind of error do you get? I made a try with pgf 2.1 official and it's fine. I think you need to update your distribution Apr 21, 2012 at 10:53
  • Just updated MikTex to 2.9, I don't see any errors but I can't see the figure on my pdf. Anyways after the update, I got the example of Gonzalo's working, so it's Ok :)
    – web2013
    Apr 21, 2012 at 11:47
  • But it's a problem. You compile with what ? Apr 21, 2012 at 11:51
  • OK I got it working by removing all the comments and '%' which were in the middle of latex code like in \node [stack] (ini) {% Btw I'm using Lyx 2.0.3 with MikTex 2.9 on Windows 7
    – web2013
    Apr 21, 2012 at 12:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .