# TikZ: How to prevent tikzfillbetween from overlapping the boundary paths?

For this code, the filling between curves overlaps half of the arrows line width as shown along the vertical arrow. So, how to make sure that it doesn't overlap any boundary path?

Additionally, for \tikzfillbetween, I would like to know how make it accept more than two surrounding paths. For example, how to tell \tikzfillbetween that the area to fill is formed by the paths Paxis, Raxis and curve?

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots,tikz}
\usetikzlibrary{shapes.geometric,hobby,arrows.meta}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}[x=1.5mm,y=1.5mm]
\draw[ultra thick, ->, name path=Paxis] (0,0) ++(0,15) -- ++(0,-10) coordinate (tipParrow);
\draw[ultra thick, ->, name path=Raxis] (0,0) ++(0,15) -- ++(10,0) coordinate (tipRarrow);
\draw [ultra thick,name path=curve] [use Hobby shortcut] (tipParrow) ++(0,2) .. ++(2,1) .. ++(2,4.5) .. ([shift={(-2,0)}]tipRarrow);
\tikzfillbetween[ of=curve and Raxis, split] {red};
\end{tikzpicture}
\end{document}


## 1 Answer

put fill on background layer:

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta, backgrounds, hobby, shapes.geometric,}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}[x=1.5mm,y=1.5mm]
\draw[ultra thick, ->, name path=Paxis] (0,0) ++(0,15) -- ++(0,-10) coordinate (tipParrow);
\draw[ultra thick, ->, name path=Raxis] (0,0) ++(0,15) -- ++(10,0) coordinate (tipRarrow);
\draw [ultra thick,name path=curve] [use Hobby shortcut] (tipParrow) ++(0,2) .. ++(2,1) .. ++(2,4.5) .. ([shift={(-2,0)}]tipRarrow);
\scoped[on background layer]
\tikzfillbetween[ of=curve and Raxis, split] {red};
\end{tikzpicture}
\end{document}


edit: above code i would simplify to:

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta, backgrounds, hobby, shapes.geometric,}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}[x=1.5mm,y=1.5mm]
\draw[ultra thick, Straight Barb-Straight Barb, name path=Paxis]
(0,-10) coordinate (tipParrow) |- (10,0) coordinate (tipRarrow);
\draw [ultra thick,name path=curve] [use Hobby shortcut]
(tipParrow) ++(0,2) .. ++(2,1) .. ++(2,4.5) .. ([shift={(-2,0)}]tipRarrow);
\scoped[on background layer]
\tikzfillbetween[of=curve and Paxis, split] {red};
\end{tikzpicture}
\end{document}


now the result is:

your second question: above code partly solve it.

• Thank you! Could you please help me with the second question of how to tell \tikzfillbetween that I need, for example, to fill the area between three paths of Paxis, Raxis and curve? – Diaa Jun 28 '18 at 8:56
• @Diaa You can always combine two intersection segments of two paths to a new path and then fill the region between the new path and another path, see e.g. here. But in this case that is not necessary. – user121799 Jun 28 '18 at 9:34
• @Diaa I guess you are simply missing a  \pgfplotsset{set layers} somewhere. If that does not work, please see section 4.27.2 of the pgfplots manual. Or are you using plain TikZ. (In that case I never got that message, so I'd need an MWE to say more.) – user121799 Aug 26 '18 at 22:36
• @marmot I just deleted my original comment since I found the solution is \tikzfillbetween[of=curve and PRaxes, on layer=background, split] {red}. However, commenting \scoped[on background layer] doesn't change the output for me, which makes me wonder what its meaning is. – Diaa Aug 26 '18 at 22:37
• @Diaa, i suggest that you ask new question where you present your problem. btw, i use \scoped[on background layer] to put fill in background, otherwise it partly cover axis lines. – Zarko Aug 26 '18 at 22:54