TransWikia.com

Drawing knots without overdrawing a grid

TeX - LaTeX Asked by HeinrichD on February 14, 2021

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}

4 Answers

The latest (on CTAN and TL as of Feb 2021) version of the spath3 library includes new routines to work with paths, including splitting them at intersections and inserting gaps. This makes it possible to draw knot diagrams by a different means to the knots tikzlibrary and is particularly well suited to this situation where there the background is not uniform.

Here's the code:

documentclass{article}
%url{https://tex.stackexchange.com/q/388611/86}
 usepackage{tikz}
 usetikzlibrary{spath3,intersections}
 begin{document}

 begin{tikzpicture}[baseline]
 draw [lightgray] (0,0) grid (2,2);
 path[spath/save=strand] (0,0) -- (2,2) -- (2,0) -- (0,2);
tikzset{
  every strand component/.style={draw,thick},
  spath/knot={strand}{8pt}{2}
}
 end{tikzpicture}

 end{document}

And here's the result:

Knot diagram showing a grid through the crossing

Answered by Andrew Stacey on February 14, 2021

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

Answered by Andrew Stacey on February 14, 2021

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

Answered by Heiko Oberdiek on February 14, 2021

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

Answered by Harald Lichtenstein on February 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP