我尝试在 Tikz 中创建类似以下的图表:
我知道如何创建我认为的每一行,我正在做类似的事情:
\tikzstyle{cell} = [rectangle, rounded corners=5pt, thick, draw, text width=0.65cm ]
\node [cell, text width=8.7cm] (n1) {Z};
我认为我可以用类似以下的方式堆叠它们:
\node [cell, text width=8.7cm] (n1) [above=of n2]{Z};
\node [cell, text width=4.1cm] (n2) {X};
\node [cell, text width=4.1cm] (n3) [right=of n2] {Y};
但这样并不能按照我想要的方式堆叠它们。我见过类似的答案,使用诸如“右上方”等内容,但这些答案似乎将它们对角放置,而不是像上图那样对齐。
任何帮助都将不胜感激-谢谢!
答案1
我认为tikz
matrix
和fit
图书馆可以帮助你。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit, matrix, shapes.geometric}
\tikzset{% use tikzset, not tikzstyle
cell/.style={
rectangle, rounded corners=5pt, thick, draw,
}
}
\begin{document}
\begin{tikzpicture}
\matrix[
matrix of nodes, nodes in empty cells,
minimum width=2cm,
column sep=4pt,
row sep=10pt,
text height=2ex,text depth=.25ex,
minimum height=4ex
] (m) {
& & & \\
& & & \\
|[cell]| A & |[cell]| B & |[cell]| C & |[cell]| D \\
} ;
\node[cell, fit=(m-2-1)(m-2-2), inner sep=0pt, label=center: X] {};
\node[cell, fit=(m-2-3)(m-2-4), inner sep=0pt, label=center: Y] {};
\node[cell, fit=(m-1-1)(m-1-4), inner sep=0pt, label=center: Z] {};
\end{tikzpicture}
\end{document}
致谢label=center:
为什么这个节点的文本没有垂直居中?。
答案2
这是 CarLaTeX 解决方案的一个变体。它使用matrix
来表示底行,但上行使用fit
放置在positioning
库中的节点来构建。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit, matrix, positioning, shapes.geometric}
\tikzset{% use tikzset, not tikzstyle
cell/.style={
minimum width=2cm,
text height=2ex,text depth=.25ex,
minimum height=4ex,
rectangle, rounded corners=5pt, thick, draw,
}
}
\begin{document}
\begin{tikzpicture}
\matrix[
matrix of nodes,
nodes = {cell, align=center},
column sep=4pt,
] (m) {
A & B & C & D \\
} ;
\node[cell, fit=(m-1-1)(m-1-2), inner sep=0pt, label=center: X, above left = 10pt and 0pt of m-1-2.north east] (X) {};
\node[cell, fit=(m-1-3)(m-1-4), inner sep=0pt, label=center: Y, above left = 10pt and 0pt of m-1-4.north east] (Y) {};
\node[cell, fit=(X)(Y), inner sep=0pt, label=center: Z, above left = 10pt and 0pt of Y.north east] {};
\end{tikzpicture}
\end{document}
以下解决方案使用tcbraster
来自tcolorbox
包。
\documentclass{article}
\usepackage[most]{tcolorbox}
\tcbset{enhanced, fontupper=\bfseries, notitle, halign=center, valign=center}
\begin{document}
\begin{tcbitemize}[%
raster width=10cm,
raster equal height=rows,
raster columns=4,
raster equal height,
raster every box/.style={height=4ex},
raster column skip=10pt,
raster row skip=4pt,
colback=white]
\tcbitem[raster multicolumn=4] Z
\tcbitem[raster multicolumn=2] X
\tcbitem[raster multicolumn=2] Y
\tcbitem A
\tcbitem B
\tcbitem C
\tcbitem D
\end{tcbitemize}
\end{document}
答案3
使用嵌套循环,没有 TikZ matrix
,也没有任何 TikZ 库。
label=center:#1
我使用了卡拉建议的好技巧。
当然,更好地管理节点长度和坐标是可能的xscale
,但我不知道该如何做。
输出
代码
\documentclass[12pt,tikz]{standalone}
\begin{document}
\tikzset
{
cell/.style=
{
rectangle,
rounded corners=5pt,
thick,
draw,
anchor=west,
inner xsep=-2pt,
minimum height=.7cm,
}
}
\def\xScale{7cm}
\begin{tikzpicture}[x=\xScale]
\foreach \k/\v in {0/{0/Z},1/{0/X,1/Y},2/{0/A,1/B,2/C,3/D}}
{
\foreach \kk/\vv in \v
{
\node
[
cell,
text width=1/2^\k * \xScale,
label=center:\vv,
]
at (\kk/2^\k,-\k)
{};
}
}
\end{tikzpicture}
\end{document}
答案4
作为forest
树......(从其文档第 29 页采用的示例):
\documentclass[margin=3mm]{standalone}
\usepackage{forest}
\begin{document}
\forestset{box/.style={
draw, rounded corners, minimum width=12mm,
no edge,
l=0, l sep=1mm,
s=0, s sep=1mm,
calign=first, anchor=base west,
% content format={\strut\forestoption{content}},
if n children=0{}{
after packing node={
minimum width/.pgfmath=
{s("!l")+max_x("!l")-s("!1")-min_x("!1")},
for children/.wrap pgfmath arg={s+={##1}}{0},
typeset node}}}}
\begin{forest} for tree={box}
[Z
[X
[A][B]
]
[Y
[C]
[D]
]
]
\end{forest}
\end{document}