# How to make a portion of curve red using TikZ?

I want to make the the closed loop part of the figure by "ultra red" color only and the other part by blue. (from (2,0) .. (0,0.93) .. (-0.93,0) .. (0,-0.93) .. (2,0))

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{hobby,decorations.markings}
\begin{document}
\begin{tikzpicture}[use Hobby shortcut]
% filled points
\fill (0,0) circle (1pt);
\fill (2,0) circle (1pt);
% middle crossing loop
\draw (3,-1) .. (2,0) .. (0,0.93) .. (-0.93,0) .. (0,-0.93) .. (2,0) .. (3,1);
\end{tikzpicture}
\end{document}


Please suggest what to do. Also suggest me if I include arrows into the curve, how to make changes of colors in to the arrows?

I think it's not possible to change drawing color in the middle of path:

But for this particular case, clipping could be a valid option. Even the black dot on (0,0) helps to hide a bad join between clipped parts.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{hobby,decorations.markings}
\begin{document}
\begin{tikzpicture}[use Hobby shortcut]
% middle crossing loop
% \draw (3,-1) .. (2,0) .. (0,0.93) .. (-0.93,0) .. (0,-0.93) .. (2,0) .. (3,1);
\begin{scope}
\clip (3,-1) rectangle (2,1);
\draw[thick, blue, fill=blue!30] (3,-1) .. (2,0) .. (0,0.93) .. (-0.93,0) .. (0,-0.93) .. (2,0) .. (3,1);
\end{scope}
\begin{scope}
\clip (2,1) rectangle (-1,-1);
\draw[thick, red, fill=red!30] (3,-1) .. (2,0) .. (0,0.93) .. (-0.93,0) .. (0,-0.93) .. (2,0) .. (3,1);
\end{scope}
% filled points
\fill (0,0) circle (1pt);
\fill (2,0) circle (1pt);
\end{tikzpicture}
\end{document}


If you want to draw arrows over any path can use decoration.markings library. Answers to TikZ: How to draw an arrow in the middle of the line? are good examples.

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{hobby,decorations.markings, arrows.meta,bending}
\begin{document}
\begin{tikzpicture}[use Hobby shortcut,
decoration={markings,
mark=between positions 0 and 1 step 2mm with
{\arrow{Stealth[length=1mm,flex]}},
}]
\begin{scope}
\clip (3,-1) rectangle (2,1);
\draw[thick, blue, fill=blue!30, postaction={decorate}] (3,-1) .. (2,0) .. (0,0.93) .. (-0.93,0) .. (0,-0.93) .. (2,0) .. (3,1);
\end{scope}
\begin{scope}
\clip (2,1) rectangle (-1,-1);
\draw[thick, red, fill=red!30, postaction={decorate}] (3,-1) .. (2,0) .. (0,0.93) .. (-0.93,0) .. (0,-0.93) .. (2,0) .. (3,1);
\end{scope}
% filled points
\fill (0,0) circle (1pt);
\fill (2,0) circle (1pt);
\end{tikzpicture}
\end{document}


Since the cross-over point is one of the nodes of the hobby path, you can use the soft blank feature of the hobby package. This allows you to specify some parts of the path as being blank. It also allows you to save and reuse the path and switch the behaviour of the "soft blanks".

\documentclass{article}
%\url{http://tex.stackexchange.com/q/162919/86}
\usepackage{tikz}
\usetikzlibrary{hobby,decorations.markings}
\begin{document}
\begin{tikzpicture}[use Hobby shortcut]
% filled points
\fill (0,0) circle (1pt);
\fill (2,0) circle (1pt);
% middle crossing loop
\draw[save Hobby path=fish,thick, red, fill=red!30] (3,-1) .. ([blank=soft]2,0) .. (0,0.93) .. (-0.93,0) .. (0,-0.93) .. (2,0) .. ([blank=soft]3,1);
\draw[restore and use Hobby path={fish}{invert soft blanks,disjoint=true},thick, blue];
\end{tikzpicture}
\end{document}


(NB I'm using the latest development version of hobby; if this doesn't work with the latest official release you'll need to download the latest from the TeX-SX Launchpad site.)

The save Hobby path=<name> saves the output of Hobby's algorithm but before it is fed back into PGF to construct a path. The restore and use Hobby path={<name>}{<options>} reloads a previously stored Hobby path and converts it to a PGF path. The <options> that can be used are those that pertain to this conversion (i.e. not any options that would affect how the algorithm itself worked). In this case, what is happening is that certain segments of the original path have been marked as soft blank. This means that they are not drawn, though are used in the computation of the path. When the path is restored, the key invert soft blanks swaps the soft blanks with the non-blanks, so the previously drawn parts are now removed and the previously hidden parts are drawn. The disjoint=true means that the converted path starts with an explicit move, this is needed because every path has to start with a move, and there is no path that the Hobby path is appended to.

(For more details, see the manual of the hobby package.)

• @Andrew_Stacey Your code is also very nice. Would you explain the line \draw[restore and use Hobby path={fish}{invert soft blanks,disjoint=true},thick, blue]; at your earliest. – user1942348 Mar 1 '14 at 13:47
• @user1942348 Done. – Loop Space Mar 1 '14 at 20:47