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 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}
share|improve this question
add comment

4 Answers

up vote 7 down vote accepted

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

share|improve this answer
    
"Who's afraid of the Big Bad Nested-TikZ-Pictures?" (Doesn't quite scan as well as the original) –  Andrew Stacey Apr 25 '12 at 6:52
    
More seriously, your last solution (naming the labels) is the one that I would have posted if you hadn't. It seems the most natural and robust solution. –  Andrew Stacey Apr 25 '12 at 6:53
    
Yes "Who's afraid of the Big Bad Nested-TikZ-Pictures?" !! In the sources of pgf, I found this : \def\tikzpicture{\def\tikzpicture{\begingroup\def\endtikzpicture{\tikzexternal‌​@origendpicture\endgroup} etc. with this comment make sure that nested \tikzpicture are processed normally (tikzlibraryexternal.code.tex by Christian Feuersaenger) –  Alain Matthes Apr 25 '12 at 7:04
    
@AndrewStacey Yes naming the labels is fine but finally perhaps the old method ( adding nodes) is more readable. In this case, the user know that labels are label node. In the pgfmanual the example gives : \node [circle,draw,label={[name=label node]above left:$a,b$}] {};. I don't like very much long lines of code but "everyone does what they want" ! –  Alain Matthes Apr 25 '12 at 7:13
    
I missed the use of local bounding box first time I read this. I didn't know of that - yet again, I learn something new from your answers. –  Andrew Stacey Apr 25 '12 at 8:42
add comment

You can use the current bounding box:

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}

\begin{tikzpicture}[typea/.style={draw,rectangle,font=\itshape}]
\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);

\begin{pgfonlayer}{background}
\node [fill=black!30,fit=(current bounding box.north west) (current bounding box.south east)] {};
\end{pgfonlayer}
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
If you want the fit command to include all anchors of a certain node (as is the case here), you can just provide the node name: fit=(current bounding box) will give you the same result as fit=(current bounding box.north west) (current bounding box.south east). –  Jake Apr 25 '12 at 5:40
add comment

You can just provide the option show background rectangle, background rectangle/.style={fill=lightgray} to the tikzpicture:

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,positioning}
\begin{document}
\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{document}

You can switch off the padding of the background rectangle by using the option tight background.

share|improve this answer
add comment

There must be certainly a better way than this. This method is quite ugly and too manual, but it works.

\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}};
%\node  (set c) [right=of set a,xshift=-0.85cm] {}; % A dummy node not necessary
\node (set d) [right=of set b,xshift=-0.85cm] {}; % Another dummy node

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

\begin{pgfonlayer}{background}
\node [fill=black!30,fit=(set a)(set d)] {}; % enough to add two diagonal nodes
\end{pgfonlayer}

\end{tikzpicture}
\end{document}

enter image description here

If you are having many sets like A, one below the other, it is enough to define one dummy node to the right of last node like in

\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}};
\node [rectangle,draw] (set c) [below=of set b,label=right:$T_3$]                 {\textit{SET C}};
\node [rectangle,draw] (set d) [below=of set c, label=right:$T_4$] {\textit{SET D}};
%\node  (set e) [right=of set a,xshift=-0.85cm] {}; % A dummy node not really needed
\node (set f) [right=of set d,xshift=-0.85cm,yshift=-0.15cm] {}; % Another dummy node, add yshift also as needed.

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

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

\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
add comment

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.