[Tex/LaTex] Fitting background in Tikz

backgroundstikz-pgf

I need that the grey background also includes the labels T1 and T2

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}
\begin{tikzpicture}
\node [rectangle,draw] (set a) [label=right:$T_1$]                 {\textit{SET A}};
\node [rectangle,draw] (set b) [below=of set a, label=right:$T_2$] {\textit{SET B}};

\draw [->] (set a) to node [auto] {\textit{q}} (set b);

\begin{pgfonlayer}{background}
\node [fill=black!30,fit=(set a) (set b)] {};
\end{pgfonlayer}

\end{tikzpicture}
\end{document}

Best Answer

Jake's answer is correct but you have a problem if you want a lightgray wrapper and a different background.

Then if you add something to the picture; you need to use nested tikzpictures (I'm not afraid of this) . Sometimes you need to use nested pictures with caution.

\begin{tikzpicture}
  \node at (5,0){extra};
\node {\begin{tikzpicture}[
      show background rectangle, 
      background rectangle/.style={fill=lightgray},
      box/.style={draw, font=\itshape}
  ]
  \node [box] (set a) [label=right:$T_1$]                 {SET A};
  \node [box] (set b) [below=of set a, label=right:$T_2$] {SET B};

  \draw [-latex] (set a) to node [auto] {\textit{q}} (set b);
\end{tikzpicture}}; 
\end{tikzpicture}

Gonzalo's answer s fine also but if you add an extra node, you need to program the picture with order in this case, I think it's better to use a scope and local bounding box like this :

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,positioning,fit}
\begin{document} 

\begin{tikzpicture}[typea/.style={draw,rectangle,font=\itshape}]
  \node at (5,0){extra}; 
  \begin{scope}[local bounding box=bb]
     \node [typea] (set a) [label=right:$T_1$] {SET A};
     \node [typea] (set b) [below=of set a, label=right:$T_2$] {SET B};
     \draw [->] (set a) to node [auto,font=\itshape] {q} (set b); 
  \end{scope}

   \begin{pgfonlayer}{background}
     \node [fill=black!30,fit=(bb)] {};
  % Jake's remark is fine, (bb) avoids (bb.north west) (bb.south east)
   \end{pgfonlayer}
  \end{tikzpicture}
\end{document}  

Before the option label ( for old users of TikZ) we used simple nodes and in this case you can avoid dummy nodes

\begin{tikzpicture}[
      box/.style={draw, font=\itshape}
  ]
  \node [box] (set a)  {SET A};
  \node [box] (set b) [below=of set a] {SET B};
  \node[anchor=west,right=6pt] (T1) at (set a.east){$T_1$};
  \node[anchor=west,right=6pt] (T2) at (set b.east){$T_2$};  
  \draw [-latex] (set a) to node [auto] {\textit{q}} (set b); 

\begin{pgfonlayer}{background}
   \node [fill=black!30,fit=(set a) (T2)] {};
\end{pgfonlayer}
\end{tikzpicture} 

Finally you can use label without dummy nodes with the option name, I use pgf 2.1 CVS and perhaps this option is a new one (I don't know)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,positioning,fit}
\begin{document}
\begin{tikzpicture}
\node [rectangle,draw] (set a) [label={[name=T1]right:$T_1$}]                 {\textit{SET A}};
\node [rectangle,draw] (set b) [below=of set a, label={[name=T2]right:$T_2$}] {\textit{SET B}};

\begin{pgfonlayer}{background}
   \node [fill=black!30,fit=(set a) (T2)] {};
\end{pgfonlayer}

\end{tikzpicture}
\end{document}  

enter image description here