%%
%% This is file `braids.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% braids.dtx  (with options: `package')
%% ----------------------------------------------------------------
%% braids --- a style file for drawing braid diagrams with TikZ/PGF
%% E-mail: stacey@math.ntnu.no
%% Released under the LaTeX Project Public License v1.3c or later
%% See http://www.latex-project.org/lppl.txt
%% ----------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{braids}[2011/05/07 v1.0 Tikz/PGF commands for drawing braid diagrams]
\def\braid@pgfversion{2.10}%
\ifx\pgfversion\braid@pgfversion
\else
\PackageWarning{braids}{This package was designed using PGF2.10; you are using \pgfversion.}%
\fi
\long\def\ge@addto@macro#1#2{%
  \begingroup
  \toks@\expandafter\expandafter\expandafter{\expandafter#1#2}%
  \xdef#1{\the\toks@}%
  \endgroup}
\newcommand{\braid}[1][]{%
  \begingroup
  \braid@start{#1}}
\def\braid@process{%
  \afterassignment\braid@handle\let\braid@token=%
}
\def\braid@process@start{%
  \afterassignment\braid@handle@start\let\braid@token=%
}
\def\braid@handle@start{%
  \let\braid@next=\braid@handle
  \ifx\braid@token a
   \let\braid@next=\braid@maybe@locate
  \else
  \ifx\braid@token(%)
   \iffalse)\fi %Indentation hack!
   \let\braid@next=\braid@assign@name
  \else
  \ifx\braid@token\@sptoken
   \let\braid@next=\braid@process@start
  \fi
  \fi
  \fi
  \braid@next%
}
\let\braid@semicolon=;
\let\braid@bar=|
\def\braid@handle{%
  \let\braid@next=\braid@process
  \ifnum\the\catcode`\;=\active
  \expandafter\let\expandafter\braid@semicolon\tikz@activesemicolon
  \fi
  \ifnum\the\catcode`\|=\active
  \expandafter\let\expandafter\braid@bar\tikz@activebar
  \fi
  \ifx\braid@token\braid@semicolon
   \let\braid@next=\braid@render
  \else
  \ifx\braid@token^
   \let\braid@next=\braid@sup
  \else
  \ifx\braid@token_
   \let\braid@next=\braid@sub
  \else
  \ifx\braid@token-
   \braid@increase@levelfalse
  \else
  \ifx\braid@token[
   \let\braid@next=\braid@process@options
  \else
  \ifx\braid@token\braid@bar
   \edef\braid@tmp{,\expandafter\the\value{braid@level}}%
   \ge@addto@macro\braid@floors\braid@tmp%
  \else
  \ifx\braid@token\bgroup
   \braid@beginscope
  \else
  \ifx\braid@token\egroup
   \braid@endscope
  \else
  \ge@addto@macro\braid@label{\braid@token}%
  \fi
  \fi
  \fi
  \fi
  \fi
  \fi
  \fi
  \fi
  \braid@next%
}
\def\braid@maybe@locate{%
  \afterassignment\braid@@maybe@locate\let\braid@token=%
}
\def\braid@@maybe@locate{%
  \let\braid@next=\braid@handle
  \ifx\braid@token t
   \let\braid@next=\braid@find@location
  \fi
  \braid@next%
}
\def\braid@find@location{%
  \afterassignment\braid@@find@location\let\braid@token=%
}
\def\braid@@find@location{%
  \let\braid@next=\braid@location@error
  \ifx\braid@token(%)
   \let\braid@next=\braid@locate
  \else
  \ifx\braid@token\@sptoken
   \let\braid@next=\braid@find@location
  \fi
  \fi
  \braid@next%
}
\def\braid@location@error{%
  \PackageWarning{braids}{Could not figure out location for braid}%
  \braid@process@start%
}
\def\braid@locate#1){%
  \tikz@scan@one@point\braid@relocate(#1)%
}
\def\braid@relocate#1{%
  #1\relax
  \advance\pgf@x by -\braid@width
  \pgftransformshift{\pgfqpoint{\pgf@x}{\pgf@y}}
  \braid@process@start%
}
\def\braid@assign@name#1){%
  \def\braid@name{#1}%
  \braid@process@start%
}
\def\braid@process@options#1]{%
    \tikzset{#1}%
  \braid@process%
}
\def\braid@sup#1_#2{\g@addto@macro\braid@label{_{#2}^{#1}}\braid@add@crossing{#2}{#1}}
\def\braid@sub#1{\@ifnextchar^{\braid@@sub{#1}}{\g@addto@macro\braid@label{_{#1}}\braid@add@crossing{#1}{1}}}
\def\braid@@sub#1^#2{\g@addto@macro\braid@label{_{#1}^{#2}}\braid@add@crossing{#1}{#2}}
\def\braid@ne{1}
\def\braid@add@crossing#1#2{%
  \edef\braid@crossing@type{#2}%
  \edef\braid@this@strand{#1}%
  \pgfmathtruncatemacro{\braid@next@strand}{#1+1}
  \ifbraid@increase@level
  \stepcounter{braid@level}
  \fi
  \braid@increase@leveltrue
  \braid@tx=\braid@this@strand\braid@width
  \braid@ty=\value{braid@level}\braid@height
  \advance\braid@ty by \braid@eh
  \braid@nx=\braid@tx
  \braid@ny=\braid@ty
  \advance\braid@nx by \braid@width
  \advance\braid@ny by \braid@height
  \braid@cy=\braid@ty
  \braid@dy=\braid@ny
  \advance\braid@cy by .5\braid@height
  \advance\braid@dy by -.5\braid@height
  \advance\braid@ty by .05\braid@height
  \advance\braid@ny by -.05\braid@height
  \expandafter\let\expandafter\braid@this@path@origin\csname braid@strand@\braid@this@strand @origin\endcsname
\ifx\braid@this@path@origin\relax
  \let\braid@this@path@origin\braid@this@strand
  \pgfsyssoftpath@setcurrentpath{\@empty}
  \pgfpathmoveto{\pgfpoint{\braid@tx}{0pt}}
  \pgfsyssoftpath@getcurrentpath{\braid@this@path}
  \else
  \expandafter\let\expandafter\braid@this@path\csname braid@strand@\braid@this@path@origin\endcsname
  \fi
  \expandafter\let\expandafter\braid@next@path@origin\csname braid@strand@\braid@next@strand @origin\endcsname
  \ifx\braid@next@path@origin\relax
  \let\braid@next@path@origin\braid@next@strand
  \pgfsyssoftpath@setcurrentpath{\@empty}
  \pgfpathmoveto{\pgfpoint{\braid@nx}{0pt}}
  \pgfsyssoftpath@getcurrentpath{\braid@next@path}
  \else
  \expandafter\let\expandafter\braid@next@path\csname braid@strand@\braid@next@path@origin\endcsname
  \fi
  \pgfsyssoftpath@setcurrentpath{\braid@this@path}
  \pgfpathlineto{\pgfqpoint{\braid@tx}{\braid@ty}}
\ifx\braid@crossing@type\braid@over@cross
\pgfpathcurveto{\pgfqpoint{\braid@tx}{\braid@cy}}{\pgfqpoint{\braid@nx}{\braid@dy}}{\pgfqpoint{\braid@nx}{\braid@ny}}
\else
\pgfpathcurvebetweentimecontinue{0}{.4}{\pgfqpoint{\braid@tx}{\braid@ty}}{\pgfqpoint{\braid@tx}{\braid@cy}}{\pgfqpoint{\braid@nx}{\braid@dy}}{\pgfqpoint{\braid@nx}{\braid@ny}}
\pgfpathcurvebetweentime{.6}{1}{\pgfqpoint{\braid@tx}{\braid@ty}}{\pgfqpoint{\braid@tx}{\braid@cy}}{\pgfqpoint{\braid@nx}{\braid@dy}}{\pgfqpoint{\braid@nx}{\braid@ny}}
\fi
  \pgfsyssoftpath@getcurrentpath{\braid@this@path}
  \pgfsyssoftpath@setcurrentpath{\braid@next@path}
  \pgfpathlineto{\pgfqpoint{\braid@nx}{\braid@ty}}
\ifx\braid@crossing@type\braid@over@cross
\pgfpathcurvebetweentimecontinue{0}{.4}{\pgfqpoint{\braid@nx}{\braid@ty}}{\pgfqpoint{\braid@nx}{\braid@cy}}{\pgfqpoint{\braid@tx}{\braid@dy}}{\pgfqpoint{\braid@tx}{\braid@ny}}
\pgfpathcurvebetweentime{.6}{1}{\pgfqpoint{\braid@nx}{\braid@ty}}{\pgfqpoint{\braid@nx}{\braid@cy}}{\pgfqpoint{\braid@tx}{\braid@dy}}{\pgfqpoint{\braid@tx}{\braid@ny}}
\else
  \pgfpathcurveto{\pgfqpoint{\braid@nx}{\braid@cy}}{\pgfqpoint{\braid@tx}{\braid@dy}}{\pgfqpoint{\braid@tx}{\braid@ny}}
\fi
  \pgfsyssoftpath@getcurrentpath{\braid@next@path}
  \expandafter\let\csname braid@strand@\braid@this@path@origin \endcsname\braid@this@path
  \expandafter\let\csname braid@strand@\braid@next@path@origin \endcsname\braid@next@path
  \expandafter\let\csname braid@strand@\braid@this@strand @origin\endcsname\braid@next@path@origin
  \expandafter\let\csname braid@strand@\braid@next@strand @origin\endcsname\braid@this@path@origin
  \pgfmathparse{\value{braid@strands} < \braid@next@strand ? "\noexpand\setcounter{braid@strands}{\braid@next@strand}" : ""}
  \pgfmathresult
  \braid@process%
}
\def\braid@floors@trim,{}
\def\braid@render@floor{%
    \draw (\floorsx,\floorsy) rectangle (\floorex,\floorey);
}
\def\braid@render{
    \ifx\braid@floors\@empty
    \else
    \pgfsys@beginscope
    \pgfsyssoftpath@setcurrentpath{\empty}
    \edef\braid@floors{\expandafter\braid@floors@trim\braid@floors}
    \braid@tx=\braid@width
    \advance\braid@tx by \braid@eh
    \braid@nx=\value{braid@strands}\braid@width
    \advance\braid@nx by -\braid@eh
    \foreach \braid@f in \braid@floors {
      \pgfsys@beginscope
      \braid@ty=\braid@f\braid@height
      \advance\braid@ty by \braid@eh
      \advance\braid@ty by \braid@height
      \braid@ny=\braid@ty
      \advance\braid@ny by \braid@height
      \edef\floorsx{\the\braid@tx}
      \edef\floorsy{\the\braid@ty}
      \edef\floorex{\the\braid@nx}
      \edef\floorey{\the\braid@ny}
      \let\tikz@options=\pgfutil@empty
    \expandafter\tikzset\expandafter{\braid@floors@style}
      \pgfmathtruncatemacro{\braid@ff}{\braid@f+2}
    \expandafter\let\expandafter\braid@floor@style\csname braid@options@floor@\braid@ff\endcsname
    \ifx\braid@floor@style\relax
    \else
    \expandafter\tikzset\expandafter{\braid@floor@style}%
    \fi
\tikz@options
\braid@render@floor
\pgfsys@endscope
    }
    \pgfsys@endscope
    \fi
  \stepcounter{braid@level}
  \foreach \braid@k in {1,...,\value{braid@strands}} {
    \pgfsys@beginscope
    \tikz@mode@drawtrue%
    \let\tikz@mode=\pgfutil@empty
    \let\tikz@options=\pgfutil@empty
    \braid@tx=\braid@k\braid@width
    \braid@ty=\value{braid@level}\braid@height
    \advance\braid@ty by 2\braid@eh
    \expandafter\let\expandafter\braid@path@origin\csname braid@strand@\braid@k @origin\endcsname
    \ifx\braid@path@origin\relax
    \pgfsyssoftpath@setcurrentpath{\@empty}
    \pgfpathmoveto{\pgfqpoint{\braid@tx}{0pt}}
    \let\braid@path@origin\braid@k
    \else
    \expandafter\let\expandafter\braid@path\csname braid@strand@\braid@path@origin\endcsname
    \pgfsyssoftpath@setcurrentpath{\braid@path}
    \fi
    \pgflineto{\pgfqpoint{\braid@tx}{\braid@ty}}
    \expandafter\tikzset\expandafter{\braid@style}
    \expandafter\let\expandafter\braid@style\csname braid@options@strand@\braid@path@origin\endcsname
    \ifx\braid@style\relax
    \else
    \expandafter\tikzset\expandafter{\braid@style}
    \fi
\braid@options
    \tikz@mode
    \tikz@options
      \edef\tikz@temp{\noexpand\pgfusepath{%
          \iftikz@mode@draw draw\fi%
      }}%
      \tikz@temp
\ifx\braid@name\pgfutil@empty
\else
\coordinate (\braid@name-\braid@path@origin-e) at (\braid@tx,\braid@ty);
\coordinate (\braid@name-rev-\braid@k-e) at (\braid@tx,\braid@ty);
\braid@nx=\braid@path@origin\braid@width
\coordinate (\braid@name-\braid@path@origin-s) at (\braid@nx,0pt);
\coordinate (\braid@name-rev-\braid@k-s) at (\braid@nx,0pt);
\fi
   \pgfsys@endscope
  }
    \ifx\braid@name\pgfutil@empty
    \else
    \braid@tx=\value{braid@strands}\braid@width
    \braid@ty=\value{braid@level}\braid@height
    \advance\braid@ty by 2\braid@eh
    \advance\braid@tx by \braid@width
    \braid@tx=.5\braid@tx
    \braid@ty=.5\braid@ty
    \coordinate (\braid@name) at (\braid@tx,\braid@ty);
    \fi
    \pgfsys@endscope
  \endgroup}
\def\braid@start#1{%
      \pgfsys@beginscope
      \setcounter{braid@level}{-1}%
      \let\braid@label\@empty
      \let\braid@floors\@empty
      \let\braid@name\empty
      \pgfkeys{/pgf/braid/.cd,#1}
      \let\braid@options\tikz@options
  \tikz@transform
      \setcounter{braid@strands}{\pgfkeysvalueof{/pgf/braid/number of           strands}}%
    \braid@width=\pgfkeysvalueof{/pgf/braid/width}
    \braid@height=\pgfkeysvalueof{/pgf/braid/height}
    \braid@eh=\pgfkeysvalueof{/pgf/braid/border height}
    \braid@height=-\braid@height
    \braid@eh=-\braid@eh
    \braid@increase@leveltrue
    \braid@process@start
}
\newdimen\braid@width
\newdimen\braid@height
\newdimen\braid@tx
\newdimen\braid@ty
\newdimen\braid@nx
\newdimen\braid@ny
\newdimen\braid@cy
\newdimen\braid@dy
\newdimen\braid@eh
\newif\ifbraid@increase@level
\let\braid@style\pgfutil@empty
\let\braid@floors@style\pgfutil@empty
\def\braid@over@cross{1}
\newcounter{braid@level}
\newcounter{braid@strands}
\pgfkeys{
    /tikz/braid/.search also={/pgf},
    /pgf/braid/.search also={/pgf,/tikz},
    /pgf/braid/.cd,
    number of strands/.initial=0,
    height/.initial=1cm,
    width/.initial=1cm,
    border height/.initial=.25cm,
    name/.code={%
      \def\braid@name{#1}%
    },
    at/.code={%
      \braid@relocate{#1}%
    },
    floor command/.code={%
      \def\braid@render@floor{#1}%
    },
    style strands/.code 2 args={%
      \def\braid@temp{#2}%
      \braidset{style each strand/.list={#1}}%
    },
    style each strand/.code={%
      \expandafter\edef\csname braid@options@strand@#1\endcsname{\braid@temp}%
    },
    style floors/.code 2 args={%
      \def\braid@temp{#2}%
      \braidset{style each floor/.list={#1}}%
    },
    style each floor/.code={%
      \expandafter\edef\csname braid@options@floor@#1\endcsname{\braid@temp}%
    },
    style all floors/.code={%
      \def\braid@floors@style{#1}
    }
}
\def\braidset#1{%
  \pgfkeys{/pgf/braid/.cd,#1}}

%% 
%% Copyright (C) 2011 by Andrew Stacey <stacey@math.ntnu.no>
%% 
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License (LPPL), either
%% version 1.3c of this license or (at your option) any later
%% version.  The latest version of this license is in the file:
%% 
%% http://www.latex-project.org/lppl.txt
%% 
%% This work is "maintained" (as per LPPL maintenance status) by
%% Andrew Stacey.
%% 
%% This work consists of the files  braids.dtx
%%                                  braids_doc.tex
%% and the derived files            README.txt,
%%                                  braids.ins,
%%                                  braids.pdf,
%%                                  braids.sty,
%%                                  braids_doc.pdf.
%% 
%%
%% End of file `braids.sty'.
