28

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}

enter image description here

How to fit the rectangle also around the node labels?

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

enter image description here

  • 1
    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
  • 13
    +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

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.