# How to use “fit” to frame the nodes and labels

I'm using the fit TikZ library to fit a rectangle around two nodes which include labels. However, the rectangle is only fit around the actual nodes, not about their labels:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}

\node [label=label1,draw] (node1) {Node1};
\node [label=label2,draw] (node2) at (4,2){Node2};

\node[fit=(node1)(node2), draw] {};
\end{tikzpicture}
\end{document}


How to fit the rectangle also around the node labels?

-

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}
\node [label=label1,draw] (node1) {Node1};
\node [label={[name=l] label2},draw] (node2) at (4,2){Node2};
\node[fit=(node1)(l), draw] {};
\end{tikzpicture}
\end{document}


-
The other possibility is to avoid label and to create the label with a simple node outside (node2). – Alain Matthes Jun 8 '12 at 6:48
+1, but I would use fit=(node1)(node2)(l1)(l2) just to be at the save side. The labels might change later and be wider then the nodes etc. – Martin Scharrer Jun 9 '12 at 10:17
Yes Martin, you are right ! – Alain Matthes Jun 9 '12 at 10:24