我想在 TikZ 中创建一个由两部分组成的节点。到目前为止,我尝试了以下操作
\usepackage{tikz}
\usetikzlibrary{shapes}
\tikzstyle{splitbox} = [rectangle split, rectangle split parts=2, minimum width=3cm, minimum height=2cm, text centered, text width=3cm, draw=black, rectangle split part fill={white, lightgray}]
\begin{document}
\begin{tikzpicture}[node distance=4cm]
\node (node) [splitbox] {\nodepart{one} The whole thing
\nodepart{two} A part of it};
\end{tikzpicture}
\end{document}
这给了我一个结果
到目前为止一切顺利。但实际上我希望它看起来更像
有人知道我该如何实现吗?我已经在谷歌上搜索了好几个小时。“部分”节点部分应该始终位于箭头开始的位置。非常感谢您的建议!
答案1
另一种解决方案:
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{fit,shapes,positioning}
\begin{document}
\begin{tikzpicture}[
node distance=0mm,
every node/.style = {rectangle, inner sep=0mm, outer sep=0mm,
align=center, minimum height=15mm},
]
\node (t) [text width=5cm] {The whole thing};
\node (b) [text width=3cm,draw,fill=lightgray,
below=of t] {A part of it};
\node [draw,fit=(t) (b)] {};
\end{tikzpicture}
\end{document}
答案2
像这样 :
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning}
\begin{document}
\begin{tikzpicture}[node distance=4cm]
\node [name=n1, rectangle, minimum width=5cm, minimum height=3cm,
text centered, text width=3cm, draw=black] {};
\node [name=t, rectangle, minimum width=5cm, minimum height=1.5cm,
text centered, text width=3cm, below=(0mm of n1.north)] {The whole thing};
\node [name=n2,rectangle, minimum width=3cm, minimum height=1.5cm,
text centered, text width=3cm, anchor=south, above=(0cm of n1.south),
draw=black,fill=lightgray] {A part of it};
\end{tikzpicture}
\end{document}
答案3
另一种选择是将唯一node
定义为matrix
和matrix of nodes
:
\documentclass[border=2mm,tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[%
part/.style={draw, fill=black!30},
whole/.style={
matrix,
draw,
matrix of nodes,
inner xsep=3mm,
inner ysep=-.5\pgflinewidth,
nodes={minimum height=12mm},
row 2/.style={nodes={part}}
}]
\node[whole] (A) {
The whole thing\\
A part of it\\
};
\end{tikzpicture}
\end{document}