tikz add tag
J...S
I wanted to group shapes in a tikzpicture into multiple groups by
drawing backgrounds of different colours around them.

Some of these shapes are members of multiple such groups.

Currently, I have a picture like this:

![topan1.png](/image?hash=b5f00ad15cd5e481d21ab8fb424404374fe23679f24bc46054ef214d0dd374d1)

using this tex source:

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

\begin{tikzpicture}[
  block/.style = {
    draw,
    shape=rectangle,
    fill=gray!20,
    align=center
  },
]
  \node[block] (a1) {a1};
  \node[block, right of=a1] (b1) {b1};
  \draw[->] (a1) -- (b1);
  \scoped[on background layer]
    \node[fill=pink!50,inner xsep=1.5mm, inner ysep=1.5mm,
          fit=(a1) (b1)] {};

  \node[block,right of=b1, xshift=1cm] (a2) {a2};
  \node[block, right of=a2] (b2) {b2};
  \draw[->] (a2) -- (b2);
  \scoped[on background layer]
    \node[fill=blue!40!red!25,inner xsep=1.5mm, inner ysep=1.5mm,
          fit=(a2) (b2)] {};
\end{tikzpicture}

\end{document}
```

Now I want to draw a box-shaped background which consists of all 4
boxes with a colour different from those two colours that have already
been used.

When I draw another 'background' like these two, the new background covers up the older ones.

```
  \scoped[on background layer]
    \node[fill=white!80!gray,inner xsep=1.5mm, inner ysep=1.5mm,
          fit=(a1) (b1) (a2) (b2)] {};
```

Like this:

![topan2.png](/image?hash=2243cd63776708efaa70ad9f06902f34d829c414d236f2f8ddabb975808d648c)

Is there a way to draw this 3rd background by leaving the parts which
have already been covered intact?

Would pgflayers be helpful here?
Top Answer
samcarter
# Manage your own layers

If you create your own layers, you can have as many as you like:


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

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

\begin{document}

\begin{tikzpicture}[
  block/.style = {
    draw,
    shape=rectangle,
    fill=gray!20,
    align=center
  },
]
  \node[block] (a1) {a1};
  \node[block, right of=a1] (b1) {b1};
  \draw[->] (a1) -- (b1);
  \begin{pgfonlayer}{background layer}
      \node[fill=pink!50,inner xsep=1.5mm, inner ysep=1.5mm,
            fit=(a1) (b1)] {};
  \end{pgfonlayer}


  \node[block,right of=b1, xshift=1cm] (a2) {a2};
  \node[block, right of=a2] (b2) {b2};
  \draw[->] (a2) -- (b2);
  \begin{pgfonlayer}{background layer}
    \node[fill=blue!40!red!25,inner xsep=1.5mm, inner ysep=1.5mm,
          fit=(a2) (b2)] {};
  \end{pgfonlayer}
  \begin{pgfonlayer}{backbackground layer}
      \node[fill=white!80!gray,inner xsep=3mm, inner ysep=3mm,
            fit=(a1) (b1) (a2) (b2)] {};  
  \end{pgfonlayer}      
\end{tikzpicture}

\end{document}
```
![Screenshot 2025-01-15 at 20.26.52.png](/image?hash=51e418230f0507e40e6b42f40028a047c58bf5c4d9cec7f5794b9f964c3de37c)

# (Ab)use page background

If it is just this one more layer behind the background you need, you could use the background of the page as a quick workaround:

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

\begin{tikzpicture}[
  block/.style = {
    draw,
    shape=rectangle,
    fill=gray!20,
    align=center
  },
  remember picture
]
  \node[block] (a1) {a1};
  \node[block, right of=a1] (b1) {b1};
  \draw[->] (a1) -- (b1);
  \scoped[on background layer]
    \node[fill=pink!50,inner xsep=1.5mm, inner ysep=1.5mm,
          fit=(a1) (b1)] {};

  \node[block,right of=b1, xshift=1cm] (a2) {a2};
  \node[block, right of=a2] (b2) {b2};
  \draw[->] (a2) -- (b2);
  \scoped[on background layer]
    \node[fill=blue!40!red!25,inner xsep=1.5mm, inner ysep=1.5mm,
          fit=(a2) (b2)] {};
\end{tikzpicture}

\AddToHook{shipout/background}{
  \begin{tikzpicture}[remember picture,overlay]
    \node[fill=white!80!gray,inner xsep=3mm, inner ysep=3mm,
              fit=(a1) (b1) (a2) (b2)] {};
  \end{tikzpicture}
}

\end{document}
```

![Screenshot 2025-01-15 at 20.42.04.png](/image?hash=697f379e8e718edd98e22fcc34168d5a379618b7b7a99ee3da1ed03f6a07e1a3)



# Other ideas:

- use lines instead of filled rectangles
- use semi-transparent shapes (the colours might need some adjustment)

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

\begin{document}

\begin{tikzpicture}[
  block/.style = {
    draw,
    shape=rectangle,
    fill=gray!20,
    align=center
  },
]
  \node[block] (a1) {a1};
  \node[block, right of=a1] (b1) {b1};
  \draw[->] (a1) -- (b1);

      \node[draw=red,dashed,inner xsep=1.5mm, inner ysep=1.5mm,
            fit=(a1) (b1)] {};



  \node[block,right of=b1, xshift=1cm] (a2) {a2};
  \node[block, right of=a2] (b2) {b2};
  \draw[->] (a2) -- (b2);

    \node[draw=blue!40!red,dashed,inner xsep=1.5mm, inner ysep=1.5mm,
          fit=(a2) (b2)] {};

      \node[draw=gray,dashed,inner xsep=3mm, inner ysep=3mm,
            fit=(a1) (b1) (a2) (b2)] {};  

\end{tikzpicture}

\bigskip

\begin{tikzpicture}[
  block/.style = {
    draw,
    shape=rectangle,
    fill=gray!20,
    align=center
  },
]
  \node[block] (a1) {a1};
  \node[block, right of=a1] (b1) {b1};
  \draw[->] (a1) -- (b1);
\scoped[on background layer]
      \node[fill=red,opacity=0.5,inner xsep=1.5mm, inner ysep=1.5mm,
            fit=(a1) (b1)] {};

  \node[block,right of=b1, xshift=1cm] (a2) {a2};
  \node[block, right of=a2] (b2) {b2};
  \draw[->] (a2) -- (b2);
\scoped[on background layer]
    \node[fill=blue!40!red,opacity=0.5,inner xsep=1.5mm, inner ysep=1.5mm,
          fit=(a2) (b2)] {};
\scoped[on background layer]
      \node[fill=lightgray,opacity=0.5,inner xsep=3mm, inner ysep=3mm,
            fit=(a1) (b1) (a2) (b2)] {};  

\end{tikzpicture}

\end{document}
```

![Screenshot 2025-01-15 at 20.32.32.png](/image?hash=a5d17b50b20c1420559a4baf67c09d86781f9abb4474604576acc65ccf2b683e)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.