# Badbox from PGF layer using fit

Why does the code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,fit}
\tikzstyle{internal} = [draw, fill, shape=circle]
\begin{document}
\begin{tikzpicture}[scale=0.6,transform shape,node distance=1.4cm,semithick]
\node[internal] (0)                    {};
\node[internal] (1) [right       of=0] {};
\node[internal] (2) [above right of=1] {};
\node[internal] (3) [above       of=2] {};
\node[internal] (4) [below right of=1] {};
\node[internal] (5) [below       of=4] {};
\node[internal] (6) [below right of=2] {};
\node[internal] (7) [right       of=6] {};
\path (0) edge node[near end]   (e1) {} (1)
(2) edge node[near start] (e2) {} (3)
(4) edge node[near start] (e3) {} (5)
(6) edge node[near start] (e4) {} (7);
\begin{pgfonlayer}{background}
\node[draw=blue!60,thick,rounded corners,fit = (e1) (e2) (e4)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}


with output

have a bad box (see the log file here), while this code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,fit}
\tikzstyle{internal} = [draw, fill, shape=circle]
\begin{document}
\begin{tikzpicture}[scale=0.6,transform shape,node distance=1.4cm,semithick]
\node[internal] (0)                    {};
\node[internal] (1) [right       of=0] {};
\node[internal] (2) [above right of=1] {};
\node[internal] (3) [above       of=2] {};
\node[internal] (4) [below right of=1] {};
\node[internal] (5) [below       of=4] {};
\node[internal] (6) [below right of=2] {};
\node[internal] (7) [right       of=6] {};
\path (0) edge node[near end]   (e1) {} (1)
(2) edge node[near start] (e2) {} (3)
(4) edge node[near start] (e3) {} (5)
(6) edge node[near start] (e4) {} (7);
\begin{pgfonlayer}{background}
\node[draw=blue!60,thick,rounded corners,fit = (e1) (e2) (e3)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}


(where "e4" is changed to "e3" in the 4th-to-last line) with output

does not have a bad box (see the log file here)?

-
Hm, interesting! The combination (e2) (e4) in the fit doesn't produce an underfull box either, while (e1) (e4) does. –  Jake Jun 24 '12 at 20:04
I have no problem with your examples (TeXLive 2011 and TikZ/PGF 2.10). –  Paul Gaborit Jun 24 '12 at 20:04
@PolGab My friend has TeXLive 2011 but still has a badbox, see his log file. –  Tyson Williams Jun 24 '12 at 20:20
@TysonWilliams Sorry... I have used TeXLive 2011 and TikZ/pgf CVS version. –  Paul Gaborit Jun 24 '12 at 20:42

Surprisingly simple issue: This comes from the fact that the fit key adjusts the size of the fitted node by setting the text width key while also activating text centered. Your nodes e1 and e4 are so wide apart that the text in your node (well, the empty text) can't be centered without introducing too much stretch on either side. When you only fit your node around e1 and e3, the stretch isn't large enough to trigger the underfull warning.

A simpler MWE to demonstrate:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}[every node/.style={outer sep=0pt}]
\coordinate (A) at (0,0);
\coordinate (B) at (3.1cm,1);
\node [draw, fit=(A) (B)] {};
\end{tikzpicture}
\end{document}


This generates an underfull warning. If we adjust the position of (B) to (3.0cm,1), the warning disappears. Even simpler, if we have

\node [
text centered,
text width=3.1cm,
] {};


we get the underfull warning as well (no fit involved at all). Changing text width to 3.0cm gets rid of the warning.

So what you can do to get rid of the warnings in your code: Set every fit/.append style=text badly centered. That style tells TikZ to also center the text, but to be more relaxed about too much stretch. In the CVS version, the fit library uses this key by default, which is why PolGab couldn't reproduce the error.

Actually, since you're not using text at all, you could also set align=left or align=right to get rid of the warning, but using text badly centered is a good idea anyway.

-
Excellent! Thank you for being so helpful. There is no way that I would have ever figured out why this was happening. Question though: I have many figures in a paper with this problem. Do I need to have every fit... be an option on each tikzpicture environment or is there a way to make this a global option that applies to all tikzpicture environments? –  Tyson Williams Jun 27 '12 at 14:05
You can just use \tikzset{every fit/.append style... in your preamble. –  Jake Jun 27 '12 at 14:10
Wonderfull! Thanks again for your help :) –  Tyson Williams Jun 27 '12 at 14:14