在 TikZ 中,我有一个样式节点,在本例中是一个带有附加分隔空间和虚线边框的矩阵。我想将这个矩阵的左侧和右侧的附加节点与北虚线边框对齐。
目前,我将某个节点左侧的节点放在矩阵内,并使用yshift
手动对齐框。这可行,但我很好奇是否有办法与样式信息对齐。
我的 MWE:
\begin{tikzpicture}
% Tikz Styles
\tikzset{
item/.style={
draw,
rectangle,
font=\sffamily\scriptsize, align=center,
inner sep=3pt
},
board/.style={
draw,
dashed,
inner sep=3mm,
matrix of nodes
},
inBoard/.style={
item,
solid,
sharp corners,
minimum height=0.8cm,
font=\sffamily\scriptsize\bfseries,
fill=hgrau,
thick
},
outerBox/.style={
item,
solid,
font=\sffamily\scriptsize\bfseries
}
}
% Elements
\matrix(Board)[board] {
\node[label](dashedBoxLabel)[]{Dashed Styled Box};
\node[inBoard] (InBox)
[draw=none,
below of=dashedBoxLabel,
node distance=1.8cm,
minimum height=12mm,
minimum width=4cm]{In box};\\
};
\node[outerBox] (OtherBox)
[left of=dashedBoxLabel,
yshift=-1.6cm,
node distance=4cm,
draw=none,
fill=hgrau,
minimum width = 3cm,
minimum height=6cm]{Other Box};
\end{tikzpicture}
答案1
对于您matrix of nodes
不需要再次提供节点语法。选项可以通过|[...]|
方式传递。
您还可以使用\\[<length>]
它来分隔行,而不是对已经在单元格内的节点的位置进行硬编码。
您可以使用锚定和positioning
库语法。请查看PGF/TikZ 中“right of=”和“right=of”之间的区别
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{matrix,positioning}
\begin{document}
\begin{tikzpicture}[
% Tikz Styles
item/.style={
draw,
rectangle,
font=\sffamily\scriptsize, align=center,
inner sep=3pt
},
board/.style={
draw,
dashed,
inner sep=3mm,
matrix of nodes
},
inBoard/.style={
item,
solid,
sharp corners,
minimum height=0.8cm,
font=\sffamily\scriptsize\bfseries,
fill=gray,
thick
},
outerBox/.style={
item,
solid,
font=\sffamily\scriptsize\bfseries
}
]
% Elements
\matrix[board] (Board) {
Dashed Styled Box\\[1cm]
|[font=\sffamily\scriptsize\bfseries,
fill=gray,
minimum height=12mm,
minimum width=4cm
]| In box\\
};
\node[outerBox] (OtherBox)
[left= 3mm of Board.north west,
anchor=north east,
draw=none,
fill=gray,
minimum width = 3cm,
minimum height=6cm]{Other Box};
\end{tikzpicture}
\end{document}