# Drawing stacks side-by-side in LaTeX

In this question, Alan Munn provided an example of how to draw a stack in LaTeX:

\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}


However, I would like to display 4 stacks side-by-side. How can this be done?

• should be some distance between your "staks"? are all stack have the same number and size of cells? or this stacks form a matrix? – Zarko Dec 5 '18 at 19:57
• It dosn't matter how much distance is between the stacks. The stacks will have different amounts of cells but should all be aligned the the bottom. Cells may have different sizes. – Paradox Dec 5 '18 at 20:12

To place additional stacks just portion the new nodes appropriately:

## Code:

\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] (A) {
\nodepart{two}a
\nodepart{three}b
\nodepart{four}c
\nodepart{five}d
};
\node[stack=5, right of=A] (B) {
\nodepart{two}a
\nodepart{three}b
\nodepart{four}c
\nodepart{five}d
};
\node[stack=5, right of=B] (C) {
\nodepart{two}a
\nodepart{three}b
\nodepart{four}c
\nodepart{five}d
};
\node[stack=5, right of=C] (D) {
\nodepart{two}a
\nodepart{three}b
\nodepart{four}c
\nodepart{five}d
};

\end{tikzpicture}%
\end{document}


with library matrix:

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

\begin{document}
\begin{tikzpicture}[node distance=1cm]
\matrix (m) [matrix of nodes,
nodes={draw, minimum width=1.5em, minimum height=2ex, outer sep=0pt},
row sep    = -\pgflinewidth,
column sep = -\pgflinewidth % <--- as matrix
]
{   a   &   a   &   a   &   a   \\
b   &   b   &   b   &   b   \\
c   &   c   &   c   &   c   \\
d   &   d   &   d   &   d   \\
};
\matrix (n) [right=of m,
matrix of nodes,
nodes={draw, minimum width=1.5em, minimum height=2ex, outer sep=0pt},
row sep    = -\pgflinewidth,
column sep = 2em  % <--- as separate stacks
]
{   a   &   a   &   a   &   a   \\
b   &   b   &   b   &   b   \\
c   &   c   &   c   &   c   \\
d   &   d   &   d   &   d   \\
};
\end{tikzpicture}
\end{document}


Really just 4 fun (and to answer a TikZ question with a non-TikZ answer, usually I am doing the opposite ;-).

\documentclass{article}
\usepackage{youngtab}
\begin{document}
\end{document}


Here I use stackengine instead of tikz. I create the macro \wstack with a comma separated list to stack. The term \wboxstrut defines the minimum vertical footprint of the stacked items, initially set to the footprint of w and later changed to \strut, which will have the effect of aligning all box edges.

It uses \fboxrule and \fboxsep to define the box rule and offset.

\documentclass{article}
\usepackage{stackengine,listofitems}
\def\wboxwidth{.7em}
\def\wboxstrut{\vphantom{w}}
\newcommand\wbox[1]{\fbox{\makebox[\wboxwidth]{#1\wboxstrut}}}
\newcommand\wstack[1]{%
\setsepchar{,}%
\setstackEOL{,}%
\savestack\tmp{\Shortstack{#1}}%
\def\wboxwidth{\wd\tmpcontent}%
\savestack\boxbuild{\wbox{\boxitems[-1]}}%
\foreachitem\x\in\boxitems{%
\ifnum\xcnt=1\relax\else%
\savestack\boxbuild{\stackon[-\fboxrule]{\boxbuild}{\wbox{\boxitems[-\xcnt]}}}%
\fi%
}%
\boxbuild%
}
\begin{document}
\wstack{,a,b,c,d}
\wstack{,a,b,c}
\wstack{b,c,d}