I would like to have two layers behind the background layer: waybackgroundlayer
and boundingboxlayer
and also use the fillbetween
.
The MWE below uses \AxisEnvironmentFig
to graph the curves. The *
option, which disables the fill between
, is used in the second figure where I had to comment out the \DrawBoundingBox
.
I would like to be able to use both the fill between
and have my two additional layers available to me.
References:
There are a several related questions on this site, but I was not able to get them to work.
- pgfplots fillbetween layering issue
- pgfplots: Fill between breaks background layer
- Why I get "the requested layer 'background' is not part of the list" error if it is part of the list?
Failed Attempts:
From pgfplots: Fill between breaks background layer and Page 410 of the pgfplots
manual, I attempted to use
\pgfplotsset{layers/standard/.define layer set={
waybackgroundlayer, boundingboxlayer,
background,
axis background,axis grid,axis ticks,axis lines,axis tick labels,
main,
axis descriptions,axis foreground
}{
grid style={/pgfplots/on layer=axis grid},
tick style={/pgfplots/on layer=axis ticks},
axis line style={/pgfplots/on layer=axis lines},
label style={/pgfplots/on layer=axis descriptions},
legend style={/pgfplots/on layer=axis descriptions},
title style={/pgfplots/on layer=axis descriptions},
colorbar style={/pgfplots/on layer=axis descriptions},
ticklabel style={/pgfplots/on layer=axis tick labels},
axis background@ style={/pgfplots/on layer=axis background},
3d box foreground style={/pgfplots/on layer=axis foreground},
}}
and also
\pgfplotsset{layers/PetersLayers/.define layer set={
waybackgroundlayer, boundingboxlayer, background,
axis background,axis grid,axis ticks,axis lines,axis tick labels,
main,
axis descriptions,axis foreground,
}{/pgfplots/layers/standard},
}
\pgfplotsset{set layers=PetersLayers}
Code:
\documentclass{standalone}
\usepackage{xstring}
\usepackage{xparse}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usetikzlibrary{backgrounds}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\pgfdeclarelayer{foreground}
\pgfdeclarelayer{background}
\pgfdeclarelayer{boundingboxlayer}
\pgfdeclarelayer{waybackgroundlayer}
\pgfsetlayers{waybackgroundlayer,boundingboxlayer,background,main,foreground}
%\pgfplotsset{layers/standard/.define layer set={
% waybackgroundlayer, boundingboxlayer,
% background,
% axis background,axis grid,axis ticks,axis lines,axis tick labels,
% main,
% axis descriptions,axis foreground
% }{
%grid style={/pgfplots/on layer=axis grid},
%tick style={/pgfplots/on layer=axis ticks},
%axis line style={/pgfplots/on layer=axis lines},
%label style={/pgfplots/on layer=axis descriptions},
%legend style={/pgfplots/on layer=axis descriptions},
%title style={/pgfplots/on layer=axis descriptions},
%colorbar style={/pgfplots/on layer=axis descriptions},
%ticklabel style={/pgfplots/on layer=axis tick labels},
%axis background@ style={/pgfplots/on layer=axis background},
%3d box foreground style={/pgfplots/on layer=axis foreground},
%}}
%\pgfplotsset{layers/PetersLayers/.define layer set={
% waybackgroundlayer, boundingboxlayer, background,
% axis background,axis grid,axis ticks,axis lines,axis tick labels,
% main,
% axis descriptions,axis foreground,
% }{/pgfplots/layers/standard},
%}
%\pgfplotsset{set layers=PetersLayers}
\newcommand*{\DrawBoundingBox}{%
\begin{pgfonlayer}{boundingboxlayer}
\draw [red, ultra thick, fill=yellow!25, use as bounding box]
([shift={(-5pt,-5pt)}]current bounding box.south west)
rectangle
([shift={(5pt,+5pt)}]current bounding box.north east);
\end{pgfonlayer}%
}
\NewDocumentCommand{\AxisEnvironmentFig}{% https://tex.stackexchange.com/a/353688/4301
s% #1=* means don't apply "fill between"
}{%
\begin{axis}[
xmin=-4, xmax=6,
ymin=-5, ymax=6,
axis lines=middle,
axis equal image=true,
axis on top,
no markers,
smooth,
domain=-6:6,
%set layers=PetersLayers,
]
%% simplest is reformulate the root function to a parabola ...
%\addplot+ [blue, thick, name path=curve] {sqrt(2*x+6)};
% ... and draw it as parametric plot
\addplot+ [blue, thick, name path=curve] (0.5*x^2-3, x);
\addplot [red, thick, name path=line] {x-1};
\IfBooleanF{#1}{% <--- Modification from 353688
% then you only want to fill the closed segment which number 1
% (the counting starts from 0)
% to to so you don't want to fill anything else
\addplot [fill=none] fill between [
of=curve and line,
% to be able to apply styles to each segment
split,
% then provide the style to fill the named segment
every segment no 1/.style={
fill=green!50,
},
];
}%
\end{axis}
}
\begin{document}
\begin{tikzpicture}
\AxisEnvironmentFig*
\DrawBoundingBox
\end{tikzpicture}
~%% The following does not work as it uses "fill between"
\begin{tikzpicture}
\AxisEnvironmentFig
%\DrawBoundingBox%% <--- What do I need to do above so I can uncomment this ???
\end{tikzpicture}
\end{document}