# Filling between two boundary lines

in the following example I plot two demonstration trajectories:

Instead of the arrow that I drew, I would like to highlight the surface between the two trajectories with the mechanism from \fillbetween or \fill but I'm not sure how to do this for this given example. I have done it before in the axis environment but how can I specifically highlight the surface denoted by delta z in the image? My MWE is:

\documentclass[tikz]{standalone}
\usetikzlibrary{spy}
\usepackage{standard}

\begin{document}
\begin{tikzpicture}[%
spy using outlines={circle, magnification=2, connect spies} ,scale = 5]
\coordinate (A1) at (-0.2,0.5);
\coordinate (B1) at (0.5, 1.8);
\coordinate (C1) at (0.4, 1);
\coordinate (D1) at (0.9,-0.3);

\coordinate (C2) at (0.37, 0.8);

\draw [blue, thick] plot [smooth cycle, tension = 0.6] coordinates {(A1) (B1) (C1) (D1)};
\draw [red, dashed] plot [smooth cycle, tension = 0.7] coordinates {(A1) (B1) (C2) (D1)};

\draw [->, thin] (-0.5,0) -- (1.5,0) node[anchor=north east, scale = 1.5] {$z_1$};
\draw [->, thin] (0,-0.5) -- (0,2) node[anchor=north west, scale = 1.5] {$z_2$};
\draw[blue] (0.8,1.6) -- (1, 1.6);
\node at (1.3, 1.7) [] {$\vect{z}_s(\omega_0t + \vartheta(t))$};
\draw[red, dashed] (0.8,1.7) -- (1, 1.7);
\node at (1.2, 1.6) [] {$\vect{z}_s(\omega_0t)$};

\draw[<->, >=latex] (0.47, 0.53) -- (0.56, 0.57) node [midway, below, pos = 0.75] {$\Delta \vect{z}$};

\coordinate (spyCoordinates) at (0.55, 0.5);
\spy[dashed, size = 2cm] on (spyCoordinates) in node[scale = 2.5] at (1.3,1);

\end{tikzpicture}
\end{document}


A nice addition would be striped highlighting instead of a constant color

• wow nice -- may i suggest removing the delta z in the bigger image since it is there as an explanation in the magnification--will give a cleaner look – js bibra May 16 '20 at 1:20
• Of course it is possible. But I already commented about putting it on top of the shading and that's why I'd like to keep it, just in case someone faces the same problem in the future – Bakr May 16 '20 at 20:48

This fills the region with a pattern. Note that by design patterns do not get transformed. The methods used here borrow from other posts which I linked (and possibly more). One can highlight the difference between the two curves, i.e. everything that is enclosed by one curve but outside of the other curve. So with

 \tikzset{protect=\pathB}


you protect the inside of the curve the path of which has been stored in \pathB, meaning it won't be touched. Then with

 \path[reuse path=\pathA,pattern={Lines[angle=45,distance={4.5pt},
line width=1pt]},pattern color=magenta];


you fill what is inside the path stored in \pathA. If swap the roles of the paths, you fill the other region. (I added this as it was asked in the comments.)

\documentclass[tikz]{standalone}
\usepackage{contour}
\contourlength{1pt}
\usetikzlibrary{patterns.meta,spy}
% based on
% https://tex.stackexchange.com/a/38995/121799
% https://tex.stackexchange.com/a/76216
% https://tex.stackexchange.com/a/59168/194703
% https://tex.stackexchange.com/q/448920/194703
\makeatletter
\tikzset{
reuse path/.code={\pgfsyssoftpath@setcurrentpath{#1}}
}
\tikzset{even odd clip/.code={\pgfseteorule},
protect/.code={
\clip[overlay,even odd clip,reuse path=#1]
(current bounding box.south west) rectangle (current bounding box.north east);
}}
\makeatother
\begin{document}
\begin{tikzpicture}[%
spy using outlines={circle, magnification=2, connect spies} ,scale = 5]
\coordinate (A1) at (-0.2,0.5);
\coordinate (B1) at (0.5, 1.8);
\coordinate (C1) at (0.4, 1);
\coordinate (D1) at (0.9,-0.3);

\coordinate (C2) at (0.37, 0.8);

\draw [blue, thick,save path=\pathA] plot [smooth cycle, tension = 0.6] coordinates {(A1) (B1) (C1) (D1)};
\draw [red, dashed,save path=\pathB] plot [smooth cycle, tension = 0.7] coordinates {(A1) (B1) (C2) (D1)};
\begin{scope}
\tikzset{protect=\pathB}
\path[reuse path=\pathA,pattern={Lines[angle=45,distance={4.5pt},
line width=1pt]},pattern color=magenta];
\end{scope}
\begin{scope}
\tikzset{protect=\pathA}
\path[reuse path=\pathB,pattern={Lines[angle=90,distance={2.5pt},
line width=1pt]},pattern color=cyan];
\end{scope}

\draw [->, thin] (-0.5,0) -- (1.5,0) node[anchor=north east, scale = 1.5] {$z_1$};
\draw [->, thin] (0,-0.5) -- (0,2) node[anchor=north west, scale = 1.5] {$z_2$};
\draw[blue] (0.8,1.6) -- (1, 1.6);
\node at (1.3, 1.7) [] {$\vec{z}_s(\omega_0t + \vartheta(t))$};
\draw[red, dashed] (0.8,1.7) -- (1, 1.7);
\node at (1.2, 1.6) [] {$\vec{z}_s(\omega_0t)$};

\draw[<->, >=latex] (0.47, 0.53) -- (0.56, 0.57) node [midway, below, pos = 0.75]
{\contour{white}{$\Delta \vec{z}$}};

\coordinate (spyCoordinates) at (0.55, 0.5);
\spy[dashed, size = 2cm] on (spyCoordinates) in node[scale = 2.5] at (1.3,1);

\end{tikzpicture}
\end{document}


• thank you. I am sorry i forgot to remove that package but this works perfectly fine. How can i put the text above the shading? – Bakr May 15 '20 at 19:15
• @Bakr The text is above the shading. If you want it to stick out, you can use the contour package. – user194703 May 15 '20 at 19:21
• @ Schrödinger's cat is it possible to highlight the small surfaces on the other side as well or are they too small ? Of course I mean the surfaces that represent the difference between the two trajectories – Bakr May 19 '20 at 17:10
• @Bakr It is possible. The above highlights the difference between the blue and the red curve, i.e. everything that is enclosed by the blue curve but outside of the red dashed one. So you only have to swap the roles of the paths: \begin{scope} \tikzset{protect=\pathA} \path[reuse path=\pathB,pattern={Lines[angle=90,distance={2.5pt}, line width=1pt]},pattern color=cyan]; \end{scope}. – user194703 May 19 '20 at 17:15
• Thank you so much. This was very helpful – Bakr May 19 '20 at 20:47