7

I am seeking a better way of doing the following: fitting nodes with a non regular shape, like it is in the MWE. So that I can give a label to the fitted nodes.

\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}

enter image description here

1
  • 2
    This might be very complicated depending on the number of nodes: see here and here for some examples in a similar direction. – Tom Bombadil May 9 '13 at 22:50
5

I would also like to know if there is any automated way to do this. In the meantime, here is an alternative which uses the fit library. This at least makes the margins consistent with standard rectangular fits.

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.