21

This question led to a new package:
braids — Blog: The Braids Package

I search to draw braids diagram, if possible in xy-pic. I already know how to do that with the knot module and some low-level instructions and I search a nicer way to do that.

The actual code :

\documentclass{minimal}
\usepackage[all,knot]{xy}
\begin{document}
\[\vcenter{\xy 0;/r1pc/:
    @={(0.5,-0.5),(0,0)}, @@{*{\vtwistneg}},(0,-2),\vtwist,
    @i@={(0,-1),(2,0),(2,-2)} @@{="save";"save"-(0,1),**@{-}}\endxy}
    \quad = \quad
    \vcenter{\xy 0;/r1pc/:
    @={(0.5,0),(0,-0.5),(0.5,-1)}, @@{*{\vtwistneg}},
    @i@={(0,0),(2,-1),(0,-2)} @@{="save";"save"-(0,1),**@{-}}\endxy}\]
\end{document}

And the desired result enter image description here

doncherry
  • 54,637
PHL
  • 7,555

2 Answers2

24

Update (2019-05-09): With the advent of TikZ's pic syntax I figured the braids package was well overdue an overhaul. The syntax below is for the original version of the package which is now depreciated (though still works). See the package documentation for how to use the new syntax.

Update (2011-09-06): The resulting package is now on CTAN as http://www.ctan.org/pkg/braids and you can read about it on our blog at http://tex.blogoverflow.com/2011/09/the-braids-package/.


Taking up Charles' challenge, here's a way of doing this in TikZ/PGF. First, the output:

braid

Next, the user code; namely, if the yucky preamble were in a nice package, this is what you would type:

\begin{document}
\begin{tikzpicture}
\braid[braid colour=red,strands=3,braid start={(0,0)}]
{\sigma_1 \sigma_2 \sigma_1^{-1}}
\node[font=\Huge] at (4.5,-1.5) {\(=\)};
\braid[strands=3,braid start={(5,0)}]
{\sigma_2 \sigma_1 \sigma_2}
\end{tikzpicture}
\end{document}

Lastly, the yucky preamble (actually including the user code so you can just copy this for a MWE):

\documentclass{standalone}
\usepackage{tikz}
\newcounter{braid}
\newcounter{strands}
\pgfkeyssetvalue{/tikz/braid height}{1cm}
\pgfkeyssetvalue{/tikz/braid width}{1cm}
\pgfkeyssetvalue{/tikz/braid start}{(0,0)}
\pgfkeyssetvalue{/tikz/braid colour}{black}
\pgfkeys{/tikz/strands/.code={\setcounter{strands}{#1}}}

\makeatletter
\def\cross{%
  \@ifnextchar^{\message{Got sup}\cross@sup}{\cross@sub}}

\def\cross@sup^#1_#2{\render@cross{#2}{#1}}

\def\cross@sub_#1{\@ifnextchar^{\cross@@sub{#1}}{\render@cross{#1}{1}}}

\def\cross@@sub#1^#2{\render@cross{#1}{#2}}


\def\render@cross#1#2{
  \def\strand{#1}
  \def\crossing{#2}
  \pgfmathsetmacro{\cross@y}{-\value{braid}*\braid@h}
  \pgfmathtruncatemacro{\nextstrand}{#1+1}
  \foreach \thread in {1,...,\value{strands}}
  {
    \pgfmathsetmacro{\strand@x}{\thread * \braid@w}
    \ifnum\thread=\strand
    \pgfmathsetmacro{\over@x}{\strand * \braid@w + .5*(1 - \crossing) * \braid@w}
    \pgfmathsetmacro{\under@x}{\strand * \braid@w + .5*(1 + \crossing) * \braid@w}
    \draw[braid] \pgfkeysvalueof{/tikz/braid start} +(\under@x pt,\cross@y pt) to[out=-90,in=90] +(\over@x pt,\cross@y pt -\braid@h);
    \draw[braid] \pgfkeysvalueof{/tikz/braid start} +(\over@x pt,\cross@y pt) to[out=-90,in=90] +(\under@x pt,\cross@y pt -\braid@h);
    \else
    \ifnum\thread=\nextstrand
    \else
     \draw[braid] \pgfkeysvalueof{/tikz/braid start} ++(\strand@x pt,\cross@y pt) -- ++(0,-\braid@h);
    \fi
   \fi
  }
  \stepcounter{braid}
}

\tikzset{braid/.style={double=\pgfkeysvalueof{/tikz/braid colour},double distance=1pt,line width=2pt,white}}

\newcommand{\braid}[2][]{%
  \begingroup
  \pgfkeys{/tikz/strands=2}
  \tikzset{#1}
  \pgfkeysgetvalue{/tikz/braid width}{\braid@w}
  \pgfkeysgetvalue{/tikz/braid height}{\braid@h}
  \setcounter{braid}{0}
  \let\sigma=\cross
  #2
  \endgroup
}
\makeatother


\begin{document}
\begin{tikzpicture}
\braid[braid colour=red,strands=3,braid start={(0,0)}]%
{\sigma_1 \sigma_2 \sigma_1^{-1}}
\node[font=\Huge] at (4.5,-1.5) {\(=\)};
\braid[strands=3,braid start={(5,0)}]
{\sigma_2 \sigma_1 \sigma_2}
\end{tikzpicture}
\end{document}

Improvements: the actual drawing commands could be optimised a little. The braids don't actually start at the start, but are shifted one over (not hard to correct). It should also be possible to work out the number of strands from the given braid: if we encounter an element using a strand we've yet to see, simply draw it from the top to the current height.

(I did try to do a bit of catcode tomfoolery so that one could use s in place of \sigma, but I couldn't get it to work (and, yes, I did replace the command by an environment in trying to do this).)

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • Thanks a lot, it's perfect even it's not a xy-pic solution. I wait a little more before accept your answer to see if there is a xy guru around here. – PHL Apr 29 '11 at 12:58
  • Could I suggest you to make your code a package and to release it on Ctan as it could be useful for (a lot of) people. – PHL Apr 29 '11 at 17:45
  • @Matsaya: Sure. I could add it to the list at: http://meta.tex.stackexchange.com/q/1220/86 – Andrew Stacey Apr 29 '11 at 17:59
  • @Matsaya: Thinking about this a bit more, may I ask what you'd want from such a package? I'm quite new to knots, links, and braids so am not sure what functionality would be useful. – Andrew Stacey Apr 29 '11 at 20:55
  • Sure you can. The most important is already contained in your code. The additional features which are nice are : 1) the possibility to draw horizontal braids 2) the possibilities to draw the "floors" 3)possibilities to have 2 (or more) nodes on the same floor (not really necessary) 4) to color a group of floor 5) individually choose the decoration (color, etc) of each "string" 6) possibility to label the top or bottom of string (individually) 7) maybe more things, I will think about that. But don't be afraid, like this, it's already a good code. – PHL Apr 29 '11 at 21:26
  • some exemples : http://matrix.cmi.ua.ac.be/fun/index.php/f_un-and-braid-groups.html – PHL Apr 29 '11 at 21:26
  • @Matsaya: Oh good grief! Is this really linked to F_un?!?! I think I might have to run a long way very fast. Seriously, those are good ideas. Is a "floor" the part corresponding to a generator? – Andrew Stacey Apr 29 '11 at 21:32
  • The "floors" are marked with dotted lines in the F_un link. And when I'm thinking of it : 8) some people like to have directed strings (with arrows on it) But maybe I could write you an email with all that and a little more ? (I already found your email address) – PHL Apr 30 '11 at 01:41
  • @Matsaya: You can email me if you like, but I prefer having these discussions in public as then others can follow and join in if they so wish. We have a chat room here for exactly this: developing packages from answers. It is: http://chat.stackexchange.com/rooms/409/from-answers-to-packages – Andrew Stacey Apr 30 '11 at 17:33
  • @Matsaya: I've just asked you a question on that link in my previous comment as a starting point. – Andrew Stacey Apr 30 '11 at 18:59
  • @Matsaya: I've just edited my answer to link to a preliminary style file. – Andrew Stacey May 07 '11 at 19:37
  • 1
    very nice work. – PHL May 10 '11 at 20:53
  • Andrew, @Matsaya: I've put this up on the favourite PGF/TikZ answers thread. – Charles Stewart May 26 '11 at 10:19
7

Stijn Symens has a nice Metapost package for drawing braids based on the abstraction of giving a string that specifies the crossings in the order that they occur, using lower case letters to draw left over right crossings and upper case the converse. He has written a brief introduction.

I don't know of any comparatively concise way of representing braids in Xypic, or PGF/Tikz for that matter, though writing a similar package for PGF should not be so hard for a PGF guru.

Charles Stewart
  • 21,014
  • 5
  • 65
  • 121
  • And writing similar module for xy should not be sor hard for a xy guru. But it seems that there are more PGF gurus here. – PHL Apr 29 '11 at 12:56
  • @Matsaya: Right, but PGF has abstractions that make this sort of thing easier: look at how Andrew's soln uses \foreach \thread in {1,...,\value{strands}} and the \pgfkeys... macros. Metapost is easiest, because it has proper string primitives. – Charles Stewart Apr 29 '11 at 13:57
  • Is it normal that the compilation with metapost gives me an error ? Anyway, it works fine with metafun. – PHL Apr 29 '11 at 21:04