18

I want to draw a stack diagram looks like this with TiKZ. enter image description here

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

2
  • Does this question help? Creating stack with variable width
    – Alan Munn
    Commented Aug 23, 2011 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?
    – roxrook
    Commented Aug 23, 2011 at 21:55

3 Answers 3

20

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}

output of code

1
  • Nice one ;) Thanks a lot. It looks very neat indeed.
    – roxrook
    Commented Aug 23, 2011 at 22:51
10

Here is an example of using drawstack.sty:

\documentclass{article}
\usepackage{drawstack}
% Use this instead if you don't want colors.
% \usepackage[nocolor]{drawstack}
\begin{document}

% The main feature of the package is to define an environment
% drawstack.
\begin{drawstack}
  % Within the environment, draw stack elements with \cell{...}
  \cell{First cell}
  \cell{Second cell}
\end{drawstack}
\end{document}

Stack Example

The example is taken as is from here where you can find more complex examples such as:

  1. Grouping cells into stack frames
  2. Stack and Base pointers
  3. Highlighting some cell e.t.c.
1
  • how to customize the size of each cell in the stack drawn using drawstack package?
    – arunmoezhi
    Commented Nov 30, 2015 at 0:42
5

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, ...).

3
  • 4
    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? Commented May 6, 2013 at 16:24
  • 3
    +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
    Commented May 6, 2013 at 20:59
  • As of 2021, overleaf complains about drawstack using an old library.
    – nikhilweee
    Commented Oct 24, 2021 at 22:42

You must log in to answer this question.

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