6

The following code gives me the error:

! Package pgf Error: Sorry, the requested layer 'background' is not part of the
 layer list. Please verify that you provided \pgfsetlayers and that 'background
' is part of this list.

But background is in \pgfsetlayers{background,main}. What am I doing wrong?

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepgfplotslibrary{fillbetween}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main} 
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis x line=center, 
    axis y line=center, 
    ticks=none
    ]
    \addplot[color=red,domain=-2:2,name path= mycurve]{x^2};
    \addplot[domain=-2:2,name path= myline]{x-1};
\begin{pgfonlayer}{background}
    \addplot[red!10] fill between [of=myline and mycurve, soft clip={domain=-1:1}];
\end{pgfonlayer}
\end{axis}
\end{tikzpicture}
\end{document}
6
  • 2
    I guess because fill between sets set layers which in turn uses another layer set in which no background layer exists. But why do you want to change the default layer where the fill between stuff is drawn into? And if this should be your goal you could also use the key--value on layer =<layer name> to do so. Commented Nov 29, 2018 at 17:45
  • @StefanPinnow thanks, is there a way to modify the fill between layers?
    – CarLaTeX
    Commented Nov 29, 2018 at 17:48
  • Sure, but this is quite some work. I guess it is much easier to use the "default" layers when set layers is set/active. Could you elaborate your final goal? Commented Nov 29, 2018 at 17:53
  • @StefanPinnow Just to have the filling behind the curves, to see the curve border, I know how to do it with some workaround, I'm wondering if there is a smart method.
    – CarLaTeX
    Commented Nov 29, 2018 at 18:00
  • @StefanPinnow Also the axis in foreground.
    – CarLaTeX
    Commented Nov 29, 2018 at 18:03

2 Answers 2

7

OK, I see meanwhile Stefan Pinnow already showed what the issue is. You need to use set layers or axis on top (or something of that sort) and then draw the filling on the desired layer, see section 4.27.2 of the pgfplots manual. The desired answer seem to be

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis on top,
    axis x line=center, 
    axis y line=center, 
    ticks=none
    ]
    \addplot[color=red,domain=-2:2,name path= mycurve]{x^2};
    \addplot[domain=-2:2,name path= myline]{x-1};
    \addplot[red!10] fill between [of=myline and mycurve, soft clip={domain=-1:1}];
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Here, as kindly pointed out by Stefan Pinnow, the fill between triggers automatically that the shaded area is on the pre main layer.

If you replace axis on top by set layers, you'll get

enter image description here

More options for layers are discussed in section 4.27.2 of the pgfplots manual. These additional options might be relevant if you want to draw several overlapping areas (and/or contours) in a certain order, and you cannot just order the various \addplots to give the desired ordering.

7
  • axis on top!!!! Bad things happen not reading the manuals! (cit.)
    – CarLaTeX
    Commented Nov 29, 2018 at 18:25
  • @CarLaTeX The manual is neither in Italian nor marmot language, so it is really hard to read. (And I thought the quote was "Don't read the manual, bad things can happen." ;-)
    – user121799
    Commented Nov 29, 2018 at 18:27
  • 3
    To be a bit more precise: set layers is automatically set by pgfplots when there is a statement \addplot fill between ..., thus it must not be set explicitly. And also layer pre main is the default layer of \addplot fill betweens which means the "fillings" are always behind all regular stuff drawn in the axis environment. Thus one will get the same result without stating this, too. Commented Nov 29, 2018 at 18:40
  • 1
    To sum up: You only need to add axis on top to get the desired result. Commented Nov 29, 2018 at 18:41
  • 2
    @CarLaTeX chat.stackexchange.com/transcript/message/3973372#3973372 Commented Nov 29, 2018 at 23:10
0

I had a similar problem when trying to draw something on the background layer behind an axis: with

\pgfdeclarelayer{background}
\pgfsetlayers{background,main} 
...
\begin{axis}[]
\begin{pgfonlayer}{background}

I got the error Sorry, the requested layer 'background' is not part of the' is part of this list.

After studying section 4.27.2 of the PGFPLOTS manual, I found that PGFPLOTS creates its own default layers for axis environments: axis background, axis grid, axis ticks, axis lines, axis tick labels, main, axis descriptions, axis foreground.

Thus,

\begin{axis}[]
\begin{pgfonlayer}{axis background} % instead of background

did the trick for me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .