我有两个节点位于一个拟合节点内。拟合节点也有一个标签。下面 MWE 显示的问题:标签与两个内部节点重叠。
问:如何增加内部节点上方的适合节点高度,以便标签 100% 可见?我可以增加适合节点的最小高度,但这也会在内部节点下方添加填充。我不想这样。内部节点应该底部对齐。仅在内部节点顶部才需要额外的空间/填充。
或者我应该将标签设为一个单独的节点,将其放置在内部节点上方并将这三个节点组合在一起而不使用实际的label
键?
\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{positioning, backgrounds, fit}
\tikzset{
base/.style = {draw=black, minimum height=2cm},
}
\begin{document}
\begin{tikzpicture}[]
\node (Foo)[base] {Foo};
\node (Bar)[base, right=of Foo] {Bar};
\begin{scope}[on background layer]
\node [draw=black!50, fit=(Foo) (Bar), label={[anchor=north]north:$Long label$}] (Fit) {};
\end{scope}
\draw[->](Foo) -- (Bar);
\end{tikzpicture}
\end{document}
答案1
您可以编写一个名为 的样式fit label
,用于测量标签并相应地扩展拟合。测量在 中完成height("#1")
,拟合向上移动该距离的一半(加上通常的inner sep
),并通过调整 来增加边界框inner ysep
。
\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{positioning, backgrounds, fit}
\tikzset{
base/.style = {draw=black, minimum height=2cm},
}
\begin{document}
\begin{tikzpicture}[fit label/.style={yshift={(height("#1")+4pt)/2},
inner ysep={(height("#1")+8pt)/2},
label={[anchor=north,font=\itshape]north:#1}}]
\node (Foo)[base] {Foo};
\node (Bar)[base, right=of Foo] {Bar};
\begin{scope}[on background layer]
\node [draw=black!50, fit=(Foo) (Bar),
fit label=Long Text] (Virtual) {};
\end{scope}
\draw[->](Foo) -- (Bar);
\end{tikzpicture}
\end{document}
或者,您可以(手动)使其变大。
\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{positioning, backgrounds, fit}
\tikzset{
base/.style = {draw=black, minimum height=2cm},
}
\begin{document}
\begin{tikzpicture}[]
\node (Foo)[base] {Foo};
\node (Bar)[base, right=of Foo] {Bar};
\begin{scope}[on background layer]
\node [draw=black!50, fit={(Foo) (Bar) ([yshift=1em]Foo.north)},
label={[anchor=north,font=\itshape]north:Virtual Machine}] (Virtual) {};
\end{scope}
\draw[->](Foo) -- (Bar);
\end{tikzpicture}
\end{document}