I'm trying to make the diagram
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?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityI'm trying to make the diagram
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?
You can use the label
option for already existing nodes or you can use new \node
s; 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}
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}
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}
\node [stack] (ini) {%
Btw I'm using Lyx 2.0.3 with MikTex 2.9 on Windows 7
(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)