6

I would like to draw a knot with self intersections. This can be done with the TikZ library knots. However, in my situation I have a grid in the background which should not be overdrawn from the white tube at the intersection. Is it possible to prevent this automatically?

Here is a minimal example. On the left, you see how the grid is overdrawn, and on the right you see how it should look like; but this workaround is not suitable for the knots I am actually working with. To be clear: Of course, the actual knot has to overdraw the grid.

enter image description here

 \documentclass{article}
 \usepackage{tikz}
 \usetikzlibrary{knots}
 \begin{document}

 \begin{tikzpicture}[baseline]
 \draw [lightgray] (0,0) grid (2,2);
 \begin{knot}[consider self intersections, clip width=10]
 \strand [thick] (0,0) -- (2,2) -- (2,0) -- (0,2);
 \flipcrossings{2}
 \end{knot}
 \end{tikzpicture}
 \quad
 \begin{tikzpicture}[baseline]
 \draw [thick] (2,0) -- (0,2);
 \draw [white, line width=2ex] (0,0) -- (2,2);
 \draw [lightgray] (0,0) grid (2,2);
 \draw [thick] (0,0) -- (2,2) -- (2,0);
 \end{tikzpicture}

 \end{document}
  • Both figures look to be identical. Where is the defference? – pzorba75 Aug 28 '17 at 11:47
  • @pzorba75 The grid at (2,2) should not be overdrawn with white. Instead it should be transparent, witch is not provided by the knots package I think. – dexteritas Aug 28 '17 at 11:50
  • I have changed the minimal example so that it becomes more clear. – HeinrichD Aug 28 '17 at 13:53
  • It may be possible, though, with some inverse clipping : by precomputing the self-intersections of the path, and then drawing this path, anti-clipping it against disks around those intersections. Then just draw the correct node resolution on top of this disk... (ask Loopspace about it, he's a clever fellow !) – marsupilam Aug 28 '17 at 20:33
7

Since the grid is light gray and the knot lines are black, there is a trick with transparency. The grid is drawn on top of the knots, but with opacity=.25. Then, the black lines remain black and the grid lines on top of the white background become light gray:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{knots}
\begin{document}
\begin{tikzpicture}[baseline]
 \begin{knot}[consider self intersections, clip width=10]
   \strand [thick, line join=round]
     (0,0) -- (2,2) -- (2,0) -- (0,2);
   \flipcrossings{2}
  \end{knot}
  \draw [opacity=.25, line cap=rect] (0, 0) grid (2,2);
\end{tikzpicture}
\end{document}

Result

With white background:

Result

| improve this answer | |
  • The problem is that my knots are colored dark blue. Here one sees the grid on it. Also, the intersections of the grid with itsself are darker. – HeinrichD Aug 31 '17 at 14:07
2

Here's a solution using transparency groups. It's not the most elegant solution as it requires drawing the knot path twice, but it works with the blue colour as you comment on Heiko's answer.

It works as following:

  1. Draw the grid.
  2. Draw the full knot path over the grid in white to erase the parts of the grid that the knot will go over.
  3. Draw the knot but multiply the colours when rendering onto the grid.

Here's the code:

\documentclass{article}
%\url{https://tex.stackexchange.com/q/388611/86}
 \usepackage{tikz}
\usetikzlibrary{knots}

 \begin{document}

\begin{tikzpicture}[baseline]
\begin{scope}[blend group=multiply]
\begin{scope}[blend group=normal]
 \draw [lightgray,ultra thin] (0,0) grid (2,2);
 \draw [thick,white] (0,0) -- (2,2) -- (2,0) -- (0,2);
\end{scope}
\begin{scope}[blend group=normal]
\begin{knot}[consider self intersections, clip width=10]
 \strand[thick,blue] (0,0) -- (2,2) -- (2,0) -- (0,2);
 \flipcrossings{2}
 \end{knot}
\end{scope}
\end{scope}

\end{tikzpicture}
 \hspace{1cm} 
 \begin{tikzpicture}[baseline]
 \draw [thick] (2,0) -- (0,2);
 \draw [white, line width=2ex] (0,0) -- (2,2);
 \draw [lightgray] (0,0) grid (2,2);
 \draw [thick] (0,0) -- (2,2) -- (2,0);
 \end{tikzpicture}

 \end{document}

Result (zooming in on the crossing):

Crossing on grid

It's not completely perfect as this screenshot shows - drawing the strand in white potentially erases a bit more than strictly necessary.

Crossing just off grid

| improve this answer | |
-3

I guess in your case the most simple way is to move the grid statement after drawing the knots.

| improve this answer | |
  • 1
    AFAIK, the OP does not want that the grid overdraws the knot lines. – Heiko Oberdiek Aug 29 '17 at 17:07
  • Well, in this case a real example would help to see the problem. With white background and black lines and grey grid it's not obvious. – Harald Lichtenstein Aug 30 '17 at 20:29
  • 2
    "To be clear: Of course, the actual knot has to overdraw the grid." – HeinrichD Aug 31 '17 at 13:42

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.