[Tex/LaTex] Remove white bounding box around Tikz picture

tikz-pgf

How can I remove the white bounding box around the gray rectangles?

enter image description here

Here's the tikz code

\begin{tikzpicture}[framed]
\tikzstyle{B1}=[anchor=west,solid,rectangle,black,draw,minimum height=0.45cm,minimum width=1cm,fill=orange!30]
\tikzstyle{B2}=[anchor=west,solid,rectangle,black,draw,minimum height=0.45cm,minimum width=1cm,fill=green!30]
\tikzstyle{B3}=[anchor=west,solid,rectangle,black,draw,minimum height=0.45cm,minimum width=1cm,fill=blue!30]
\fill[fill=gray!30] (0,0) rectangle (8.1,0.6);
\fill[fill=gray!10] (0,0.6) rectangle (8.1,1.2);
\node[B1] at (2,0.9) {};
\node[B2] at (3,0.9) {};
\node[B3] at (4,0.9) {};
\node[B1] at (5,0.9) {};
\node[B2] at (6,0.9) {};
\node[B3] at (7,0.9) {};
\node[B1] at (3,0.3) {};
\node[B2] at (4,0.3) {};
\node[B3] at (5,0.3) {};
\end{tikzpicture}

Best Answer

It is an artifact induced by framed.

From the pgfmanual (section 25):

The size of the background rectangle is determined as follows: We start with the bounding box of the picture. Then, a certain separator distance is added on the sides.

You can use the option inner frame sep=0pt to turn this behavior off, or the switch tight background.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{backgrounds}

\begin{document}

\begin{tikzpicture}[tight background, background rectangle/.style={fill=yellow}, framed]
  \tikzstyle{B1}=[anchor=west,solid,rectangle,black,draw,minimum height=0.45cm,minimum width=1cm,fill=orange!30]
  \tikzstyle{B2}=[anchor=west,solid,rectangle,black,draw,minimum height=0.45cm,minimum width=1cm,fill=green!30]
  \tikzstyle{B3}=[anchor=west,solid,rectangle,black,draw,minimum height=0.45cm,minimum width=1cm,fill=blue!30]
  \path[fill=gray!30] (0,0) rectangle (8.1,0.6);
  \path (0,0.6) rectangle (8.1,1.2);
  \node[B1] at (2,0.9) {};
  \node[B2] at (3,0.9) {};
  \node[B3] at (4,0.9) {};
  \node[B1] at (5,0.9) {};
  \node[B2] at (6,0.9) {};
  \node[B3] at (7,0.9) {};
  \node[B1] at (3,0.3) {};
  \node[B2] at (4,0.3) {};
  \node[B3] at (5,0.3) {};
\end{tikzpicture}

\end{document}

I modified your rectangles a bit, for the example: if the background is tight, it is actually behind the picture, so the frame is invisible behind the rectangles. The yellow rectangle is actually the frame, below.

enter image description here