答案1
您可以使用label
已经存在的节点的选项,也可以使用新的\node
;在下面的示例中,我使用了这两种方法:
\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}
一个小的附加示例仅用于说明将标签放置在节点旁边的两种方法(第二个示例需要库positioning
):
\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}
答案2
我对 Gonzalo 的答案做了一些修改。我删除了两个库calc
,positioning
并修改了一些样式。我改变了节点的创建顺序。(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}