4

I would like to draw a bounding box around a group of nodes to indicate a relationship. I am doing this using the tikz fit page as shown in the attached code.

I now want to change the background colour for the bounding box.

When I add the fill in the node that calculates the fill region, my nodes are over by the fill. How can fill the bounding box region without over writing my existing nodes.

Do I redraw my nodes over the top again? Or is there a better solution?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit}

\tikzset{block/.style={rectangle, draw}}

\begin{document}
\begin{tikzpicture}

\node[block] (node1) { Node1 };
\node[block, right=of node1] (node2) { Node2 };
\node[block, below=of node2] (node3) { Node3 };

\node [
   dashed,
   draw,
   %fill={rgb:red,230;green,84;blue,0},    %% This over writes my nodes
   inner ysep=0.25cm,inner xsep=0.25cm,
   fit=(node1)(node2)(node3)] (box) {};

\end{tikzpicture}
\end{document}
6

Another solution (without backgrounds library), in the case you can put all the nodes on the same path, is to use the option behind path, like this :

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{positioning,fit}

\begin{document}
  \begin{tikzpicture}[block/.style={rectangle, draw, fill=white}]
    \path
      node[block] (node1) { Node1 }
      node[block, right=of node1] (node2) { Node2 }
      node[block, below=of node2] (node3) { Node3 }
      node [behind path, % <------
            dashed,
            draw,
            fill={rgb:red,230;green,84;blue,0},
            inner sep=0.25cm,
            fit=(node1)(node2)(node3)] (box) {};
  \end{tikzpicture}
\end{document}

enter image description here

3

enter image description here

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

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\tikzset{block/.style={rectangle, draw,fill=white}}

\begin{document}
\begin{tikzpicture}

\node[block] (node1) { Node1 };
\node[block, right=of node1] (node2) { Node2 };
\node[block, below=of node2] (node3) { Node3 };

\begin{pgfonlayer}{background}
\node [
   dashed,fill=red,
   draw,
   %fill={rgb:red,230;green,84;blue,0},    %% This over writes my nodes
   inner ysep=0.25cm,inner xsep=0.25cm,
   fit=(node1)(node2)(node3)] (box) {};

\end{pgfonlayer}

\end{tikzpicture}
\end{document}
3

Here is a way to do it using scopes and a local bounding box.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,backgrounds}

\tikzset{block/.style={rectangle, draw}}

\begin{document}
\begin{tikzpicture}

\begin{scope}[local bounding box=box1]
\node[block] (node1) { Node1 };
\node[block, right=of node1] (node2) { Node2 };
\node[block, below=of node2] (node3) { Node3 };
\path (box1.south west) ++(-.25cm,-.25cm)
      (box1.north east) ++(.25cm, .25cm);% add margins
\end{scope}

\begin{scope}[on background layer]
\draw[dashed,fill=red] (box1.south west) rectangle (box1.north east);
\end{scope}

\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.