3

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}
  • 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. – Stefan Pinnow Nov 29 '18 at 17:45
  • @StefanPinnow thanks, is there a way to modify the fill between layers? – CarLaTeX Nov 29 '18 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? – Stefan Pinnow Nov 29 '18 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 Nov 29 '18 at 18:00
  • @StefanPinnow Also the axis in foreground. – CarLaTeX Nov 29 '18 at 18:03
4

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.

  • axis on top!!!! Bad things happen not reading the manuals! (cit.) – CarLaTeX Nov 29 '18 at 18:25
  • 2
    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. – Stefan Pinnow Nov 29 '18 at 18:40
  • 1
    To sum up: You only need to add axis on top to get the desired result. – Stefan Pinnow Nov 29 '18 at 18:41
  • 2
  • 1
    @marmot You could edit your answer and add what Stefan pointed out, for future users who'll have the same problem, thanks! – CarLaTeX Nov 30 '18 at 3:57

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.