我正在寻找一种更好的方法来做以下事情:拟合具有非规则形状的节点,就像在 MWE 中一样。这样我就可以为拟合的节点添加标签。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{any/.style={draw,shape=rectangle}}
\tikzset{nxs/.style={xshift=-2mm}}
\tikzset{pxs/.style={xshift=2mm}}
\tikzset{nys/.style={yshift=-2mm}}
\tikzset{pys/.style={yshift=2mm}}
\begin{document}
\begin{tikzpicture}
\node [any] (n1) {N1};
\node [any,right=of n1] (n2) {N2};
\node [any,above=of n2] (n3) {N3};
\draw ([nxs,pys]n1.north west) -- ([nxs,pys]n2.north west) -|
([nxs,pys]n3.north west) -- ([pxs,pys]n3.north east) -|
([pxs,nys]n2.south east) -- ([nxs,nys]n1.south west) -- cycle;
\node [yshift=4mm] at (n1.north) {Cluster};
\end{tikzpicture}
\end{document}
答案1
我还想知道是否有任何自动化的方法可以做到这一点。与此同时,这里有一个使用 fit 库的替代方法。这至少使边距与标准矩形配合一致。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, fit}
\tikzset{any/.style={draw,shape=rectangle}}
\begin{document}
\begin{tikzpicture}
\node [any] (n1) {N1};
\node [any,right=of n1] (n2) {N2};
\node [any,above=of n2] (n3) {N3};
\node[fit=(n1) (n2)] (n12) {};
\node[fit=(n2) (n3)] (n23) {};
\draw (n12.north west) -- (n12.north west-| n23.west) -- (n23.north west)
-- (n23.north east) -- (n23.south east) -- (n12.south west) -- cycle;
\node [inner xsep = 0pt, anchor=south west] at (n12.north west) {Cluster};
\end{tikzpicture}
\end{document}