# TikZ: Creating stack with variable width

I am new to TikZ and want to draw a simple stack Diagram. The Diagram should look like:

[a] [b] [c] [d]  |-|
|j|
[f            ]  | |
| |
[g            ]  | |
| |
[h            ]  |_|


I hope my ASCII-Art can be seen. Right now i try to use something like:

\begin{tikzpicture}[x=20pt, y=20pt, node distance=1pt,outer sep = 0pt]

\tikzstyle{box}=[rectangle,draw,anchor=north west,text centered]
\tikzstyle{smallbox}=[box,minimum height=20pt,minimum width=40pt,text width=4em]
\tikzstyle{normalbox}=[box,minimum height=20pt,minimum width=80pt,text width=16em]
\tikzstyle{bigbox}=[box,minimum height=40pt,minimum width=80pt,text width=16em]

\node[smallbox,fill=green!20] (a) at (1,1) {a};
\node[smallbox,fill=green!20] (b) [right = of a] {b};
\node[smallbox,fill=green!20] (c) [right = of b] {c};
\node[smallbox,fill=green!20] (d) [right = of c] {d};

\node[normalbox,fill=green!20] (f) [below = of a] {f};
\node[bigbox,fill=green!20] (g) [below = of f] {g};
\node[normalbox,fill=green!20] (h) [below = of g] {h};

\end{tikzpicture}


But the stacks are not aligned correctly and I am also having problems with the vertical stack (j) to the right.

-
Please look at the preview pane before submitting the question! Then you can see what your post will look like. I'll try and tidy it up for you. –  Hendrik Vogt Jan 14 '11 at 21:19
@hendrik: Sorry, for the wrong looking post. Thank you for cleaning it up! –  mindcry Jan 15 '11 at 9:29

• Specifying below always sets the anchor to north, so you have to specify the below of = a for node f before specifying the anchor. Otherwise your anchor specification will be overwritten.
• The north west anchor of f should be below a.south west, not below a.south (which is the default).
• In your node size calculations you forgot that text width does not include the inner sep, which is 0.3333em by default (so there is an additional 0.6666em per box plus 3 times the distance between nodes and we have to subtract the inner sep of the large box).
• Specifying both minimum width and text width is redundant, especially if the text width is larger.
• Positioning the j node works analogously.

The complete code is:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[x=20pt, y=20pt, node distance=1pt,outer sep = 0pt]

\tikzstyle{box}=[rectangle,draw,anchor=north west,text centered, fill=green!20, inner sep=0.3333em]
\tikzstyle{smallbox}=[box,minimum height=20pt,text width=4em]
\tikzstyle{normalbox}=[box,minimum height=20pt,text width={16em + 3*0.6666em + 3pt}]
\tikzstyle{bigbox}=[normalbox,minimum height=40pt]

\node[smallbox] (a) at (1,1) {a};
\node[smallbox] (b) [right = of a] {b};
\node[smallbox] (c) [right = of b] {c};
\node[smallbox] (d) [right = of c] {d};

\node[below = of a.south west, normalbox] (f) {f};
\node[bigbox] (g) [below = of f] {g};
\node[normalbox] (h) [below = of g] {h};

\node[box, right = of d.north east, anchor=north west,text width=4em, minimum height=103pt] (j) {j};
\end{tikzpicture}
\end{document}


-
Thanks you really much for the solution. It is working flawlessly! –  mindcry Jan 15 '11 at 9:30