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.

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

3 Answers 3


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

\begin{tikzpicture}[stack/.style={rectangle split, rectangle split parts=#1,draw, anchor=center}]
\node[stack=5]  {


output of code

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

Here is an example of using drawstack.sty:

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

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

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.
  • how to customize the size of each cell in the stack drawn using drawstack package?
    – arunmoezhi
    Nov 30, 2015 at 0:42

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

  • 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? 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
    May 6, 2013 at 20:59
  • As of 2021, overleaf complains about drawstack using an old library.
    – nikhilweee
    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 .