# How to draw stack diagram with TiKz?

I want to draw a stack diagram looks like this with TiKZ.

I wonder is there way to draw this diagram with automatic positioning like drawing Automaton? Any idea? Thanks in advance.

-
Does this question help? Creating stack with variable width –  Alan Munn Aug 23 '11 at 21:50
@Alan Munn: Thanks for the link. However, that thread is not exactly what I'm looking for because it draws box on top of another. My situation is slightly different though because it divides a tall rectangle box into smaller ones, by drawing a horizontal line. Any other suggestion? –  Chan Aug 23 '11 at 21:55

You could use the shapes.multipart library and use nodes with parts:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\begin{document}
\begin{tikzpicture}[stack/.style={rectangle split, rectangle split parts=#1,draw, anchor=center}]
\node[stack=5]  {
\nodepart{two}a
\nodepart{three}b
\nodepart{four}c
\nodepart{five}d
};
\end{tikzpicture}

\end{document}


-
Nice one ;) Thanks a lot. It looks very neat indeed. –  Chan Aug 23 '11 at 22:51

You may have a look at drawstack.sty, which provides a few simples macros to draw stacks (tailored for execution stack, with addresses, stack pointer, ...).

-
Welcome to TeX-SX! You may have a look at our starter page for a quick intro if you wish to familiarize yourself with our format. To improve the answer, would you like to add a minimal working example as well as the result it provides? –  Claudio Fiandrino May 6 '13 at 16:24
+1 Nice package, very useful! As we like to be answers on this site to be self-contained, it would be nice if you could add a minimal working example that demonstrates your package. Moreover, if we recommend our own work (which is perfectly okay!) we tend to make this somehow explicit in the answer (e.g., add a "written by me" or "my drawstack package"). –  Daniel May 6 '13 at 20:59