[Tex/LaTex] Draw rectangle around nodes with tikz


I have a lot of nodes placed with tikz. Now I need to draw an rectangle around all these nodes.

I can draw the rectangle manually with

\draw [fill=gray!5] (2, -6) rectangle (18, 6.5) 
(2, 6.5) node[below right,fill,draw] {Rectangle Title};

All my nodes are named and placed relatively:

\node [below=5cm of nodeA] (nodeB) {Node B text};

Best Answer

Let suppose, that your nodes have names. Among them are most outside northernmost, southernmost, easternmost and westernmost. In this case, as above comments say, the simple solution is:

  • in preamble put TikZ library fit
  • in your TikZ picture use \node[draw,inner sep=2mm,label=below:Rectangle Title,fit=(northernmost) (southernmost) (easternmost) (westernmost)] {};