在这问题,Alan Munn 提供了一个如何在 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}
但是我想并排显示 4 个堆栈。该怎么做?
答案1
要放置额外的堆栈,只需适当分配新节点:
代码:
\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}
答案2
与库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}
答案3
答案4
这里我使用stackengine
而不是tikz
。我使用逗号分隔的列表来创建要堆叠的宏\wstack
。术语\wboxstrut
定义堆叠项目的最小垂直占用空间,最初设置为 的占用空间w
,后来更改为\strut
,这将具有对齐所有框边缘的效果。
自动调整内容宽度。
它使用\fboxrule
和\fboxsep
来定义盒子规则和偏移。
\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}%
\readlist\boxitems{#1}%
\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}
\wstack{,aaa,b,c,d}\qquad
\def\wboxstrut{\strut}
\wstack{,a,b,c,d}
\wstack{,a,b,c}
\wstack{b,c,d}
\wstack{,aaa,b,c,d}
\end{document}