Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 14 down vote accepted
\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

share|improve this answer
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
4  
+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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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