%%
%% This is file `braids.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% braids_code.dtx  (with options: `package')
%% ----------------------------------------------------------------
%% braids --- a style file for drawing braid diagrams with TikZ/PGF
%% E-mail: loopspace@mathforge.org
%% Released under the LaTeX Project Public License v1.3c or later
%% See http://www.latex-project.org/lppl.txt
%% ----------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{braids}[2011/10/18 v1.1 Tikz/PGF commands for drawing braid diagrams]
\PackageWarning{braids}{%
  This package has been reimplemented as a TikZ library; if starting with a fresh document, please consider using that instead.%
}%
\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@token1%
   \ifbraid@increase@level
    \stepcounter{braid@level}
   \fi
   \braid@increase@leveltrue
   \ge@addto@macro\braid@label{\braid@token}%
  \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
  \ifx\braid@token\braid@olabel@strand
   \let\braid@next=\braid@olabel@strand
  \else
  \ifx\braid@token\braid@clabel@strand
   \let\braid@next=\braid@clabel@strand
  \else
  \ge@addto@macro\braid@label{\braid@token}%
  \fi
  \fi
  \fi
  \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
  \advance\braid@ty by \braid@nf\braid@height
  \advance\braid@ny by -\braid@nf\braid@height
  \braid@cy=\braid@ty
  \braid@dy=\braid@ny
  \advance\braid@cy by \braid@cf\braid@height
  \advance\braid@dy by -\braid@cf\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}}
\pgfmathsetmacro{\braid@gst}{0.5 - \pgfkeysvalueof{/pgf/braid/gap}}%
\pgfmathsetmacro{\braid@gend}{0.5 + \pgfkeysvalueof{/pgf/braid/gap}}%
\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}{\braid@gst}%
  {\pgfqpoint{\braid@tx}{\braid@ty}}%
  {\pgfqpoint{\braid@tx}{\braid@cy}}%
  {\pgfqpoint{\braid@nx}{\braid@dy}}%
  {\pgfqpoint{\braid@nx}{\braid@ny}}%
  \pgfpathcurvebetweentime{\braid@gend}{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}{\braid@gst}%
  {\pgfqpoint{\braid@nx}{\braid@ty}}%
  {\pgfqpoint{\braid@nx}{\braid@cy}}%
  {\pgfqpoint{\braid@tx}{\braid@dy}}%
  {\pgfqpoint{\braid@tx}{\braid@ny}}
  \pgfpathcurvebetweentime{\braid@gend}{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%
}
\newcommand{\braid@olabel@strand}[3][]{%
  \edef\braid@tmp{{\the\value{braid@level}}}%
  \expandafter\ifx\csname braid@strand@#2@origin\endcsname\relax
  \g@addto@macro\braid@tmp{{#2}}%
  \else
  \edef\braid@tmpa{{\csname braid@strand@#2@origin\endcsname}}%
  \ge@addto@macro\braid@tmp{\braid@tmpa}%
  \fi
  \g@addto@macro\braid@tmp{{#3}{#1}}%
  \ge@addto@macro{\braid@strand@labels}{\braid@tmp}%
  \braid@process%
}
\newcommand{\braid@clabel@strand}[3][]{%
  \edef\braid@tmp{{\the\value{braid@level}}}%
  \g@addto@macro\braid@tmp{{#2}{#3}{#1}}%
  \ge@addto@macro{\braid@strand@labels}{\braid@tmp}%
  \braid@process%
}
\def\braid@floors@trim,{}
\def\braid@render@floor{%
    \draw (\floorsx,\floorsy) rectangle (\floorex,\floorey);
}
\def\braid@render@strand@labels#1{%
  \def\braid@tmp{#1}%
  \ifx\braid@tmp\pgfutil@empty
  \let\braid@next=\pgfutil@gobble
  \else
  \let\braid@next=\braid@@render@strand@labels
  \fi
  \braid@next{#1}%
}
\def\braid@@render@strand@labels#1#2#3#4{%
  \begingroup
  \pgfscope
  \let\tikz@options=\pgfutil@empty
  \let\tikz@mode=\pgfutil@empty
  \let\tik@transform=\pgfutil@empty
  \let\tikz@fig@name=\pgfutil@empty
  \tikzset{/pgf/braid/strand label,#4}%
  \braid@nx=#2\braid@width
  \braid@ny=#1\braid@height
  \advance\braid@ny by \braid@eh
  \advance\braid@ny by \braid@height
  \pgftransformshift{\pgfqpoint{\braid@nx}{\braid@ny}}%
  \tikz@options
  \setbox\pgfnodeparttextbox=\hbox%
  \bgroup%
  \tikzset{every text node part/.try}%
  \ifx\tikz@textopacity\pgfutil@empty%
  \else%
  \pgfsetfillopacity{\tikz@textopacity}%
  \pgfsetstrokeopacity{\tikz@textopacity}%
  \fi%
  \pgfinterruptpicture%
  \tikz@textfont%
  \ifx\tikz@text@width\pgfutil@empty%
  \else%
  \begingroup%
  \pgfmathsetlength{\pgf@x}{\tikz@text@width}%
  \pgfutil@minipage[t]{\pgf@x}\leavevmode\hbox{}%
  \tikz@text@action%
  \fi%
  \tikz@atbegin@node%
  \bgroup%
  \aftergroup\unskip%
  \ifx\tikz@textcolor\pgfutil@empty%
  \else%
  \pgfutil@colorlet{.}{\tikz@textcolor}%
  \fi%
  \pgfsetcolor{.}%
  \setbox\tikz@figbox=\box\pgfutil@voidb@x%
  \tikz@uninstallcommands%
  \tikz@halign@check%
  \ignorespaces%
  #3
  \egroup
  \tikz@atend@node%
  \ifx\tikz@text@width\pgfutil@empty%
  \else%
  \pgfutil@endminipage%
  \endgroup%
  \fi%
  \endpgfinterruptpicture%
  \egroup%
   \ifx\tikz@text@width\pgfutil@empty%
    \else%
      \pgfmathsetlength{\pgf@x}{\tikz@text@width}%
      \wd\pgfnodeparttextbox=\pgf@x%
    \fi%
    \ifx\tikz@text@height\pgfutil@empty%
    \else%
      \pgfmathsetlength{\pgf@x}{\tikz@text@height}%
      \ht\pgfnodeparttextbox=\pgf@x%
    \fi%
    \ifx\tikz@text@depth\pgfutil@empty%
    \else%
      \pgfmathsetlength{\pgf@x}{\tikz@text@depth}%
      \dp\pgfnodeparttextbox=\pgf@x%
    \fi%
  \pgfmultipartnode{\tikz@shape}{\tikz@anchor}{\tikz@fig@name}{%
    {\begingroup\tikz@finish}%
  }%
  \endpgfscope
  \endgroup
  \braid@render@strand@labels%
}
\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
  \ifx\braid@strand@labels\pgfutil@empty
  \else
   \expandafter\braid@render@strand@labels\braid@strand@labels{}%
  \fi
    \pgfsys@endscope
  \endgroup}
\def\braid@start#1{%
  \pgfsys@beginscope
  \setcounter{braid@level}{-1}%
  \let\braid@label\@empty
  \let\braid@strand@labels\@empty
  \let\braid@floors\@empty
  \let\braid@name\empty
  \let\clabel=\braid@clabel@strand
  \let\olabel=\braid@olabel@strand
  \pgfkeys{/pgf/braid/.cd,#1}%
  \ifbraid@strand@labels@origin
  \let\label=\braid@olabel@strand
  \else
  \let\label=\braid@clabel@strand
  \fi
  \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}%
  \pgfkeysgetvalue{/pgf/braid/control factor}{\braid@cf}%
  \pgfkeysgetvalue{/pgf/braid/nudge factor}{\braid@nf}%
  \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
\newif\ifbraid@strand@labels@origin
\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,
    gap/.initial=.1,
    border height/.initial=.25cm,
    control factor/.initial=.5,
    nudge factor/.initial=.05,
    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}
    },
    strand label/.style={},
    strand label by origin/.is if=braid@strand@labels@origin,
}
\def\braidset#1{%
  \pgfkeys{/pgf/braid/.cd,#1}}

%% 
%% Copyright (C) 2011-2019 by Andrew Stacey <loopspace@mathforge.org>
%% 
%% 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,
%%                                  tikzlibrarybraids.code.tex,
%%                                  braids_doc.pdf.
%% 
%%
%% End of file `braids.sty'.
