5

I have been struggling to shade in a specific region in an image I am trying to make. Here is a MWE

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{figure}[!h]
\centering
\begin{tikzpicture}

\tikzset{
    partial ellipse/.style args={#1:#2:#3}{
        insert path={+ (#1:#3) arc (#1:#2:#3)}
    }
}

\draw (-5, 0) ellipse (.5 and 3);
\draw (5,0) [partial ellipse=-90:90:.5cm and 3cm];

\draw[dashed] (0,0) [partial ellipse=-90:90:.25cm and 1.5cm];
\draw[blue] (2,0) [partial ellipse=-90:90:.25cm and 1.70cm];
\draw[blue] (-2,0) [partial ellipse=-90:90:.25cm and 1.70cm];


\draw (-5,3) .. controls (-2.5,1) and  (2.5,1) .. (5,3);
\draw (-5,-3) .. controls (-2.5, -1) and (2.5,-1) .. (5,-3);



\end{tikzpicture}

\end{figure}
\end{document}

This produces the figure

enter image description here

I would like to be able to shade the region bounded between the two blue curves on the left and right, and the black curves on top. I tried using the \clip feature, but admittedly I don't quite understand it. The best I was able to get was to shade the region with the right blue curve as a boundary, but the rest was a rectangular region.

I was wondering if there was a way to perhaps name each curve, and then specify a region by giving the "corners" and specifying to shade following along a curve, if that makes sense. Really, I am just looking for any way to shade this region.

5

Yes, it is possible to do that precisely as you suggest: name the paths, build two "corners", and then fill the region between.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections} %<-added
\usepackage{pgfplots} %<-added
\usepgfplotslibrary{fillbetween} %<-added
\begin{document}
\begin{figure}[!h]
\centering
\begin{tikzpicture}

\tikzset{
    partial ellipse/.style args={#1:#2:#3}{
        insert path={+ (#1:#3) arc (#1:#2:#3)}
    }
}

\draw (-5, 0) ellipse (.5 and 3);
\draw (5,0) [partial ellipse=-90:90:.5cm and 3cm];

\draw[blue,name path=right] (2,0) [partial ellipse=-90:90:.25cm and 1.70cm];
\draw[blue,name path=left] (-2,0) [partial ellipse=-90:90:.25cm and 1.70cm];


\draw[name path=top] (-5,3) .. controls (-2.5,1) and  (2.5,1) .. (5,3);
\draw[name path=bottom] (-5,-3) .. controls (-2.5, -1) and (2.5,-1) .. (5,-3);

  \path [%draw,line width=3,blue,
    name path=topleft,
    intersection segments={
        of=top and left,
        sequence={A1[reverse] -- B0[reverse]}
    }];
  \path [%draw,line width=3,purple,
    name path=bottomright,
    intersection segments={
        of=bottom and right,
        sequence={A0 -- B1}
   }];

  \fill [
    blue!40!white,
    intersection segments={
        of=topleft and bottomright,
        sequence={A1 -- B1},
    }
  ];

% moved this down  
\draw[dashed] (0,0) [partial ellipse=-90:90:.25cm and 1.5cm];

\end{tikzpicture}

\end{figure}
\end{document}

enter image description here

EDIT: Just for fun and completeness: it might be worthwhile to do the fills on the background layer, which requires the corresponding library. Another (though minor) advantage of this method as opposed of using clips is that shading becomes more intuitive. That is, if you shade with a gradient, but clip a big junk of the shaded area away, the colors at the boundaries do not match the inputs. Of course, the disadvantage of this method advertized here is that the compilation time is somewhat longer.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections,backgrounds} %<-added
\usepackage{pgfplots} %<-added
\usepgfplotslibrary{fillbetween} %<-added
\begin{document}
\begin{figure}[!h]
\centering
\begin{tikzpicture}

\tikzset{
    partial ellipse/.style args={#1:#2:#3}{
        insert path={+ (#1:#3) arc (#1:#2:#3)}
    }
}

\draw (-5, 0) ellipse (.5 and 3);
\draw (5,0) [partial ellipse=-90:90:.5cm and 3cm];

\draw[blue,name path=right] (2,0) [partial ellipse=-90:90:.25cm and 1.70cm];
\draw[blue,name path=left] (-2,0) [partial ellipse=-90:90:.25cm and 1.70cm];


\draw[name path=top] (-5,3) .. controls (-2.5,1) and  (2.5,1) .. (5,3);
\draw[name path=bottom] (-5,-3) .. controls (-2.5, -1) and (2.5,-1) .. (5,-3);

% moved this down  
\draw[dashed] (0,0) [partial ellipse=-90:90:.25cm and 1.5cm];

\begin{scope}[on background layer]
  \path [%draw,line width=3,blue,
    name path=topleft,
    intersection segments={
        of=top and left,
        sequence={A1[reverse] -- B0[reverse]}
    }];
  \path [%draw,line width=3,purple,
    name path=bottomright,
    intersection segments={
        of=bottom and right,
        sequence={A0 -- B1}
   }];

  \shade [top color=blue!20,bottom color=blue,
    intersection segments={
        of=topleft and bottomright,
        sequence={A1 -- B1},
    }
  ];
\end{scope}

\end{tikzpicture}

\end{figure}
\end{document}

enter image description here

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.