[Tex/LaTex] Knot diagrams in TikZ


This question led to a new package:

(Technically, this question led to a subpackage of the spath3 package; the spath3 package provides some foundations on which a TikZ library knots was built and the TikZ library is bundled with the spath3 library.)

See also the blog post: How can I draw a knot in TeX?

I need to use TikZ to draw some knot/braid diagrams. These will be built from families of curves, which appear to cross over each other in places. At each crossing, it needs to be obvious which line is going over which, and so I'm using the TikZ double functionality, to draw a fat white line underneath the thin black lines representing the curves. That way, if I draw 'on top' line segments after 'on the bottom' line segments, I'll get the correct effect.

But still, drawing these diagrams is a pain. In particular, I might have two continuous curves in one diagram, with the first curve going on top of the second in some places, and beneath it in others. So I can't simply draw the 'lower' curve first: I have to draw different segments of the curves individually.

For ease of programming, I'd really like to have all the code for each curve together. One way I could achieve this is if I could change the layer independently for each segment of a multi-point path command. The idea would be something like this:

\draw (1) to [layer=middlelayer] (2) to [layer=toplayer] (3) to [layer=bottomlayer] (4);

Is this possible? Or is there a better way to achieve the effect I want?

Unfortunately, my diagrams need to be much more freeform than those produced by Andrew Stacey's braid package. They're not of the form of generators of the braid group stacked neatly on top of one another. Here's an example of the sort of thing I need:

enter image description here

Best Answer

Update: This is now available on CTAN (see above). Bugfixes and improvements will generally be available on github before being uploaded to CTAN. Note that that repository is quite cluttered, the relevant .dtx file is called spath3.dtx. This generates all the necessary files.

Update: The code has undergone considerable revision following extensive discussion with Jamie in the chat room From Answers to Packages. The code can now be downloaded from the TeX-SX Launchpad Project. The necessary files are knots.dtx and spath.dtx. The file knots_test.tex contains some example code. To create the .sty files from the .dtx, run tex <file>.dtx. Alternatively, running latex (or pdflatex) on knots_test.tex with shell escapes enabled will automatically generate the .sty files.

Okay, here's my first attempt at implementing what we discussed in chat. It is certainly a bit rough around the edges, and I give absolutely no guarantees that it won't TeX your cat instead.



\tikzoption{save knot path}{\tikz@addmode{\pgfsyssoftpath@getcurrentpath\knot@tmppath\expandafter\global\expandafter\let#1=\knot@tmppath}}
\tikzoption{use knot path}{\tikz@addmode{\expandafter\pgfsyssoftpath@setcurrentpath#1}}
  knot/draft mode/.is if=knot@draftmode
  \foreach \knot@str in {1,...,\the\value{knot@strings}} {
    \expandafter\expandafter\expandafter\draw\expandafter\expandafter\expandafter[\csname knot@string@opts@\knot@str\endcsname,use knot path=\csname knot@string@\knot@str\endcsname] (0,0);
    \csname knot@string@\knot@str\endcsname
    \node[circle,fill=white,fill opacity=.5] at (\pgf@xa,\pgf@ya) {\knot@str};
  \foreach \knot@sta in {1,...,\knot@stam} {
    \pgfmathtruncatemacro{\knot@stap}{\knot@sta + 1}
    \foreach \knot@stb in {\knot@stap,...,\the\value{knot@strings}} {
      \pgfintersectionofpaths{\expandafter\pgfsetpath\csname knot@string@\knot@sta\endcsname}{\expandafter\pgfsetpath\csname knot@string@\knot@stb\endcsname}
    \foreach \intsect in {1,...,\pgfintersectionsolutions} {
        \node[circle,fill=white,fill opacity=.5] at (\intsectx,\intsecty)         {\knot@sta-\knot@stb-\intsect};
%\message{\knot@sta-\knot@stb-\intsect not defined}
\pgfmathtruncatemacro{\knot@under}{\csname knot@crossing@\knot@sta-\knot@stb-\intsect\endcsname == \knot@sta ? \knot@stb : \knot@sta}
\expandafter\let\expandafter\knot@over\csname knot@crossing@\knot@sta-\knot@stb-\intsect\endcsname
\clip (\intsectx,\intsecty) circle[radius=10pt];
    \expandafter\expandafter\expandafter\draw\expandafter\expandafter\expandafter[\csname knot@string@opts@\knot@under\endcsname,use knot path=\csname knot@string@\knot@under\endcsname] (0,0);
    \expandafter\expandafter\expandafter\draw\expandafter\expandafter\expandafter[\csname knot@string@opts@\knot@over\endcsname,use knot path=\csname knot@string@\knot@over\endcsname,white,line width=3\pgflinewidth] (0,0);
    \expandafter\expandafter\expandafter\draw\expandafter\expandafter\expandafter[\csname knot@string@opts@\knot@over\endcsname,use knot path=\csname knot@string@\knot@over\endcsname] (0,0);
  \expandafter\def\csname knot@string@opts@\the\value{knot@strings}\endcsname{#1}%
\path[save knot path=\csname knot@string@\the\value{knot@strings}\endcsname]}
\expandafter\def\csname knot@crossing@#1\endcsname{#2}}


\begin{knot}[knot/draft mode]
\strand[red,ultra thick]  (0,0) .. controls +(1,0) and +(-1,0) .. ++(2,-1) .. controls +(1,0) and +(-1,0) .. ++(2,1) .. controls +(1,0) and +(-1,0) .. ++(2,-1) .. controls +(1,0) and +(-1,0) .. ++(2,1);
\strand[blue,ultra thick] (0,-1) .. controls +(1,0) and +(-1,0) .. ++(2,1) .. controls +(1,0) and +(-1,0) .. ++(2,-1) .. controls +(1,0) and +(-1,0) .. ++(2,1) .. controls +(1,0) and +(-1,0) .. ++(2,-1);

\strand[red,ultra thick]  (0,0) .. controls +(1,0) and +(-1,0) .. ++(2,-1) .. controls +(1,0) and +(-1,0) .. ++(2,1) .. controls +(1,0) and +(-1,0) .. ++(2,-1) .. controls +(1,0) and +(-1,0) .. ++(2,1);
\strand[blue,ultra thick] (0,-1) .. controls +(1,0) and +(-1,0) .. ++(2,1) .. controls +(1,0) and +(-1,0) .. ++(2,-1) .. controls +(1,0) and +(-1,0) .. ++(2,1) .. controls +(1,0) and +(-1,0) .. ++(2,-1);

Result (draft version on top):

knots by intersection

Hopefully the syntax is obvious enough from the example. (It looks a lot nicer in the original PDF; the conversion to PNG is awful!). There are lots of places for improvement, but I thought I'd see if this was on the right track before polishing it.