2

I would like to fill the space between the three curves in this MWE

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}[scale=3]
    \draw[name path=A] (0,0) to[out=5, in = 185] node[pos=0,left] {A1} node[pos=1,right] {A2} (3,3);
    \draw[name path=B] (2,0.5) to[out=135,in=-90] node[pos=0,below right] {B1} node[pos=1,above] {B2} (-1,4);
    \draw[name path=C] (-0.5,1) to[out=90,in=135] node[pos=0,below] {C1} node[pos=1,below right] {C2} (2.5,2);

    \path[name intersections={of=A and B,by=AB}];
    \draw (AB) node[below=1em] {AB} circle[radius=2pt];

    \path[name intersections={of=A and C,by=AC}];
    \draw (AC) node[above=1em] {AC} circle[radius=2pt];

    \path[name intersections={of=B and C,by=BC}];
    \draw (BC) node[above=1em] {BC} circle[radius=2pt];
\end{tikzpicture}
\end{document}

LaTeX Output

In Fill the Area between two “\draw” lines, the question is similar, but there are only two draw lines, so the area can be filled all at once using fillbetween.

The technique in TikZ: Drawing an arc from an intersection to an intersection seems like it should work here, but I have not been able to make it work:

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usepgfplotslibrary{fillbetween}

\makeatletter
\tikzset{use path/.code=\tikz@addmode{\pgfsyssoftpath@setcurrentpath#1}}
\makeatother

\pgfdeclarelayer{bg}
\pgfsetlayers{bg,main}

\begin{document}
\begin{tikzpicture}[scale=3]
    \draw[name path=A] (0,0) to[out=5, in = 185] node[pos=0,left] {A1} node[pos=1,right] {A2} (3,3);
    \draw[name path=B] (2,0.5) to[out=135,in=-90] node[pos=0,below right] {B1} node[pos=1,above] {B2} (-1,4);
    \draw[name path=C] (-0.5,1) to[out=90,in=135] node[pos=0,below] {C1} node[pos=1,below right] {C2} (2.5,2);

    \path[name intersections={of=A and B,by=AB}];
    \draw (AB) node[below=1em] {AB} circle[radius=2pt];

    \path[name intersections={of=A and C,by=AC}];
    \draw (AC) node[above=1em] {AC} circle[radius=2pt];

    \path[name intersections={of=B and C,by=BC}];
    \draw (BC) node[above=1em] {BC} circle[radius=2pt];

    \begin{pgfonlayer}{bg}
    \draw [magenta,fill=green,intersection segments={
                of=A and B,
                sequence={L2--R2}
              },name path=ABpath];
    \fill[orange,intersection segments={
                of=ABpath and C,
                sequence={L2--R2}
              }];
    \end{pgfonlayer}
\end{tikzpicture}
\end{document}

MWE2 output

In this second MWE, ABpath is intended to follow curve A from A2 to AB and then curve B from AB to B2, but the straight magenta line from A2 to AB suggests that it does not. I think I should be able to use an intersection of ABpath and C to define the boundaries of the area to be filled, but the orange fill illustrates that I haven't gotten it right.

0
2

Your approach works, you only need to reverse the right path.

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\makeatletter
\tikzset{use path/.code=\tikz@addmode{\pgfsyssoftpath@setcurrentpath#1}}
\makeatother

\pgfdeclarelayer{bg}
\pgfsetlayers{bg,main}

\begin{document}
\begin{tikzpicture}[scale=3]
    \draw[name path=A] (0,0) to[out=5, in = 185] node[pos=0,left] {A1} node[pos=1,right] {A2} (3,3);
    \draw[name path=B] (2,0.5) to[out=135,in=-90] node[pos=0,below right] {B1} node[pos=1,above] {B2} (-1,4);
    \draw[name path=C] (-0.5,1) to[out=90,in=135] node[pos=0,below] {C1} node[pos=1,below right] {C2} (2.5,2);

    \path[name intersections={of=A and B,by=AB}];
    \draw (AB) node[below=1em] {AB} circle[radius=2pt];

    \path[name intersections={of=A and C,by=AC}];
    \draw (AC) node[above=1em] {AC} circle[radius=2pt];

    \path[name intersections={of=B and C,by=BC}];
    \draw (BC) node[above=1em] {BC} circle[radius=2pt];

    \begin{pgfonlayer}{bg}
    \path[%draw=magenta,thick,->,
        intersection segments={
                of=A and B,
                sequence={L2[reverse]--R2}
              },name path=AB];
    \path[%draw=blue,thick,->,
        fill=orange,
        intersection segments={
                of=AB and C,
                sequence={L2--R2}
              }];             
    \end{pgfonlayer}
\end{tikzpicture}
\end{document}

enter image description here

In order to debug that, it is usually advantageous to add arrow heads to see the orientation. Of course, I commented them out in the final version.

In this example you do not need any libraries, nor pgfplots.

\documentclass{standalone}
\usepackage{tikz}
\pgfdeclarelayer{bg}
\pgfsetlayers{bg,main}

\begin{document}
\begin{tikzpicture}[scale=3,
  path A/.style 2 args={insert path={(0,0) to[out=5, in = 185] #1 (3,3) #2}},
  path B/.style 2 args={insert path={(2,0.5) to[out=135,in=-90] #1 (-1,4) #2}},
  path C/.style 2 args={insert path={(-0.5,1) to[out=90,in=135] #1 (2.5,2) #2}}]
    \draw[path A={coordinate[pos=0,label=left:A1] (A1)
     coordinate[pos=1,label=right:A2] (A2)}{}];
    \draw[path B={coordinate[pos=0,label=below right:B1] (B1) 
        coordinate[pos=1,label=above:B2] (B2)}{}];
    \draw[path C={coordinate[pos=0,label=below:C1] (C1)
    coordinate[pos=1,label=below right:C2] (C2)}{}];
    \begin{pgfonlayer}{bg}
     \clip[path A={}{-- (B2) -- cycle}];
     \clip[path B={}{-- (A2) -- cycle}];
     \clip[path C={}{-- (B1) -- cycle}];
     \fill[orange] (A2) -- (B2) -- (A1) -- (B1) -- cycle;
    \end{pgfonlayer}
\end{tikzpicture}
\end{document}

enter image description here

BTW, use path is now an official style that ships with TikZ. There are several different versions around on this site (and the version that ships with TikZ is IMHO not the most useful one).

2
  • Thanks! I'm not familiar with this paradigm of defining the paths as styles - is there a place I can read more about it or a keyword I can search by? I see that it saved you the trouble of naming and referencing the paths, but it looks like your choice of how many style arguments to use and where to place them was tailored to this case - would be handy to see examples of other ways people have used the technique. Nov 17 '19 at 0:15
  • @user1476176 The insert path key is described on p. 151 of pgf manual v3.1.4. Yes, in this case I did just quickly define something that does what I want without the need to repeat things. If we are very lucky, some competent users decide to work on this issue, so that path recycling becomes more convenient and structured.
    – user194703
    Nov 17 '19 at 0:19
3

Here's a solution using version 2.0 of the spath3 library that introduces some methods for manipulating TikZ/PGF paths. In particular, it can split a path at intersection points and then recombine them to define the region you want to fill (including reversing them if needed).

\documentclass{article}
%\url{https://tex.stackexchange.com/q/516723/86}

\usepackage{tikz}
\usetikzlibrary{intersections, spath3}

\ExplSyntaxOn

\cs_set_eq:NN \getComponentOf \clist_item:Nn

\ExplSyntaxOff

\begin{document}
\begin{tikzpicture}%[scale=3]
\draw[spath/save=A] (0,0) to[out=5, in = 185] node[pos=0,left] {A1} node[pos=1,right] {A2} (3,3);
\draw[spath/save=B] (2,0.5) to[out=135,in=-90] node[pos=0,below right] {B1} node[pos=1,above] {B2} (-1,4);
\draw[spath/save=C] (-0.5,1) to[out=90,in=135] node[pos=0,below] {C1} node[pos=1,below right] {C2} (2.5,2);

\tikzset{
  spath/split at intersections={A}{B},
  spath/split at intersections={B}{C},
  spath/split at intersections={C}{A},
  spath/get components of={A}\Acpts,
  spath/get components of={B}\Bcpts,
  spath/get components of={C}\Ccpts,
}

\fill[
  orange,
  spath/restore=\getComponentOf\Ccpts{2}
]
[
  spath/append reverse=\getComponentOf\Acpts{2},
  spath/append=\getComponentOf\Bcpts{2},
]
;

%% Useful for figuring out which components to use and what direction they go in
\foreach \pth/\cpts in {A/\Acpts, B/\Bcpts, C/\Ccpts}
{
  \expandafter\let\expandafter\cpts\cpts
  \foreach[count=\k] \cpt in \cpts
  {
    \node[font=\tiny] at (spath cs:{\cpt} .5) {\pth-\k};
    \node[transform shape, spath/transform to=\cpt{.8}, font=\tiny] {>};
  }
}

\end{tikzpicture}
\end{document}

Picture with the region between three curves filled in.

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.