## How to draw knots with Latex?

I am writing an exam for my students, and the topic is intro knots theory. I have no idea how to put knots into the file, but I know many MO users who can draw amazing diagrams in their papers.

Can someone please provide some hints on what can be used, preferably with some example codes? I do not need complicated diagrams, just some simple knots and links with few crossings. Thanks in advance.

-
Plus, my phone does not take nice pictures (-: – Hailong Dao Apr 15 2010 at 7:53
Drawing and photographing and importing to computer is overcomplicating it IMO. Just print the paper with the places for the diagrams left blank, draw the diagrams by hand on the paper, and photocopy it. – Zsbán Ambrus Apr 15 2010 at 12:28
Putting the photograph in the digital file has the advantage that you don't have to keep a master physical copy on hand. So long as a substantial amount of the exam is being saved digitally anyway, there may as well be a digital master copy. (I suppose that this assumes that the exam, complete with diagrams, is likely to be reused...) – Niel de Beaudrap Apr 15 2010 at 12:55
Here is the quick and dirty for writing the test. Go to a paper on the arxiv that you know has a diagram for the knot you want on the test. Download the source. Find the diagram as a pdf file or eps file. Use the graphicx package for embedding it in your exam. – Charlie Frohman Apr 15 2010 at 13:16
Aha! Precisely the problem I had with my knot theory course. Tried the honest approach first (xfig), opted for draw it by hand and scan it after the first homework/sols. – algori Jul 7 2010 at 5:26
show 3 more comments

Knotinfo has .png files of all knots of 12 crossings or less.
http://www.indiana.edu/~knotinfo

-
This seems like the best answer! – Allen Knutson Apr 15 2010 at 13:52
Knotinfo has the advantage of using circle packings of the 2-sphere to construct its knot diagrams so they're fairly appealing diagrams. The disadvantage of knotinfo is the diagrams are efficient (maybe you want a complicated unknot, etc?), and the knots are prime. I use xfig, exported to .eps for most of the diagrams in my papers. – Ryan Budney Apr 15 2010 at 14:18
That's pretty good, but I want to draw a link as well, say sum of 2 Hopf links. I can't find links on the site, do you know some sources? – Hailong Dao Apr 15 2010 at 14:20
I ended up using this method (-:, thanks. – Hailong Dao Apr 18 2010 at 17:49
You can also create .eps files from the .png files by using the 'Trace' tool in programs like Illustrator, Inkscape or Corel Draw. This is a great thing if you want to adjust the knot, scale it or change colours. – JCollins Mar 25 2011 at 10:44

I would discourage the method of drawing them by hand and importing them via scanner etc. as they won't be scalable. Many journals would be unhappy with such an image, and in any case it would look bad and make your pdf filesize huge. Instead, I am a big fan of the free program Inkscape, which you can get for unix/linux and also mac with the X server, and probably Windows, if you're into that kind of thing. It produces pdf files, in which one can zoom in on the curves and they look nice. Also they print quickly, whereas if you include an image file in a pdf it will take a long time to print on a laser printer (which prefers postscript).

Inkscape is similar to xy-pic, only it's very easy to use. You can start by drawing the knots free hand using the pen tool. Then you click a tool which highlights all the data points of a line segment, and you can start deleting them (alternatively, you can use the command "Path->Simplify"). It also has two extra compatibilities with latex: First, you can get a script to actually run snippets of latex in inkscape to make fonts and such for labels. Alternatively, you can just compile the font you want into a pdf and import it. It will import in a scalable way. Then when you're done you export to pdf, and you can include it back into a new pdf if you are using pdflatex to compile your .tex document. If you use ordinary latex, then you need to save it to .eps; I prefer the former.

Since they only have much fancier screenshots on their homepage, here is a paper with diagrams done in Inkscape.

http://arxiv.org/pdf/0908.3013

-
I realize that some issues in paragraph 1 don't apply to writing a single exam for one-time printout, but I interpreted the question to ask about the more general challenge of drawing nice diagrams in latex. – David Jordan Apr 15 2010 at 11:16
Also you can scan a black and white sketch, and tell Inkscape to "trace" it for you, and then perform the simplifying step I mentioned (most paths need no more than 3 or 4 data points, even if they start with 100. The fewer they have the more curvy and regular they look. – David Jordan Apr 15 2010 at 11:18
yes, that is why it is useful. pdf's also use scalable vector graphics, and so we have a faithful functor svg--> pdf – David Jordan Apr 15 2010 at 12:38
+1 for the faithful functor! – Andrew Stacey Apr 15 2010 at 13:13
Version 0.47 of Inkscape has a routine for drawing knots: tavmjong.free.fr/INKSCAPE/MANUAL/html/… – Agol Aug 5 2010 at 1:19
show 3 more comments

I gave a talk on the subject of drawing knots using computers (without much detail, but I cover both 2D and 3D methods), the slides might be of interest.

-
1. I would seriously consider the photographic technology suggested by Kevin Buzzard in the comments, if you happen to have a decent digital camera handy (and if you trust your drawing skills). Or, as Peter Shor suggests in the comments, you can use a scanner if you have one handy. Multiple 2½-dimension diagrams, without tons of regularity, are not fun to draw on a computer --- unless perhaps you have access to touch-screens.

2. If you want special effects or other benefits of computer-aided graphics badly enough, you can do it directly in LaTeX with TiKZ. I recommend their fairly comprehensive and reasonably readable manual. For sufficiently complicated diagrams, I find that drawing diagrams with TiKZ is relatively painless, and more robust to changes (e.g. in how you want lines to curve, or the positions of nodes if you have them) than ones drawn in xfig.

The over/under relationships can be achieved by drawing the "over" curve-segments twice: once with a thicker white line (or a line matching the background color), and once with a thinner line corresponding to the color of the knot. If you use the PGF extensions (technology which accompanies the TiKZ package), you can quite probably do colorful things such as gradients along the knot curve, if this serves to emphasize features of your knot diagrams.

Good luck with your exam!

Added after Peter's comment — my workplace at the time of my original post has the odd feature that it is more likely to have convenient cameras than convenient scanners.

-
+1 for TikZ. Also, if you prefer to draw them but want them looking computer-generated (and want them to look integrated with the document), Inkscape has a plugin that can export to TikZ format. – Andrew Stacey Apr 15 2010 at 10:40
Ooh, I wasn't aware that Inkscape can export to TikZ. Are you referring to this ( code.google.com/p/inkscape2tikz ) or something else? – Willie Wong Apr 15 2010 at 13:43
TiKZ even has code to do the double drawing Niel talks about automatically: you want to do something like \draw [double] ... – Mikael Vejdemo-Johansson Apr 15 2010 at 14:54
Both comments 1-2 are what I would have posted. – Theo Johnson-Freyd Apr 15 2010 at 16:01
You don't need a camera. There should be a scanner you can use somewhere. – Peter Shor Feb 22 2011 at 14:06

Update 2013-04-22: I've now uploaded a TikZ library to CTAN expressly for the purpose of drawing knots. It is already in MikTeX (according to CTAN), TeXLive might have to wait until TL2013 has been released. It is released as part of the spath3 library. Here's the link to CTAN. The hobby package also contains some useful code for drawing knots. For drawing braids, there's the braids package. And here's a short blog post I wrote explaining the variety of methods.

This is a follow-up to Niels' answer, namely to use TikZ. I've recently had occasion to draw a few knots and links using TikZ and have quite a workable system. I'll not post code here (ask by email or on http://tex.stackexchange.com if you want). Using it, I can produce SVGs (as evidenced by some nLab pages: http://ncatlab.org/nlab/show/Borromean+link, http://ncatlab.org/nlab/show/Whitehead+link, and some others) as well as PDFs, PNGs, and so forth. As an example, here's the trefoil knot:

-
 Look great, thank you! I will email you someday. – Hailong Dao Feb 22 2011 at 19:36

Aaron Lauda has a nice description using the package XY-pic here. There are commands that generate pieces of knots (such as crossings in various orientations), although I prefer just using the \crv "curve" command to make splines.

-
Of course, to call the code hard to read would be an understatement... – Tilman Apr 15 2010 at 8:07
Friends don't let friends use XY-pic! – Ben Webster May 1 2010 at 14:30

Wolfram|Alpha will draw knots with Alexander-Briggs input. Mathematica will do it with almost any sort of input, including Dowker notation and Conway notation, and most any named knot. The command to look up in the help files is "KnotData".

From Wolfram|Alpha, I'm sure there's some way to grab the picture into a file. Mathematica itself will export to JPG, PNG, PDF, EPS, GIF, etc.

-

The knot atlas gang have a mathematica package for this - you paste the pictures it produces.

-

Here's a, perhaps silly, suggestion that perhaps isn't quite what you're looking for:

Draw them properly in 3D using something like Blender, and then render it to a 2D picture.

The second animation on this page is of a variant on the borromean rings.

-

EDIT - Following the suggestions of several people I have, somewhat painfully, switched from what is described below to using just pdflatex and pinlabel. I still use xfig for figures, exporting them to both eps and pdf to make pinlabel happy.

I draw knots in a different program (xfig), save them as eps files, and then include them via

\begin{figure}[htbp]
$$\begin{array}{c} \epsfig{file=Figures/foo.eps, width = 10 cm} \\ \epsfig{file=Figures/bar.eps, width = 10 cm} \end{array}$$
\caption{Top: A picture of a foo. Bottom: a bar.}
\label{Fig:FooBar}
\end{figure}


Then latex, dvips -o, ps2pdf produces the pdf file. This technique allows one to use psfrag to add labels inside of figures.

-
But then you're making yourself dependent on the ps-workflow and miss out on all the nice typographical improvements of pdftex. – Tilman Apr 15 2010 at 8:04
I draw lots of knots, and I do it this way. I think Xara is the best drawing programme for knots myself- I can draw a 15-crossing knot nicely and with everything where I want it in around 10 minutes. I also use psfrag. I include in minipages usually. e-mail me for code snippets if you are interested. – Daniel Moskovich Apr 15 2010 at 8:33
Xfig can produce PDF and can use LaTeX internally for labels (not WYSIWYG, though) so this method can be used properly with pdflatex. However, I don't recommend it due to the presence of TikZ – Andrew Stacey Apr 15 2010 at 10:39
This brings up an algorithmic question: are there nice algorithms for producing (numerically) "nice" (whatever that might mean) 3d embeddings of knots. Then use graphics programs with hidden surface elimination. – Victor Miller Apr 15 2010 at 15:02

I didn't really like Simon's link. The tutorial there seems to be based solely on drawing the curves by specifying coordinates. I find that a more, say, tabular or rigidly based approach is much easier to work with.

There's a good xy-pic tutorial for creating knots at http://lf.starlogik.de/res/XYPic-Knot-Intro.pdf

There's a great xymatrix tutorial at http://www.ctan.org/tex-archive/macros/generic/diagrams/xypic/xy/doc/xyguide.pdf

There are only 10 pages to read in each manual and you'll have mastered them both. You could get away without reading the xymatrix tutorial, but I'd reccommend reading it because it goes more in-depth into some of the parameter modifiers that aren't actually explained in the knot manual.

-

Have a look at the knot program by Kodama:

http://www.math.kobe-u.ac.jp/~kodama/knot.html

You can draw and manipulate knot diagrams using the mouse and there is an option to export the figures to pstricks. It also comes with all the prime knots of up to 10 crossings.

-

You can draw knots with metapost:

\documentclass{article}
\usepackage{mpgraphics}
\begin{document}
\begin{mpdisplay}
for i=0 step 1 until 360:
pair P;
P = 2cm* (sind(2*i), cosd(3*i));
fill fullcircle scaled 4mm shifted P withcolor white;
draw fullcircle scaled 4mm shifted P;
endfor;
picture p;
p:=nullpicture;
for i=-180 step 1 until 180:
pair P;
P = 2cm* (sind(2*i), cosd(3*i));
addto p contour fullcircle scaled 4mm shifted P withcolor white;
addto p doublepath fullcircle scaled 4mm shifted P
withpen pencircle scaled .5bp;
endfor;
clip p to (.5cm,2.5cm) -- (.5cm,1.5cm)--
(-.5cm,1.5cm) -- (-.5cm,2.5cm)--cycle;
draw p;
\end{mpdisplay}
\end{document}


or use pst-knot package (using postscript).

-
Finding explicit parametrizations of knots seems to be awfully time consuming for the purposes of answering Hailong's original question! – Ryan Budney Mar 24 2011 at 17:47