I'm drawing a knot diagram and I use the \clip command to help shading the regions. However, it seems that the \strand command of the knot environment have some strange effect to \clip.
For instance, the correct result should be (let's call it Diagram 1):
which is generated by
% Diagram 1
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{knots}
\usetikzlibrary{hobby}
\begin{document}
\definecolor{skyblue}{RGB}{60,120,234}
\scalebox{0.3}{\begin{tikzpicture}[use Hobby shortcut]
\pgfdeclarelayer{foreground}
\pgfsetlayers{main,foreground}
\begin{pgfonlayer}{foreground}
\begin{scope}
\clip (-2,3) .. (0,2) .. (0.4,1)
.. (0,0) .. (-0.4,-1)
.. (0,-2) .. (2,-3)
.. (4.8,0) .. (2,3)
.. (0,2) .. (-0.4,1) -- (-2,3);
\clip (-2,-3) .. (0,-2) .. (0.4,-1)
.. (0,0) .. (-0.4,1)
.. (0,2) .. (2,3)
.. (4.8,0) .. (2,-3)
.. (0,-2) .. (-0.4,-1) -- (-2,-3);
\fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
\end{scope}
\begin{scope}
\clip (2,3) .. (0,2) .. (-0.4,1)
.. (0,0) .. (0.4,-1)
.. (0,-2) .. (-2,-3)
.. (-4.8,0) .. (-2,3)
.. (0,2) .. (0.4,1) -- (2,3);
\clip (2,-3) .. (0,-2) .. (-0.4,-1)
.. (0,0) .. (0.4,1)
.. (0,2) .. (-2,3)
.. (-4.8,0) .. (-2,-3)
.. (0,-2) .. (0.4,-1) -- (2,-3);
\fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
\end{scope}
\draw (-2.2,0) node[scale=3] {$+$};
\draw (2.2,0) node[scale=3] {$-$};
\draw (5,2.5) node[scale=3] {$M$};
\end{pgfonlayer}
\begin{knot}[
consider self intersections,
clip width=10,
clip radius=0.5cm,
ignore endpoint intersections=false,
flip crossing/.list={6,14}
]
\strand[very thick,black,closed]
(0.4,1) .. (0,2) .. (-2,3)
.. (-4.8,0) .. (-2,-3)
.. (0,-2) .. (0.4,-1)
.. (0,0) .. (-0.4,1)
.. (0,2) .. (2,3)
.. (4.8,0) .. (2,-3)
.. (0,-2) .. (-0.4,-1) .. (0,0);
\end{knot}
\end{tikzpicture}}
\end{document}
Notice that \clip is before \strand.
However, if I switch the order of the code fragment containing \clip and \strand:
% Diagram 2
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{knots}
\usetikzlibrary{hobby}
\begin{document}
\definecolor{skyblue}{RGB}{60,120,234}
\scalebox{0.3}{\begin{tikzpicture}[use Hobby shortcut]
\pgfdeclarelayer{foreground}
\pgfsetlayers{main,foreground}
\begin{knot}[
consider self intersections,
clip width=10,
clip radius=0.5cm,
ignore endpoint intersections=false,
flip crossing/.list={6,14}
]
\strand[very thick,black,closed]
(0.4,1) .. (0,2) .. (-2,3)
.. (-4.8,0) .. (-2,-3)
.. (0,-2) .. (0.4,-1)
.. (0,0) .. (-0.4,1)
.. (0,2) .. (2,3)
.. (4.8,0) .. (2,-3)
.. (0,-2) .. (-0.4,-1) .. (0,0);
\end{knot}
\begin{pgfonlayer}{foreground}
\begin{scope}
\clip (-2,3) .. (0,2) .. (0.4,1)
.. (0,0) .. (-0.4,-1)
.. (0,-2) .. (2,-3)
.. (4.8,0) .. (2,3)
.. (0,2) .. (-0.4,1) -- (-2,3);
\clip (-2,-3) .. (0,-2) .. (0.4,-1)
.. (0,0) .. (-0.4,1)
.. (0,2) .. (2,3)
.. (4.8,0) .. (2,-3)
.. (0,-2) .. (-0.4,-1) -- (-2,-3);
\fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
\end{scope}
\begin{scope}
\clip (2,3) .. (0,2) .. (-0.4,1)
.. (0,0) .. (0.4,-1)
.. (0,-2) .. (-2,-3)
.. (-4.8,0) .. (-2,3)
.. (0,2) .. (0.4,1) -- (2,3);
\clip (2,-3) .. (0,-2) .. (-0.4,-1)
.. (0,0) .. (0.4,1)
.. (0,2) .. (-2,3)
.. (-4.8,0) .. (-2,-3)
.. (0,-2) .. (0.4,-1) -- (2,-3);
\fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
\end{scope}
\draw (-2.2,0) node[scale=3] {$+$};
\draw (2.2,0) node[scale=3] {$-$};
\draw (5,2.5) node[scale=3] {$M$};
\end{pgfonlayer}
\end{tikzpicture}}
\end{document}
The result becomes (let's call it Diagram 2):
As you can see, the shading doesn't fit into the curve.
Even if I use the code for generating Diagram 1 (where \clip is before \strand), the error would still appear when there are other \strand commands before the \clip code. One can see this by repeating the code for Diagram 1 twice:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{knots}
\usetikzlibrary{hobby}
\begin{document}
\definecolor{skyblue}{RGB}{60,120,234}
\scalebox{0.3}{\begin{tikzpicture}[use Hobby shortcut]
\pgfdeclarelayer{foreground}
\pgfsetlayers{main,foreground}
\begin{pgfonlayer}{foreground}
\begin{scope}
\clip (-2,3) .. (0,2) .. (0.4,1)
.. (0,0) .. (-0.4,-1)
.. (0,-2) .. (2,-3)
.. (4.8,0) .. (2,3)
.. (0,2) .. (-0.4,1) -- (-2,3);
\clip (-2,-3) .. (0,-2) .. (0.4,-1)
.. (0,0) .. (-0.4,1)
.. (0,2) .. (2,3)
.. (4.8,0) .. (2,-3)
.. (0,-2) .. (-0.4,-1) -- (-2,-3);
\fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
\end{scope}
\begin{scope}
\clip (2,3) .. (0,2) .. (-0.4,1)
.. (0,0) .. (0.4,-1)
.. (0,-2) .. (-2,-3)
.. (-4.8,0) .. (-2,3)
.. (0,2) .. (0.4,1) -- (2,3);
\clip (2,-3) .. (0,-2) .. (-0.4,-1)
.. (0,0) .. (0.4,1)
.. (0,2) .. (-2,3)
.. (-4.8,0) .. (-2,-3)
.. (0,-2) .. (0.4,-1) -- (2,-3);
\fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
\end{scope}
\draw (-2.2,0) node[scale=3] {$+$};
\draw (2.2,0) node[scale=3] {$-$};
\draw (5,2.5) node[scale=3] {$M$};
\end{pgfonlayer}
\begin{knot}[
consider self intersections,
clip width=10,
clip radius=0.5cm,
ignore endpoint intersections=false,
flip crossing/.list={6,14}
]
\strand[very thick,black,closed]
(0.4,1) .. (0,2) .. (-2,3)
.. (-4.8,0) .. (-2,-3)
.. (0,-2) .. (0.4,-1)
.. (0,0) .. (-0.4,1)
.. (0,2) .. (2,3)
.. (4.8,0) .. (2,-3)
.. (0,-2) .. (-0.4,-1) .. (0,0);
\end{knot}
\end{tikzpicture}}
\scalebox{0.3}{\begin{tikzpicture}[use Hobby shortcut]
\pgfdeclarelayer{foreground}
\pgfsetlayers{main,foreground}
\begin{pgfonlayer}{foreground}
\begin{scope}
\clip (-2,3) .. (0,2) .. (0.4,1)
.. (0,0) .. (-0.4,-1)
.. (0,-2) .. (2,-3)
.. (4.8,0) .. (2,3)
.. (0,2) .. (-0.4,1) -- (-2,3);
\clip (-2,-3) .. (0,-2) .. (0.4,-1)
.. (0,0) .. (-0.4,1)
.. (0,2) .. (2,3)
.. (4.8,0) .. (2,-3)
.. (0,-2) .. (-0.4,-1) -- (-2,-3);
\fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
\end{scope}
\begin{scope}
\clip (2,3) .. (0,2) .. (-0.4,1)
.. (0,0) .. (0.4,-1)
.. (0,-2) .. (-2,-3)
.. (-4.8,0) .. (-2,3)
.. (0,2) .. (0.4,1) -- (2,3);
\clip (2,-3) .. (0,-2) .. (-0.4,-1)
.. (0,0) .. (0.4,1)
.. (0,2) .. (-2,3)
.. (-4.8,0) .. (-2,-3)
.. (0,-2) .. (0.4,-1) -- (2,-3);
\fill[skyblue,opacity=0.2] (-4.8,-3) rectangle (4.8,3);
\end{scope}
\draw (-2.2,0) node[scale=3] {$+$};
\draw (2.2,0) node[scale=3] {$-$};
\draw (5,2.5) node[scale=3] {$M$};
\end{pgfonlayer}
\begin{knot}[
consider self intersections,
clip width=10,
clip radius=0.5cm,
ignore endpoint intersections=false,
flip crossing/.list={6,14}
]
\strand[very thick,black,closed]
(0.4,1) .. (0,2) .. (-2,3)
.. (-4.8,0) .. (-2,-3)
.. (0,-2) .. (0.4,-1)
.. (0,0) .. (-0.4,1)
.. (0,2) .. (2,3)
.. (4.8,0) .. (2,-3)
.. (0,-2) .. (-0.4,-1) .. (0,0);
\end{knot}
\end{tikzpicture}}
\end{document}
You can evidently see that even though they are drawn by the same TikZ codes, the results are not the same.
Could someone explain what's going on and how to fix this? Thanks in advance.

\stranduses\clipinternally to correctly draw the crossing-over and crossing-under parts. – Henri Menke Aug 21 '19 at 10:33