我有一张图表,其中块层堆叠在一起,如图所示。
使用此代码:
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, chains, calc}
\begin{document}
\begin{tikzpicture}[start chain=1 going right, start chain=2 going below, node distance=1mm]
\tikzset{
vert/.style = {draw, rectangle, minimum width=1.9cm, minimum height=1cm, text centered, text width=1.2cm, font=\scriptsize, draw=black, fill=none},
mid/.style = {draw, rectangle, minimum width=8cm+3\pgflinewidth, minimum height=1cm, text centered, text width=8.0cm, font=\scriptsize, draw=black, fill=none},
infra/.style = {draw, rectangle, minimum width=8cm+3\pgflinewidth, minimum height=1cm, text centered, text width=8.0cm, font=\scriptsize, draw=black, fill=none}
}
\node [name=r1c1, on chain=1, vert] {A};
\node [name=r1c2, on chain=1, vert] {B};
\node [name=r1c3, on chain=1, vert] {...};
\node [name=r1c4, on chain=1, vert] {X};
\draw let \p1=($(r1c4.east)-(r1c1.west)$), \n1 = {veclen(\x1,\y1)} in node [name=r3c1, on chain=2, mid, anchor=north west, yshift=-1mm, minimum width=\n1-\pgflinewidth] at (r1c1.south west) {Middle};
\draw let \p1=($(r1c4.east)-(r1c1.west)$), \n1 = {veclen(\x1,\y1)} in node [name=r4c1, on chain=2, infra, minimum width=\n1-\pgflinewidth] {Bottom};
\end{tikzpicture}
\end{document}
现在我想在顶部添加另一层四个块(例如 1、2、...、N),每个块的宽度与 A、B、...、X 相同。并且对于每个(垂直)对,例如 1 和 A,绘制一个虚线矩形来包围它们,并将四对(每对由两个垂直块和虚线包围块组成)均匀分布在Middle
/Bottom
层的宽度上。最后,在整个堆栈图的右侧垂直绘制一个双头箭头,箭头两端分别带有文本Up
和。Down
请原谅我的画得不好,但希望下面的图片能说明我想要的
我摆弄了一下链条包,但很难在顶部添加另外四个块。任何建议都将不胜感激
答案1
作为起点:
编辑(2)
采用到beamer
文档类,这次测试了两次,代码是正确的……
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, calc, chains, fit}
\begin{document}
\begin{frame}[fragile] % <---
\frametitle{Test}
\begin{center}
\begin{tikzpicture}[
node distance = 2mm,
start chain = going right,
box/.style = {draw, minimum width=#1, minimum height=1cm,
text width =\pgfkeysvalueof{/pgf/minimum width}-2*\pgfkeysvalueof{/pgf/inner xsep},
align=center},
FIT/.style = {draw, dashed, inner sep=0.5mm, fit=#1},
box/.default = 19mm
]
\begin{scope}[every node/.append style={box, on chain}]
\node (r1c1) {1};
\node (r1c2) {2};
\node (r1c3) [draw=none] {\dots};
\node (r1c4) {$n$};
%
\node (r2c1) [below=of r1c1] {A};
\node (r2c2) {B};
\node (r2c3) [draw=none] {\dots};
\node (r2c4) {X};
\end{scope}
%
\node[FIT=(r1c1) (r2c1), label=text 1] {};
\node[FIT=(r1c2) (r2c2), label=text 2] {};
\node[FIT=(r1c4) (r2c4), label=text $n$] {};
%
\path let \p1 = ($(r1c4.east)-(r1c1.west)$),
\n1 = {veclen(\x1,\y1)} in
node (r3c1) [box=\n1, below right=1mm and 0mm of r2c1.south west]
{Middle}
node (r4c1) [box=\n1, below=of r3c1] {Bottom};
%
\path[draw=gray, very thick, Straight Barb-Straight Barb]
([xshift=7mm] r1c4.north east) node[below right] {Up}
--
([xshift=7mm] r4c1.south east) node[above right] {Down};
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}
text width
我使用最小宽度参数和所有节点的计算来定义通用样式minimum width
。对于较窄的节点,对于较小的节点,定义默认尺寸 19 毫米,对于较宽的节点,通过计算来定义- 对于虚线节点使用
˙fit
库 - 对于框中的字体大小,我删除了您的选择
\scriptsize
。在我看来,框图现在更美观了。