3

I would like to color a part of curve, as in the picture. I used the following code

\documentclass{article}
\usepackage{tikz,amsmath, amssymb,bm,color}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\usetikzlibrary{shapes,arrows,arrows.meta,angles,quotes,patterns,patterns.meta}
\begin{document}
    \begin{tikzpicture}[>=stealth']
        %\tikzstyle{every node}=[font=\footnotesize]
    \draw[->](-0.5,1.5)--(9.5,1.5) node[right]{$u$};
    \draw[->](0,-2)--(0,4.75) node[left]{$v$};

    \draw [thick] plot [domain=0:2](0.5,2.5) 
    .. controls ++(60:2.5) and ++(120:1.5) .. (3.5,3.5)
    .. controls ++(-60:1) and ++(-120:1) .. (5,3.5)
    .. controls ++(60:1.5) and ++(120:1.5) .. (7,2.5)
    .. controls ++(-60:1) and ++(-160:1) .. (9,2);  
    \begin{scope}[yscale=-1,shift={(0,-3)}]
        \draw [thick] (0.5,2.5) 
        .. controls ++(60:2.5) and ++(120:1.5) .. (3.5,3.5)
        .. controls ++(-60:1) and ++(-120:1) .. (5,3.5)
        .. controls ++(60:1.5) and ++(120:1.5) .. (7,2.5)
        .. controls ++(-60:1) and ++(-160:1) .. (9,2);
    \end{scope}
\end{tikzpicture}

\end{document}

enter image description here

1 Answers1

4

One option is to draw another curve on top (Updated based on @ hpekristiansen comment plus some cleanup):

\documentclass{article}
\usepackage{tikz,amsmath, amssymb,bm,color}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\usetikzlibrary{shapes,arrows,arrows.meta,angles,quotes,patterns,patterns.meta}

\begin{document} \begin{tikzpicture}[>=stealth', remember picture, overlay] \tikzstyle{reverseclip}=[ insert path={ (current page.north east) -- (current page.south east) -- (current page.south west) -- (current page.north west) -- (current page.north east) } ];

    \tikzset{
        myCurve/.pic={
            .code={
                \draw [thick] plot [domain=0:2] (0.5,2.5) 
                .. controls ++(60:2.5) and ++(120:1.5) .. (3.5,3.5)
                .. controls ++(-60:1) and ++(-120:1) .. (5,3.5)
                .. controls ++(60:1.5) and ++(120:1.5) .. (7,2.5)
                .. controls ++(-60:1) and ++(-160:1) .. (9,2);  
            }
        }
    }
    \draw[->](-0.5,1.5)--(9.5,1.5) node[right]{$u$};
    \draw[->](0,-2)--(0,4.75) node[left]{$v$};

    \coordinate (B) at (4,5);
    \coordinate (E) at (7,-5);

    \begin{scope}
        \clip[reverseclip] (B) rectangle (E);
        \path (0, 0) pic {myCurve};
    \end{scope}        

    \begin{scope}
        \clip (B) rectangle (E);
        \path[red] (0, 0) pic {myCurve};
    \end{scope}

    \begin{scope}[shift={(0,3)}]
        \path (0, 0) pic[yscale=-1] {myCurve};
    \end{scope}

\end{tikzpicture}

\end{document}

enter image description here

jak123
  • 4,252
  • 5
  • 26
  • 49
  • 1
    +1 good methode. Be aware that this can create viewer artefacts(bleed trough of color) - to be safe, one could draw the original path with an inverse clip. – hpekristiansen Jan 15 '24 at 16:16
  • Good point @hpekristiansen! Is there an easy way to do an inverse clip? – jak123 Jan 15 '24 at 16:59
  • Some times is is easiest to just put the other areas in a clip: \clip (x11,y11) rectangle (x12,y12) (x21,y21) rectangle (x22,y22) .... Other times you can define a style like here: https://tex.stackexchange.com/a/12033/8650 (note that \tikzstyle is deprecated use reverseclip/.style .... – hpekristiansen Jan 15 '24 at 17:14
  • reverseclip/.style={[insert path={(current page.north east) -- (current page.south east) -- (current page.south west) -- (current page.north west) -- (current page.north east)} ]} – hpekristiansen Jan 15 '24 at 17:23
  • 1
    \clip[reverseclip] (x1,y1) rectangle (x2,y2) – hpekristiansen Jan 15 '24 at 17:24
  • @hpekristiansen: I made the update based on your comments. For some reason, the new style syntax didn't work for me on overleaf. Thanks! – jak123 Jan 16 '24 at 13:40