Clip leaks when used in an "on background layer" scope?
Brief outline of the bug
It seems that when using a scope with on background layer, the clipping areas leak to other on background layer scopes unless it is protected by another scope.
I am not sure if this is expected behavior or not.
Minimal working example (MWE)
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[]
\draw (0,0) rectangle (1,1);
\begin{scope}[on background layer]
\begin{scope}
\clip (0,0) rectangle (1,1);
\filldraw[yellow] (0,0) circle[radius=1cm];
\end{scope}
\end{scope}
\begin{scope}[on background layer]
\begin{scope}
\clip (-1,-1) rectangle (1,1);
\filldraw[orange] (0,0) circle[radius=.2cm];
\end{scope}
\end{scope}
\end{tikzpicture}
\end{document}
results in
with correct clipping, but if you comment out the inner scope environments, you get:
In both, I am a bit surprised by the bounding box, though.
This seems to me the expected behavior.
When multiple \begin{scope}[on background layer] are used, their environment contents accumulate. So without the inner scope env,
\begin{scope}[on background layer]
\clip (0,0) rectangle (1,1);
\filldraw[yellow] (0,0) circle[radius=1cm];
\end{scope}
\begin{scope}[on background layer]
\clip (-1,-1) rectangle (1,1);
\filldraw[orange] (0,0) circle[radius=.2cm];
\end{scope}
is equivalent to
\begin{scope}[on background layer]
\clip (0,0) rectangle (1,1);
\filldraw[yellow] (0,0) circle[radius=1cm];
\clip (-1,-1) rectangle (1,1);
\filldraw[orange] (0,0) circle[radius=.2cm];
\end{scope}
Then according to /tikz/clip doc,
Multiple clippings accumulate, that is, clipping is always done against the intersection of all clipping areas that have been specified inside the current scopes. The only way of enlarging the clipping area is to end a
{scope}.
Therefore the clipping area used for \filldraw[orange]...; is still (0,0) rectangle (1,1).
The ambiguous point might be, why are two \path (\clip is an abbr. of \path[clip]) used in different \begin{scope}[on background layer] considered (and finally used) in the same scope?
In my understanding (which might be wrong) \begin{scope}[on background layer] acts like a collector, not a real scope. Unfortunately the pgfmanual doesn't mention this.
Ahh, ok, expected behavior, so. BTW, this is a simplified example; I applied the clip to different pics around my drawing, and the background was disappearing ;-).
Maybe the "collector" behavior of scope in the case that on background layer is used should be added to the manual. My drawing worked ok until I added that option, and nothing hinted that the clip would not be restricted to the current scope.
I'm still surprised about the bounding box thing, but it seems that the clip rectangle of the second scope is taken as the new BB, so maybe it's also expected (I thought that the clip region would have been ignored; but I know how to solve this with overlay.
Ok, thanks as always, @muzimuzhi! Very helpful. I'll close this issue now. Feel free to reopen if you want to track this as a documentation issue.
I tried different combinations of clipping, scopes, and layers, and found a wired case. It shows the drawings added to background layer inherits the main layer clipping area, but only in part.
\documentclass[margin=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\fbox{\begin{tikzpicture}
\clip (0,0) rectangle (1,1);
\begin{scope}[on background layer]
\fill[orange] (0,0) circle[radius=1cm];
\end{scope}
\end{tikzpicture}}
\end{document}