Debian Codegraph @

texmf-dist/source/latex/braids/braids_code.dtx

raw
1 % \iffalse meta-comment
2 %<*internal>
3 \iffalse
4 %</internal>
5 %<*readme>
6 ----------------------------------------------------------------
7 braids --- a TikZ library for drawing braid diagrams
8 bundle version: v2.3 2024/01/09
9 E-mail: Andrew Stacey <[email protected]>
10 Released under the LaTeX Project Public License v1.3c or later
11 See http://www.latex-project.org/lppl.txt
12 ----------------------------------------------------------------
13
14 This library defines some commands for drawing braid diagrams with TikZ/PGF.
15 The initial idea of this package came from a question and answer on the site https://tex.stackexchange.com.
16
17 The package comes in two versions: a package (braids.sty) and a TikZ library (tikzlibrarybraids.code.tex). The original package (braids.sty) is depreciated and has been frozen at version 1.1. The TikZ library is maintained and should be used in preference, except for legacy code.
18 %</readme>
19 %<*internal>
20 \fi
21 \def\nameofplainTeX{plain}
22 \ifx\fmtname\nameofplainTeX\else
23 \expandafter\begingroup
24 \fi
25 %</internal>
26 %<*install>
27 \input l3docstrip.tex
28 \keepsilent
29 \askforoverwritefalse
30 \preamble
31 ----------------------------------------------------------------
32 braids --- a TikZ library for drawing braid diagrams
33 bundle version: v2.3 2024/01/09
34 E-mail: Andrew Stacey <[email protected]>
35 Released under the LaTeX Project Public License v1.3c or later
36 See http://www.latex-project.org/lppl.txt
37 ----------------------------------------------------------------
38
39 \endpreamble
40 \postamble
41
42 Copyright (C) 2011-2024 by Andrew Stacey <[email protected]>
43
44 This work may be distributed and/or modified under the
45 conditions of the LaTeX Project Public License (LPPL), either
46 version 1.3c of this license or (at your option) any later
47 version. The latest version of this license is in the file:
48
49 http://www.latex-project.org/lppl.txt
50
51 This work is "maintained" (as per LPPL maintenance status) by
52 Andrew Stacey.
53
54 This work consists of the files braids.dtx
55 braids_doc.tex
56 and the derived files README.txt,
57 braids.ins,
58 braids.pdf,
59 braids.sty,
60 tikzlibrarybraids.code.tex,
61 braids_doc.pdf.
62
63 \endpostamble
64 \usedir{tex/latex/braids}
65 \generate{
66 \file{braids.sty}{\from{\jobname.dtx}{package}}
67 }
68 \generate{
69 \file{tikzlibrarybraids.code.tex}{\from{\jobname.dtx}{library}}
70 }
71 %</install>
72 %<install>\endbatchfile
73 %<*internal>
74 \usedir{source/latex/braids}
75 \generate{
76 \file{\jobname.ins}{\from{\jobname.dtx}{install}}
77 }
78 \nopreamble\nopostamble
79 \usedir{doc/latex/braids}
80 \generate{
81 \file{README.txt}{\from{\jobname.dtx}{readme}}
82 }
83 \ifx\fmtname\nameofplainTeX
84 \expandafter\endbatchfile
85 \else
86 \expandafter\endgroup
87 \fi
88 %</internal>
89 %<*driver>
90 \documentclass{l3doc}
91 \usepackage[T1]{fontenc}
92 \usepackage{lmodern}
93 \usepackage{tikz}
94 \usepackage[nowarning]{braids}
95 \usetikzlibrary{braids}
96
97 \EnableCrossrefs
98 \CodelineIndex
99 \RecordChanges
100 \begin{document}
101 \DocInput{\jobname.dtx}
102 \end{document}
103 %</driver>
104 % \fi
105 %
106 % \CheckSum{2401}
107 %
108 % \CharacterTable
109 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
110 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
111 % Digits \0\1\2\3\4\5\6\7\8\9
112 % Exclamation \! Double quote \" Hash (number) \#
113 % Dollar \$ Percent \% Ampersand \&
114 % Acute accent \' Left paren \( Right paren \)
115 % Asterisk \* Plus \+ Comma \,
116 % Minus \- Point \. Solidus \/
117 % Colon \: Semicolon \; Less than \<
118 % Equals \= Greater than \> Question mark \?
119 % Commercial at \@ Left bracket \[ Backslash \\
120 % Right bracket \] Circumflex \^ Underscore \_
121 % Grave accent \` Left brace \{ Vertical bar \|
122 % Right brace \} Tilde \~}
123 %
124 %
125 % \changes{1.0}{2011/05/03}{Converted to DTX file}
126 % \changes{1.1}{2011/05/03}{Extended syntax}
127 % \changes{2.0}{2019/03/20}{Converted TikZ library}
128 % \changes{2.1}{2019/08/14}{Extended crossing capability}
129 % \changes{2.2}{2022/10/26}{Advanced positioning}
130 % \changes{2.3}{2024/01/09}{Disambiguated height}
131 %
132 % \DoNotIndex{\newcommand,\newenvironment}
133 %
134 % \GetFileInfo{tikzlibrarybraids.code.tex}
135 % \title{The \textsf{braids} package: codebase}
136 % \author{Andrew Stacey \\ \texttt{[email protected]}}
137 % \date{\fileversion~from \filedate}
138 %
139 % \maketitle
140 %
141 % \section{Introduction}
142 %
143 % This is a package for drawing braid diagrams using PGF/TikZ.
144 % Its inspiration was a question and answer on the website \texttt{http://tex.stackexchange.com}.
145 %
146 % \section{History}
147 %
148 % \begin{itemize}
149 % \item v1.0 First public release.
150 %
151 % \item v1.1 Added ability to configure the gap size, the control points, and the ``nudge''.
152 % Added ability to add labels to strands between crossings.
153 %
154 % \item v2 Reimplemented as TikZ library rather than a standalone package.
155 % \end{itemize}
156 % \StopEventually{}
157 %
158 % \section{Implementation}
159 %
160 % \iffalse
161 %<*package>
162 % \fi
163 %
164 % Issue a notice that this is a depreciated version of the braids package.
165 % \begin{macrocode}
166 \NeedsTeXFormat{LaTeX2e}
167 \ProvidesPackage{braids}[2011/10/18 v1.1 Tikz/PGF commands for drawing braid diagrams (depreciated package)]
168 \newif\if@braids@warning@
169 \@braids@warning@true
170 \DeclareOption{nowarning}{%
171 \@braids@warning@false
172 }
173
174 \ProcessOptions
175
176 \if@braids@warning@
177 \PackageWarning{braids}{%
178 This package is frozen at v1.1 from 2011/10/18 and has been reimplemented as a TikZ library; if starting with a fresh document, please consider using that instead.%
179 }%
180 \fi
181 % \end{macrocode}
182 %
183 % \begin{macro}[internal]{\ge@addto@macro}
184 % This is an expanded version of \Verb+\g@addto@macro+.
185 % Namely, it adds the \emph{expansion} of the second argument to the first.
186 % \begin{macrocode}
187 \long\def\ge@addto@macro#1#2{%
188 \begingroup
189 \toks@\expandafter\expandafter\expandafter{\expandafter#1#2}%
190 \xdef#1{\the\toks@}%
191 \endgroup}
192 % \end{macrocode}
193 % \end{macro}
194 %
195 % \begin{macro}{\braid}
196 % This is the user command.
197 % We start a group to ensure that all our assignments are local, and then call our initialisation code.
198 % The optional argument is for any keys to be set.
199 % \begin{macrocode}
200 \newcommand{\braid}[1][]{%
201 \begingroup
202 \braid@start{#1}}
203 % \end{macrocode}
204 % \end{macro}
205 %
206 % \begin{macro}[internal]{\braid@process}
207 % This is the token swallower.
208 % This takes the next token on the braid specification and passes it to the handler command (in the macro \Verb+\braid@token+) which decides what to do next.
209 % (Incidentally, the code here is heavily influenced by TikZ.
210 % That's probably not very surprising.)
211 % \begin{macrocode}
212 \def\braid@process{%
213 \afterassignment\braid@handle\let\braid@token=%
214 }
215 % \end{macrocode}
216 % \end{macro}
217 %
218 % \begin{macro}[internal]{\braid@process@start}
219 % This is a variant of \Verb+\braid@process+ which is used at the start where we might have a few extra bits and pieces before the braid itself starts.
220 % Specifically, we test for the \Verb+at+ and \Verb+(name)+ possibilities.
221 % \begin{macrocode}
222 \def\braid@process@start{%
223 \afterassignment\braid@handle@start\let\braid@token=%
224 }
225 % \end{macrocode}
226 % \end{macro}
227 %
228 % \begin{macro}[internal]{\braid@handle@start}
229 % This is the handler in use at the start.
230 % It looks for the tokens \Verb+a+ or \Verb+(+ which (might) signal the start of an \Verb+at (coordinate)+ or \Verb+(name)+.
231 % If we get anything else (modulo spaces) we decide that we've reached the end of the initialisation stuff and it is time to get started on the braid itself.
232 % \begin{macrocode}
233 \def\braid@handle@start{%
234 \let\braid@next=\braid@handle
235 \ifx\braid@token a
236 % \end{macrocode}
237 % We got an \Verb+a+ so we might have an \Verb+at (coordinate)+
238 % \begin{macrocode}
239 \let\braid@next=\braid@maybe@locate
240 \else
241 \ifx\braid@token(%)
242 % \end{macrocode}
243 % We got an \Verb+(+ so we have a name
244 % \begin{macrocode}
245 \iffalse)\fi %Indentation hack!
246 \let\braid@next=\braid@assign@name
247 \else
248 \ifx\braid@token\@sptoken
249 % \end{macrocode}
250 % Space; boring, redo from start
251 % \begin{macrocode}
252 \let\braid@next=\braid@process@start
253 \fi
254 \fi
255 \fi
256 \braid@next%
257 }
258 % \end{macrocode}
259 % \end{macro}
260 %
261 % \begin{macro}[internal]{\braid@handle}
262 % This is the main handler for parsing the braid word.
263 % It decides what action to take depending on what the token is.
264 % We have to be a bit careful with catcodes, some packages set
265 % \Verb+;+ and \Verb+|+ to be active.
266 % We should probably also be careful with \Verb+^+ and \Verb+_+.
267 % \begin{macrocode}
268 \let\braid@semicolon=;
269 \let\braid@bar=|
270 \def\braid@handle{%
271 \let\braid@next=\braid@process
272 % \end{macrocode}
273 % Start by checking our catcodes to see what we should check against
274 % \begin{macrocode}
275 \ifnum\the\catcode`\;=\active
276 \expandafter\let\expandafter\braid@semicolon\tikz@activesemicolon
277 \fi
278 \ifnum\the\catcode`\|=\active
279 \expandafter\let\expandafter\braid@bar\tikz@activebar
280 \fi
281 \ifx\braid@token\braid@semicolon
282 % \end{macrocode}
283 % Semicolon, means that we're done reading our braid.
284 % It's time to render it.
285 % \begin{macrocode}
286 \let\braid@next=\braid@render
287 \else
288 \ifx\braid@token^
289 % \end{macrocode}
290 % Superscript character, the next token tells us whether it's an over-crossing or an under-crossing.
291 % \begin{macrocode}
292 \let\braid@next=\braid@sup
293 \else
294 \ifx\braid@token_
295 % \end{macrocode}
296 % Subscript character, the next token tells us which strands cross.
297 % \begin{macrocode}
298 \let\braid@next=\braid@sub
299 \else
300 \ifx\braid@token-
301 % \end{macrocode}
302 % Hyphen, this is so that we can have more than one crossing on the same level.
303 % \begin{macrocode}
304 \braid@increase@levelfalse
305 \else
306 \ifx\braid@token1%
307 % \end{macrocode}
308 % 1: this means the ``identity'' crossing, so no crossing here.
309 % Increase the level, unless overriden, and add to the label.
310 % \begin{macrocode}
311 \ifbraid@increase@level
312 \stepcounter{braid@level}
313 \fi
314 \braid@increase@leveltrue
315 \ge@addto@macro\braid@label{\braid@token}%
316 \else
317 \ifx\braid@token[%
318 % \end{macrocode}
319 % Open bracket, this means we have some more options to process.
320 % \begin{macrocode}
321 \let\braid@next=\braid@process@options
322 \else
323 \ifx\braid@token\braid@bar
324 % \end{macrocode}
325 % Bar, this tells us that we want a ``floor'' at this point.
326 % \begin{macrocode}
327 \edef\braid@tmp{,\expandafter\the\value{braid@level}}%
328 \ge@addto@macro\braid@floors\braid@tmp%
329 \else
330 \ifx\braid@token\bgroup
331 % \end{macrocode}
332 % Begin group, which we reinterpret as begining a scope.
333 % \begin{macrocode}
334 \braid@beginscope
335 \else
336 \ifx\braid@token\egroup
337 % \end{macrocode}
338 % End group, which ends the scope
339 % \begin{macrocode}
340 \braid@endscope
341 \else
342 \ifx\braid@token\braid@olabel@strand
343 \let\braid@next=\braid@olabel@strand
344 \else
345 \ifx\braid@token\braid@clabel@strand
346 \let\braid@next=\braid@clabel@strand
347 \else
348 % \end{macrocode}
349 % Otherwise, we add the token to the braid label.
350 % \begin{macrocode}
351 \ge@addto@macro\braid@label{\braid@token}%
352 \fi
353 \fi
354 \fi
355 \fi
356 \fi
357 \fi
358 \fi
359 \fi
360 \fi
361 \fi
362 \fi
363 \braid@next%
364 }
365 % \end{macrocode}
366 % \end{macro}
367 %
368 % \begin{macro}[internal]{\braid@maybe@locate}
369 % If we got an \Verb+a+ token in the \Verb+\braid@handle@start+ then it \emph{might} mean we're looking at \Verb+at (coordinate)+ or it might mean that the user has decided to use \Verb+a+ as the braid parameter.
370 % So we examine the next token for a \Verb+t+.
371 % \begin{macrocode}
372 \def\braid@maybe@locate{%
373 \afterassignment\braid@@maybe@locate\let\braid@token=%
374 }
375 % \end{macrocode}
376 % \end{macro}
377 %
378 % \begin{macro}[internal]{\braid@@maybe@locate}
379 % This is where we test for \Verb+t+ and act appropriately.
380 % \begin{macrocode}
381 \def\braid@@maybe@locate{%
382 \let\braid@next=\braid@handle
383 \ifx\braid@token t
384 \let\braid@next=\braid@find@location
385 \fi
386 \braid@next%
387 }
388 % \end{macrocode}
389 % \end{macro}
390 %
391 % \begin{macro}[internal]{\braid@find@location}
392 % This macro starts us looking for a coordinate.
393 % \begin{macrocode}
394 \def\braid@find@location{%
395 \afterassignment\braid@@find@location\let\braid@token=%
396 }
397 % \end{macrocode}
398 % \end{macro}
399 %
400 % \begin{macro}[internal]{\braid@@find@location}
401 % This is the test for the start of a coordinate.
402 % If we get a \Verb+(+ that means we've reached the coordinate.
403 % A space means ``carry on''.
404 % Anything else is a (non-fatal) error.
405 % \begin{macrocode}
406 \def\braid@@find@location{%
407 \let\braid@next=\braid@location@error
408 \ifx\braid@token(%)
409 \let\braid@next=\braid@locate
410 \else
411 \ifx\braid@token\@sptoken
412 \let\braid@next=\braid@find@location
413 \fi
414 \fi
415 \braid@next%
416 }
417 % \end{macrocode}
418 % \end{macro}
419 %
420 % \begin{macro}[internal]{\braid@location@error}
421 % This is our error message for not getting a location.
422 % \begin{macrocode}
423 \def\braid@location@error{%
424 \PackageWarning{braids}{Could not figure out location for braid}%
425 \braid@process@start%
426 }
427 % \end{macrocode}
428 % \end{macro}
429 %
430 % \begin{macro}[internal]{\braid@locate}
431 % If we reached a \Verb+(+ when looking for a coordinate, everything up to the next \Verb+)+ is that coordinate.
432 % Then we parse the coordinate and call the relocation macro.
433 % \begin{macrocode}
434 \def\braid@locate#1){%
435 \tikz@scan@one@point\braid@relocate(#1)%
436 }
437 % \end{macrocode}
438 % \end{macro}
439 %
440 % \begin{macro}[internal]{\braid@relocate}
441 % This is the macro that actually does the relocation.
442 % \begin{macrocode}
443 \def\braid@relocate#1{%
444 #1\relax
445 \advance\pgf@x by -\braid@width
446 \pgftransformshift{\pgfqpoint{\pgf@x}{\pgf@y}}
447 \braid@process@start%
448 }
449 % \end{macrocode}
450 % \end{macro}
451 %
452 % \begin{macro}[internal]{\braid@assign@name}
453 % This macro saves our name.
454 % \begin{macrocode}
455 \def\braid@assign@name#1){%
456 \def\braid@name{#1}%
457 \braid@process@start%
458 }
459 % \end{macrocode}
460 % \end{macro}
461 %
462 % \begin{macro}[internal]{\braid@process@options}
463 % The intention of this macro is to allow setting of style options mid-braid.
464 % (At present, this wouldn't make a lot of sense.)
465 % \begin{macrocode}
466 \def\braid@process@options#1]{%
467 \tikzset{#1}%
468 \braid@process%
469 }
470 % \end{macrocode}
471 % \end{macro}
472 %
473 % The next macros handle the actual braid elements.
474 % Everything has to have a subscript, but the superscript is optional and can come before or after the subscript.
475 %
476 % \begin{macro}[internal]{\braid@sup}
477 % This handles braid elements of the form \Verb+a^{-1}_2+.
478 % \begin{macrocode}
479 \def\braid@sup#1_#2{%
480 \g@addto@macro\braid@label{_{#2}^{#1}}%
481 \braid@add@crossing{#2}{#1}%
482 }
483 % \end{macrocode}
484 % \end{macro}
485 %
486 % \begin{macro}[internal]{\braid@sub}
487 % \begin{macrocode}
488 % This handles braid elements of the form \Verb+a_1+ or \Verb+a_1^{-1}+.
489 \def\braid@sub#1{%
490 \@ifnextchar^{\braid@@sub{#1}}%
491 {\g@addto@macro\braid@label{_{#1}}\braid@add@crossing{#1}{1}}%
492 }
493 % \end{macrocode}
494 % \end{macro}
495 %
496 % \begin{macro}[internal]{\braid@@sub}
497 % Helper macro for \Verb+\braid@sub+.
498 % \begin{macrocode}
499 \def\braid@@sub#1^#2{%
500 \g@addto@macro\braid@label{_{#1}^{#2}}%
501 \braid@add@crossing{#1}{#2}%
502 }
503 % \end{macrocode}
504 % \end{macro}
505 %
506 % \begin{macro}[internal]{\braid@ne}
507 % Remember what \Verb+1+ looks like for testing against.
508 % \begin{macrocode}
509 \def\braid@ne{1}
510 % \end{macrocode}
511 % \end{macro}
512 %
513 % \begin{macro}[internal]{\braid@add@crossing}
514 % This is the macro which adds the crossing to the current list of strands.
515 % The strands are stored as \emph{soft paths} (see the TikZ/PGF documentation).
516 % So this selects the right strands and then extends them according to the crossing type.
517 % \begin{macrocode}
518 \def\braid@add@crossing#1#2{%
519 % \end{macrocode}
520 % Our crossing type, which is \Verb+#2+, is one of \Verb+1+ or \Verb+-1+.
521 % Our strands are \Verb+#1+ and \Verb-#1+1-.
522 % \begin{macrocode}
523 \edef\braid@crossing@type{#2}%
524 \edef\braid@this@strand{#1}%
525 \pgfmathtruncatemacro{\braid@next@strand}{#1+1}
526 % \end{macrocode}
527 % Increment the level counter, if requested.
528 % The controls whether the crossing is on the same level as the previous one or is one level further on.
529 % \begin{macrocode}
530 \ifbraid@increase@level
531 \stepcounter{braid@level}
532 \fi
533 % \end{macrocode}
534 % Default is to request increment so we set it for next time.
535 % \begin{macrocode}
536 \braid@increase@leveltrue
537 % \end{macrocode}
538 % Now we figure out the coordinates of the crossing.
539 % \Verb+(\braid@tx,\braid@ty)+ is the top-left corner (assuming the braid flows down the page).
540 % \Verb+(\braid@nx,\braid@ny)+ is the bottom-right corner (assuming the braid flows down the page).
541 % We start by setting \Verb+(\braid@tx,\braid@ty)+ according to the level and strand number, then shift \Verb+\braid@ty+ by \Verb+\braid@eh+ which is the ``edge height'' (the little extra at the start and end of each strand).
542 % Then from these values, we set \Verb+(\braid@nx,\braid@ny)+ by adding on the appropriate amount.
543 % The heights \Verb+\braid@cy+ and \Verb+\braid@dy+ are for the control points for the strands as they cross.
544 % They're actually the same height, but using two gives us the possibility of changing them independently in a later version of this package.
545 % Lastly, we bring \Verb+\braid@ty+ and \Verb+\braid@ny+ towards each other just a little so that there is ``clear water'' between subsequent crossings (makes it look a bit better if the same strand is used in subsequent crossings).
546 % \begin{macrocode}
547 \braid@tx=\braid@this@strand\braid@width
548 \braid@ty=\value{braid@level}\braid@height
549 \advance\braid@ty by \braid@eh
550 \braid@nx=\braid@tx
551 \braid@ny=\braid@ty
552 \advance\braid@nx by \braid@width
553 \advance\braid@ny by \braid@height
554 \advance\braid@ty by \braid@nf\braid@height
555 \advance\braid@ny by -\braid@nf\braid@height
556 \braid@cy=\braid@ty
557 \braid@dy=\braid@ny
558 \advance\braid@cy by \braid@cf\braid@height
559 \advance\braid@dy by -\braid@cf\braid@height
560 % \end{macrocode}
561 % Now we try to find a starting point for the strand ending here.
562 % We might not have used this strand before, so it might not exist.
563 % \begin{macrocode}
564 \expandafter\let\expandafter\braid@this@path@origin%
565 \csname braid@strand@\braid@this@strand @origin\endcsname
566 % \end{macrocode}
567 % If we haven't seen this strand before, that one will be \Verb+\relax+.
568 % \begin{macrocode}
569 \ifx\braid@this@path@origin\relax
570 % \end{macrocode}
571 % Haven't seen this strand before, so initialise it.
572 % Record the initial position of the strand.
573 % \begin{macrocode}
574 \let\braid@this@path@origin\braid@this@strand
575 % \end{macrocode}
576 % Start a new soft path.
577 % \begin{macrocode}
578 \pgfsyssoftpath@setcurrentpath{\@empty}
579 \pgfpathmoveto{\pgfpoint{\braid@tx}{0pt}}
580 % \end{macrocode}
581 % Save the path as \Verb+\braid@this@path+.
582 % \begin{macrocode}
583 \pgfsyssoftpath@getcurrentpath{\braid@this@path}
584 \else
585 % \end{macrocode}
586 % We have seen this before, so we simply copy the associated path in to \Verb+\braid@this@path+.
587 % \begin{macrocode}
588 \expandafter\let\expandafter\braid@this@path%
589 \csname braid@strand@\braid@this@path@origin\endcsname
590 \fi
591 % \end{macrocode}
592 % Now we do the same again with the other strand in the crossing.
593 % \begin{macrocode}
594 \expandafter\let\expandafter\braid@next@path@origin%
595 \csname braid@strand@\braid@next@strand @origin\endcsname
596 \ifx\braid@next@path@origin\relax
597 \let\braid@next@path@origin\braid@next@strand
598 \pgfsyssoftpath@setcurrentpath{\@empty}
599 \pgfpathmoveto{\pgfpoint{\braid@nx}{0pt}}
600 \pgfsyssoftpath@getcurrentpath{\braid@next@path}
601 \else
602 \expandafter\let\expandafter\braid@next@path%
603 \csname braid@strand@\braid@next@path@origin\endcsname
604 \fi
605 % \end{macrocode}
606 % Now that we have the paths for our two strands, we extend them to the next level.
607 % We start by selecting the first path.
608 % \begin{macrocode}
609 \pgfsyssoftpath@setcurrentpath{\braid@this@path}
610 % \end{macrocode}
611 % Draw a line down to the current level, note that this line is always non-trivial since we shifted the corners of the crossing in a little.
612 % \begin{macrocode}
613 \pgfpathlineto{\pgfqpoint{\braid@tx}{\braid@ty}}
614 % \end{macrocode}
615 % Curve across to the next position.
616 % Depending on the crossing type, we either have a single curve or we have to break it in two.
617 % Our gap is to interrupt at times determined by the gap key.
618 % \begin{macrocode}
619 \pgfmathsetmacro{\braid@gst}{0.5 - \pgfkeysvalueof{/pgf/braid/gap}}%
620 \pgfmathsetmacro{\braid@gend}{0.5 + \pgfkeysvalueof{/pgf/braid/gap}}%
621 \ifx\braid@crossing@type\braid@over@cross
622 % \end{macrocode}
623 % We're on the overpass, so just one curve needed.
624 % \begin{macrocode}
625 \pgfpathcurveto{\pgfqpoint{\braid@tx}{\braid@cy}}%
626 {\pgfqpoint{\braid@nx}{\braid@dy}}%
627 {\pgfqpoint{\braid@nx}{\braid@ny}}
628 \else
629 % \end{macrocode}
630 % We're on the underpass, so we need to interrupt our path to allow the other curve to go past.
631 % \begin{macrocode}
632 \pgfpathcurvebetweentimecontinue{0}{\braid@gst}%
633 {\pgfqpoint{\braid@tx}{\braid@ty}}%
634 {\pgfqpoint{\braid@tx}{\braid@cy}}%
635 {\pgfqpoint{\braid@nx}{\braid@dy}}%
636 {\pgfqpoint{\braid@nx}{\braid@ny}}%
637 \pgfpathcurvebetweentime{\braid@gend}{1}%
638 {\pgfqpoint{\braid@tx}{\braid@ty}}%
639 {\pgfqpoint{\braid@tx}{\braid@cy}}%
640 {\pgfqpoint{\braid@nx}{\braid@dy}}%
641 {\pgfqpoint{\braid@nx}{\braid@ny}}
642 \fi
643 % \end{macrocode}
644 % We're done with this path, so now we save it.
645 % \begin{macrocode}
646 \pgfsyssoftpath@getcurrentpath{\braid@this@path}
647 % \end{macrocode}
648 % Now do the same with the second path.
649 % \begin{macrocode}
650 \pgfsyssoftpath@setcurrentpath{\braid@next@path}
651 \pgfpathlineto{\pgfqpoint{\braid@nx}{\braid@ty}}
652 \ifx\braid@crossing@type\braid@over@cross
653 \pgfpathcurvebetweentimecontinue{0}{\braid@gst}%
654 {\pgfqpoint{\braid@nx}{\braid@ty}}%
655 {\pgfqpoint{\braid@nx}{\braid@cy}}%
656 {\pgfqpoint{\braid@tx}{\braid@dy}}%
657 {\pgfqpoint{\braid@tx}{\braid@ny}}
658 \pgfpathcurvebetweentime{\braid@gend}{1}%
659 {\pgfqpoint{\braid@nx}{\braid@ty}}%
660 {\pgfqpoint{\braid@nx}{\braid@cy}}%
661 {\pgfqpoint{\braid@tx}{\braid@dy}}%
662 {\pgfqpoint{\braid@tx}{\braid@ny}}
663 \else
664 \pgfpathcurveto{\pgfqpoint{\braid@nx}{\braid@cy}}%
665 {\pgfqpoint{\braid@tx}{\braid@dy}}%
666 {\pgfqpoint{\braid@tx}{\braid@ny}}
667 \fi
668 \pgfsyssoftpath@getcurrentpath{\braid@next@path}
669 % \end{macrocode}
670 % Now save the paths to their proper macros again.
671 % \begin{macrocode}
672 \expandafter\let%
673 \csname braid@strand@\braid@this@path@origin \endcsname%
674 \braid@this@path
675 \expandafter\let%
676 \csname braid@strand@\braid@next@path@origin \endcsname%
677 \braid@next@path
678 % \end{macrocode}
679 % Now update the origins
680 % \begin{macrocode}
681 \expandafter\let%
682 \csname braid@strand@\braid@this@strand @origin\endcsname%
683 \braid@next@path@origin
684 \expandafter\let%
685 \csname braid@strand@\braid@next@strand @origin\endcsname%
686 \braid@this@path@origin
687 % \end{macrocode}
688 % increment the strand counter, if necessary
689 % \begin{macrocode}
690 \pgfmathparse{\value{braid@strands} < \braid@next@strand ?
691 "\noexpand\setcounter{braid@strands}{\braid@next@strand}" : ""}
692 \pgfmathresult
693 % \end{macrocode}
694 % And merrily go on our way with the next bit of the braid specification.
695 % \begin{macrocode}
696 \braid@process%
697 }
698 % \end{macrocode}
699 % \end{macro}
700 %
701 % \begin{macro}[internal]{\braid@olabel@strand}
702 % This macro allows us to label a strand just before a crossing.
703 % The first argument is the strand number at that particular crossing and the second is the label.
704 % We also save the current height.
705 % This version takes the strand number as meaning the \emph{original} ordering.
706 % \begin{macrocode}
707 \newcommand{\braid@olabel@strand}[3][]{%
708 \edef\braid@tmp{{\the\value{braid@level}}}%
709 \expandafter\ifx\csname braid@strand@#2@origin\endcsname\relax
710 \g@addto@macro\braid@tmp{{#2}}%
711 \else
712 \edef\braid@tmpa{{\csname braid@strand@#2@origin\endcsname}}%
713 \ge@addto@macro\braid@tmp{\braid@tmpa}%
714 \fi
715 \g@addto@macro\braid@tmp{{#3}{#1}}%
716 \ge@addto@macro{\braid@strand@labels}{\braid@tmp}%
717 \braid@process%
718 }
719 % \end{macrocode}
720 % \end{macro}
721 %
722 % \begin{macro}[internal]{\braid@clabel@strand}
723 % This macro allows us to label a strand just before a crossing.
724 % The first argument is the strand number at that particular crossing and the second is the label.
725 % We also save the current height.
726 % This version takes the strand number as meaning the \emph{current} ordering.
727 % \begin{macrocode}
728 \newcommand{\braid@clabel@strand}[3][]{%
729 \edef\braid@tmp{{\the\value{braid@level}}}%
730 \g@addto@macro\braid@tmp{{#2}{#3}{#1}}%
731 \ge@addto@macro{\braid@strand@labels}{\braid@tmp}%
732 \braid@process%
733 }
734 % \end{macrocode}
735 % \end{macro}
736 %
737 % \begin{macro}[internal]{\braid@floors@trim}
738 % The list of floors, if given, will start with a superfluous comma.
739 % This removes it.
740 % \begin{macrocode}
741 \def\braid@floors@trim,{}
742 % \end{macrocode}
743 % \end{macro}
744 %
745 % \begin{macro}[internal]{\braid@render@floor}
746 % This is the default rendering for floors: it draws a rectangle.
747 % \begin{macrocode}
748 \def\braid@render@floor{%
749 \draw (\floorsx,\floorsy) rectangle (\floorex,\floorey);
750 }
751 % \end{macrocode}
752 % \end{macro}
753 %
754 % \begin{macro}[internal]{\braid@render@strand@labels}
755 % This starts rendering the labels on the strands at the crossings.
756 % \begin{macrocode}
757 \def\braid@render@strand@labels#1{%
758 \def\braid@tmp{#1}%
759 \ifx\braid@tmp\pgfutil@empty
760 \let\braid@next=\pgfutil@gobble
761 \else
762 \let\braid@next=\braid@@render@strand@labels
763 \fi
764 \braid@next{#1}%
765 }
766 % \end{macrocode}
767 % \end{macro}
768 %
769 % \begin{macro}[internal]{\braid@@render@strand@labels}
770 % This is the actual renderer.
771 % \begin{macrocode}
772 \def\braid@@render@strand@labels#1#2#3#4{%
773 \begingroup
774 \pgfscope
775 \let\tikz@options=\pgfutil@empty
776 \let\tikz@mode=\pgfutil@empty
777 \let\tik@transform=\pgfutil@empty
778 \let\tikz@fig@name=\pgfutil@empty
779 \tikzset{/pgf/braid/strand label,#4}%
780 \braid@nx=#2\braid@width
781 \braid@ny=#1\braid@height
782 \advance\braid@ny by \braid@eh
783 \advance\braid@ny by \braid@height
784 \pgftransformshift{\pgfqpoint{\braid@nx}{\braid@ny}}%
785 \tikz@options
786 \setbox\pgfnodeparttextbox=\hbox%
787 \bgroup%
788 \tikzset{every text node part/.try}%
789 \ifx\tikz@textopacity\pgfutil@empty%
790 \else%
791 \pgfsetfillopacity{\tikz@textopacity}%
792 \pgfsetstrokeopacity{\tikz@textopacity}%
793 \fi%
794 \pgfinterruptpicture%
795 \tikz@textfont%
796 \ifx\tikz@text@width\pgfutil@empty%
797 \else%
798 \begingroup%
799 \pgfmathsetlength{\pgf@x}{\tikz@text@width}%
800 \pgfutil@minipage[t]{\pgf@x}\leavevmode\hbox{}%
801 \tikz@text@action%
802 \fi%
803 \tikz@atbegin@node%
804 \bgroup%
805 \aftergroup\unskip%
806 \ifx\tikz@textcolor\pgfutil@empty%
807 \else%
808 \pgfutil@colorlet{.}{\tikz@textcolor}%
809 \fi%
810 \pgfsetcolor{.}%
811 \setbox\tikz@figbox=\box\pgfutil@voidb@x%
812 \tikz@uninstallcommands%
813 \tikz@halign@check%
814 \ignorespaces%
815 #3
816 \egroup
817 \tikz@atend@node%
818 \ifx\tikz@text@width\pgfutil@empty%
819 \else%
820 \pgfutil@endminipage%
821 \endgroup%
822 \fi%
823 \endpgfinterruptpicture%
824 \egroup%
825 \ifx\tikz@text@width\pgfutil@empty%
826 \else%
827 \pgfmathsetlength{\pgf@x}{\tikz@text@width}%
828 \wd\pgfnodeparttextbox=\pgf@x%
829 \fi%
830 \ifx\tikz@text@height\pgfutil@empty%
831 \else%
832 \pgfmathsetlength{\pgf@x}{\tikz@text@height}%
833 \ht\pgfnodeparttextbox=\pgf@x%
834 \fi%
835 \ifx\tikz@text@depth\pgfutil@empty%
836 \else%
837 \pgfmathsetlength{\pgf@x}{\tikz@text@depth}%
838 \dp\pgfnodeparttextbox=\pgf@x%
839 \fi%
840 \pgfmultipartnode{\tikz@shape}{\tikz@anchor}{\tikz@fig@name}{%
841 {\begingroup\tikz@finish}%
842 }%
843 \endpgfscope
844 \endgroup
845 \braid@render@strand@labels%
846 }
847 % \end{macrocode}
848 % \end{macro}
849 %
850 % \begin{macro}[internal]{\braid@render}
851 % This is called at the end of the braid and it renders the braids and floors according to whatever has been built up up to now.
852 % \begin{macrocode}
853 \def\braid@render{
854 % \end{macrocode}
855 % Check for floors since we do them first.
856 % \begin{macrocode}
857 \ifx\braid@floors\@empty
858 \else
859 % \end{macrocode}
860 % Have some floors, start a scope and prepare to render them.
861 % \begin{macrocode}
862 \pgfsys@beginscope
863 % \end{macrocode}
864 % Clear the path (just to be sure).
865 % \begin{macrocode}
866 \pgfsyssoftpath@setcurrentpath{\empty}
867 % \end{macrocode}
868 % Trim the initial comma off the list of floors.
869 % \begin{macrocode}
870 \edef\braid@floors{\expandafter\braid@floors@trim\braid@floors}
871 % \end{macrocode}
872 % Initialise our horizontal coordinates.
873 % \begin{macrocode}
874 \braid@tx=\braid@width
875 \advance\braid@tx by \braid@eh
876 \braid@nx=\value{braid@strands}\braid@width
877 \advance\braid@nx by -\braid@eh
878 % \end{macrocode}
879 % Loop over the list of floors.
880 % \begin{macrocode}
881 \foreach \braid@f in \braid@floors {
882 \pgfsys@beginscope
883 % \end{macrocode}
884 % Figure out the vertical coordinates for the current floor.
885 % \begin{macrocode}
886 \braid@ty=\braid@f\braid@height
887 \advance\braid@ty by \braid@eh
888 \advance\braid@ty by \braid@height
889 \braid@ny=\braid@ty
890 \advance\braid@ny by \braid@height
891 % \end{macrocode}
892 % Save the coordinates for use in the floor rendering macro.
893 % \begin{macrocode}
894 \edef\floorsx{\the\braid@tx}
895 \edef\floorsy{\the\braid@ty}
896 \edef\floorex{\the\braid@nx}
897 \edef\floorey{\the\braid@ny}
898 \let\tikz@options=\pgfutil@empty
899 % \end{macrocode}
900 % Load general floor style options.
901 % \begin{macrocode}
902 \expandafter\tikzset\expandafter{\braid@floors@style}
903 % \end{macrocode}
904 % Load any style options specific to this floor.
905 % We're actually offset by 2 from what the user thinks the floor level is.
906 % \begin{macrocode}
907 \pgfmathtruncatemacro{\braid@ff}{\braid@f+2}
908 % \end{macrocode}
909 % Load the relevant floor style, if it exists.
910 % \begin{macrocode}
911 \expandafter\let\expandafter\braid@floor@style%
912 \csname braid@options@floor@\braid@ff\endcsname
913 \ifx\braid@floor@style\relax
914 \else
915 % \end{macrocode}
916 % There is a floor style for this level, so process it.
917 % \begin{macrocode}
918 \expandafter\tikzset\expandafter{\braid@floor@style}%
919 \fi
920 % \end{macrocode}
921 % The \Verb+\tikzset+ just parses the options, we need to call \Verb+\tikz@options+ to actually set them.
922 % \begin{macrocode}
923 \tikz@options
924 % \end{macrocode}
925 % Now we call the rendering code.
926 % \begin{macrocode}
927 \braid@render@floor
928 % \end{macrocode}
929 % Done!
930 % End the scope for \emph{this} floor and go again.
931 % \begin{macrocode}
932 \pgfsys@endscope
933 }
934 % \end{macrocode}
935 % Done rendering floors, end the scope.
936 % \begin{macrocode}
937 \pgfsys@endscope
938 \fi
939 % \end{macrocode}
940 % Finished with floors (if we had them), now get on with the strands.
941 % \begin{macrocode}
942 \stepcounter{braid@level}
943 \foreach \braid@k in {1,...,\value{braid@strands}} {
944 % \end{macrocode}
945 % Start a local scope to ensure we don't mess with other braids
946 % \begin{macrocode}
947 \pgfsys@beginscope
948 % \end{macrocode}
949 % Default is to draw each braid
950 % \begin{macrocode}
951 \tikz@mode@drawtrue%
952 \let\tikz@mode=\pgfutil@empty
953 \let\tikz@options=\pgfutil@empty
954 % \end{macrocode}
955 % (x,y) coordinates of bottom of strand
956 % \begin{macrocode}
957 \braid@tx=\braid@k\braid@width
958 \braid@ty=\value{braid@level}\braid@height
959 \advance\braid@ty by 2\braid@eh
960 % \end{macrocode}
961 % Try to find the starting point of this strand
962 % \begin{macrocode}
963 \expandafter\let\expandafter\braid@path@origin%
964 \csname braid@strand@\braid@k @origin\endcsname
965 \ifx\braid@path@origin\relax
966 % \end{macrocode}
967 % If that doesn't exist, we'll just draw a straight line
968 % so we move to the top of the current position
969 % \begin{macrocode}
970 \pgfsyssoftpath@setcurrentpath{\@empty}
971 \pgfpathmoveto{\pgfqpoint{\braid@tx}{0pt}}
972 \let\braid@path@origin\braid@k
973 \else
974 % \end{macrocode}
975 % If the path does exist, we load it
976 % \begin{macrocode}
977 \expandafter\let\expandafter\braid@path%
978 \csname braid@strand@\braid@path@origin\endcsname
979 \pgfsyssoftpath@setcurrentpath{\braid@path}
980 \fi
981 % \end{macrocode}
982 % Extend the path to the bottom
983 % \begin{macrocode}
984 \pgflineto{\pgfqpoint{\braid@tx}{\braid@ty}}
985 % \end{macrocode}
986 % Load common style options
987 % \begin{macrocode}
988 \expandafter\tikzset\expandafter{\braid@style}
989 % \end{macrocode}
990 % Load any style options specific to this strand
991 % \begin{macrocode}
992 \expandafter\let\expandafter\braid@style%
993 \csname braid@options@strand@\braid@path@origin\endcsname
994 \ifx\braid@style\relax
995 \else
996 \expandafter\tikzset\expandafter{\braid@style}
997 \fi
998 \braid@options
999 \tikz@mode
1000 \tikz@options
1001 % \end{macrocode}
1002 % This is the command that actually draws the strand.
1003 % \begin{macrocode}
1004 \edef\tikz@temp{\noexpand\pgfusepath{%
1005 \iftikz@mode@draw draw\fi%
1006 }}%
1007 \tikz@temp
1008 % \end{macrocode}
1009 % If our braid has a name, we label the ends of the strand.
1010 % \begin{macrocode}
1011 \ifx\braid@name\pgfutil@empty
1012 \else
1013 % \end{macrocode}
1014 % Label the ends of the strand.
1015 % \begin{macrocode}
1016 \coordinate (\braid@name-\braid@path@origin-e) at (\braid@tx,\braid@ty);
1017 \coordinate (\braid@name-rev-\braid@k-e) at (\braid@tx,\braid@ty);
1018 \braid@nx=\braid@path@origin\braid@width
1019 \coordinate (\braid@name-\braid@path@origin-s) at (\braid@nx,0pt);
1020 \coordinate (\braid@name-rev-\braid@k-s) at (\braid@nx,0pt);
1021 \fi
1022 % \end{macrocode}
1023 % Done with this strand, close the scope and do the next one.
1024 % \begin{macrocode}
1025 \pgfsys@endscope
1026 }
1027 % \end{macrocode}
1028 % If our braid has a name, we also want to label the centre.
1029 % \begin{macrocode}
1030 \ifx\braid@name\pgfutil@empty
1031 \else
1032 \braid@tx=\value{braid@strands}\braid@width
1033 \braid@ty=\value{braid@level}\braid@height
1034 \advance\braid@ty by 2\braid@eh
1035 \advance\braid@tx by \braid@width
1036 \braid@tx=.5\braid@tx
1037 \braid@ty=.5\braid@ty
1038 \coordinate (\braid@name) at (\braid@tx,\braid@ty);
1039 \fi
1040 % \end{macrocode}
1041 % Now we label the strands if needed.
1042 % \begin{macrocode}
1043 \ifx\braid@strand@labels\pgfutil@empty
1044 \else
1045 \expandafter\braid@render@strand@labels\braid@strand@labels{}%
1046 \fi
1047 % \end{macrocode}
1048 % All done now, close the scope and end the group (which was opened right at the start).
1049 % \begin{macrocode}
1050 \pgfsys@endscope
1051 \endgroup}
1052 % \end{macrocode}
1053 % \end{macro}
1054 %
1055 % \begin{macro}[internal]{\braid@start}
1056 % This starts off the braid, initialising a load of stuff.
1057 % We start a PGF scope, set the level to \(-1\), the label, floors, and name to empty, process any options we're given, and save certain lengths for later use..
1058 % \begin{macrocode}
1059 \def\braid@start#1{%
1060 \pgfsys@beginscope
1061 \setcounter{braid@level}{-1}%
1062 \let\braid@label\@empty
1063 \let\braid@strand@labels\@empty
1064 \let\braid@floors\@empty
1065 \let\braid@name\empty
1066 \let\clabel=\braid@clabel@strand
1067 \let\olabel=\braid@olabel@strand
1068 \pgfkeys{/pgf/braid/.cd,#1}%
1069 \ifbraid@strand@labels@origin
1070 \let\label=\braid@olabel@strand
1071 \else
1072 \let\label=\braid@clabel@strand
1073 \fi
1074 \let\braid@options\tikz@options
1075 \tikz@transform
1076 \setcounter{braid@strands}{%
1077 \pgfkeysvalueof{/pgf/braid/number of strands}}%
1078 \braid@width=\pgfkeysvalueof{/pgf/braid/width}%
1079 \braid@height=\pgfkeysvalueof{/pgf/braid/height}%
1080 \braid@eh=\pgfkeysvalueof{/pgf/braid/border height}%
1081 \pgfkeysgetvalue{/pgf/braid/control factor}{\braid@cf}%
1082 \pgfkeysgetvalue{/pgf/braid/nudge factor}{\braid@nf}%
1083 \braid@height=-\braid@height
1084 \braid@eh=-\braid@eh
1085 \braid@increase@leveltrue
1086 \braid@process@start
1087 }
1088 % \end{macrocode}
1089 % \end{macro}
1090 %
1091 % These are the lengths we'll use as we construct the braid
1092 % \begin{macrocode}
1093 \newdimen\braid@width
1094 \newdimen\braid@height
1095 \newdimen\braid@tx
1096 \newdimen\braid@ty
1097 \newdimen\braid@nx
1098 \newdimen\braid@ny
1099 \newdimen\braid@cy
1100 \newdimen\braid@dy
1101 \newdimen\braid@eh
1102 % \end{macrocode}
1103 %
1104 % An if to decide whether or not to step to the next level or not
1105 % \begin{macrocode}
1106 \newif\ifbraid@increase@level
1107 % \end{macrocode}
1108 % An if to decide whether label indices should be absolute or not
1109 % \begin{macrocode}
1110 \newif\ifbraid@strand@labels@origin
1111 % \end{macrocode}
1112 %
1113 %
1114 % Some initial values
1115 % \begin{macrocode}
1116 \let\braid@style\pgfutil@empty
1117 \let\braid@floors@style\pgfutil@empty
1118 \def\braid@over@cross{1}
1119 % \end{macrocode}
1120 %
1121 % Counters to track the strands and the levels.
1122 % \begin{macrocode}
1123 \newcounter{braid@level}
1124 \newcounter{braid@strands}
1125 % \end{macrocode}
1126 %
1127 % All the keys we'll use.
1128 % \begin{macrocode}
1129 \pgfkeys{
1130 % \end{macrocode}
1131 % Handle unknown keys by passing them to \Verb+pgf+ and \Verb+tikz+.
1132 % \begin{macrocode}
1133 /tikz/braid/.search also={/pgf},
1134 /pgf/braid/.search also={/pgf,/tikz},
1135 % \end{macrocode}
1136 % Our ``namespace'' is \Verb+/pgf/braid+.
1137 % \begin{macrocode}
1138 /pgf/braid/.cd,
1139 number of strands/.initial=0,
1140 height/.initial=1cm,
1141 width/.initial=1cm,
1142 gap/.initial=.1,
1143 border height/.initial=.25cm,
1144 control factor/.initial=.5,
1145 nudge factor/.initial=.05,
1146 name/.code={%
1147 \def\braid@name{#1}%
1148 },
1149 at/.code={%
1150 \braid@relocate{#1}%
1151 },
1152 floor command/.code={%
1153 \def\braid@render@floor{#1}%
1154 },
1155 style strands/.code 2 args={%
1156 \def\braid@temp{#2}%
1157 \braidset{style each strand/.list={#1}}%
1158 },
1159 style each strand/.code={%
1160 \expandafter\edef%
1161 \csname braid@options@strand@#1\endcsname{\braid@temp}%
1162 },
1163 style floors/.code 2 args={%
1164 \def\braid@temp{#2}%
1165 \braidset{style each floor/.list={#1}}%
1166 },
1167 style each floor/.code={%
1168 \expandafter\edef%
1169 \csname braid@options@floor@#1\endcsname{\braid@temp}%
1170 },
1171 style all floors/.code={%
1172 \def\braid@floors@style{#1}
1173 },
1174 strand label/.style={},
1175 strand label by origin/.is if=braid@strand@labels@origin,
1176 }
1177 % \end{macrocode}
1178 % \begin{macro}{\braidset}
1179 % Shorthand for setting braid-specific keys.
1180 % \begin{macrocode}
1181 \def\braidset#1{%
1182 \pgfkeys{/pgf/braid/.cd,#1}}
1183 % \end{macrocode}
1184 % \end{macro}
1185 %
1186 % \iffalse
1187 %</package>
1188 % \fi
1189 % \begin{macrocode}
1190 %<*library>
1191 %<@@=braid>
1192 % \end{macrocode}
1193 %
1194 % \section{Reimplementation as a TikZ Library}
1195 %
1196 % Life is so much easier with \LaTeX3.
1197 % \begin{macrocode}
1198 \ProvidesFile{tikzlibrarybraids.code.tex}[%
1199 2024/01/09 v2.3 Tikz/PGF library for drawing braid diagrams%
1200 ]
1201 \RequirePackage{expl3}
1202 \ExplSyntaxOn
1203 % \end{macrocode}
1204 % Define all the variables we'll be using.
1205 % \begin{macrocode}
1206 \tl_new:N \l_@@_tmpa_tl
1207 \tl_new:N \l_@@_tmpb_tl
1208 \tl_new:N \l_@@_tmpc_tl
1209 \tl_new:N \l_@@_tmpd_tl
1210 \tl_new:N \l_@@_anchor_strand_tl
1211 \tl_new:N \l_@@_anchor_level_tl
1212 \fp_new:N \l_@@_height_fp
1213 \fp_new:N \l_@@_width_fp
1214 \fp_new:N \l_@@_nudge_fp
1215 \fp_new:N \l_@@_control_fp
1216 \fp_new:N \l_@@_ctrlax_fp
1217 \fp_new:N \l_@@_ctrlay_fp
1218 \fp_new:N \l_@@_ctrlbx_fp
1219 \fp_new:N \l_@@_ctrlby_fp
1220 \fp_new:N \l_@@_endx_fp
1221 \fp_new:N \l_@@_endy_fp
1222 \fp_new:N \l_@@_anchor_x_fp
1223 \fp_new:N \l_@@_anchor_y_fp
1224 \int_new:N \l_@@_tmpa_int
1225 \int_new:N \l_@@_tmpb_int
1226 \int_new:N \l_@@_length_int
1227 \int_new:N \l_@@_strands_int
1228 \int_new:N \l_@@_crossing_int
1229 \int_new:N \l_@@_crossing_start_int
1230 \int_new:N \l_@@_crossing_end_int
1231 \int_new:N \l_@@_crossing_width_int
1232 \int_new:N \l_@@_crossing_long_int
1233 \int_new:N \l_@@_crossing_start_factor_int
1234 \int_new:N \l_@@_crossing_end_factor_int
1235 \int_new:N \l_@@_anchor_level_int
1236 \int_new:N \l_@@_floor_int
1237 \seq_new:N \l_@@_tmpa_seq
1238 \seq_new:N \l_@@_word_seq
1239 \seq_new:N \l_@@_crossing_seq
1240 \seq_new:N \l_@@_anchor_seq
1241 \seq_new:N \l_@@_floors_seq
1242 \str_new:N \l_@@_tmpa_str
1243 \bool_new:N \l_@@_step_level_bool
1244 \bool_new:N \l_@@_swap_crossing_bool
1245 \bool_new:N \l_@@_default_crossing_bool
1246 \bool_new:N \l_@@_default_symbol_bool
1247 \bool_set_true:N \l_@@_default_crossing_bool
1248 \bool_set_true:N \l_@@_default_symbol_bool
1249 \bool_new:N \l_@@_floor_bool
1250 \bool_new:N \l_@@_height_bool
1251 \bool_new:N \l_@@_crossing_height_bool
1252 \prop_new:N \l_@@_strands_prop
1253 \prop_new:N \l_@@_permutation_prop
1254 \prop_new:N \l_@@_crossing_permutation_prop
1255 \prop_new:N \l_@@_inverse_prop
1256 \prop_new:N \l_@@_anchor_prop
1257 \msg_new:nnn {braids} {height} {The~ keys~ "height"~ and~ "crossing~ height"~ shouldn't~ be~ used~ together~ (the~ setting~ for~ "crossing~ height"~ will~ be~ used)}
1258 \cs_generate_variant:Nn \seq_set_split:Nnn {NVn}
1259 \cs_generate_variant:Nn \str_compare:nNnTF {VNnTF}
1260 % \end{macrocode}
1261 % Our interface is through a TikZ pic.
1262 % \begin{macrocode}
1263 \tikzset{
1264 braid/.pic={
1265 \begin{scope}[every~ braid/.try]
1266 \@@_parse_word:n {#1}
1267 \@@_count:
1268 \@@_render:
1269 \end{scope}
1270 },
1271 floor/.pic={
1272 \path[pic~ actions, draw=none] (0,0) rectangle (1,1);
1273 \path[pic~ actions, fill=none] (0,0) -- (1,0) (0,1) -- (1,1);
1274 },
1275 /tikz/braid/.search~ also={/tikz},
1276 braid/.cd,
1277 % \end{macrocode}
1278 % The various TikZ parameters for the braid.
1279 %
1280 % The anchor determines which part of the braid is located at the position specified by the pic.
1281 % It can be of the form \Verb+n-m+ where \Verb+n+ is a strand number and +m+ is a crossing level.
1282 % The strand number can be either a number or \Verb+rev-n+ to use the ending numbering of the strands.
1283 % The crossing level can also be \Verb+s+ or \Verb+e+ which means the actual start or end of the strand (including the border).
1284 %
1285 % \begin{macrocode}
1286 anchor/.initial=1-s,
1287 % \end{macrocode}
1288 % \Verb+number of strands+ sets a minimum for the number of strands in the braid (otherwise, it is set by the strands used in the specified crossings).
1289 % \begin{macrocode}
1290 number~ of~ strands/.initial=0,
1291 % \end{macrocode}
1292 %
1293 % These keys determine whether crossings are over or under by default.
1294 % \begin{macrocode}
1295 crossing~ convention/.is~choice,
1296 crossing~ convention/over/.code={
1297 \bool_set_true:N \l_@@_default_crossing_bool
1298 },
1299 crossing~ convention/down/.code={
1300 \bool_set_true:N \l_@@_default_crossing_bool
1301 },
1302 crossing~ convention/under/.code={
1303 \bool_set_false:N \l_@@_default_crossing_bool
1304 },
1305 crossing~ convention/up/.code={
1306 \bool_set_false:N \l_@@_default_crossing_bool
1307 },
1308 crossing~ convention/.default=over,
1309 % \end{macrocode}
1310 %
1311 % \begin{macrocode}
1312 flip~ crossing~ convention/.code={
1313 \bool_set_inverse:N \l_@@_default_crossing_bool
1314 },
1315 % \end{macrocode}
1316 %
1317 % These keys determine whether elements should be inverted by default.
1318 % \begin{macrocode}
1319 set~ symbols/.is~choice,
1320 set~ symbols/over/.code={
1321 \bool_set_true:N \l_@@_default_symbol_bool
1322 },
1323 set~ symbols/down/.code={
1324 \bool_set_true:N \l_@@_default_symbol_bool
1325 },
1326 set~ symbols/under/.code={
1327 \bool_set_false:N \l_@@_default_symbol_bool
1328 },
1329 set~ symbols/up/.code={
1330 \bool_set_false:N \l_@@_default_symbol_bool
1331 },
1332 set~ symbols/.default=over,
1333 % \end{macrocode}
1334 %
1335 % \begin{macrocode}
1336 flip~ symbols/.code={
1337 \bool_set_inverse:N \l_@@_default_symbol_bool
1338 },
1339 % \end{macrocode}
1340 %
1341 % The next two keys are used to control the separation between the crossings.
1342 % The original braid package used the \Verb+height+ as part of how it determined the direction of the braid on the page.
1343 % In particular, the \Verb+height+ could be negative, and indeed the default is for it to be so since a vertical braid usually flows from top to bottom.
1344 % Now that the drawing is reimplemented as a \Verb+pic+ then the direction is better controlled using transformations.
1345 % So then \Verb+height+ should be simply to set the gap between crossings in whatever orientation.
1346 % In particular, \Verb+height+ should be a positive length.
1347 %
1348 % To avoid backwards incompatibility, the original (counter-intuitive) version of \Verb+height+ is retained but a new key, \Verb+crossing height+, is introduced which can only be positive (rather, the code will take its absolute value so you can \emph{declare} it to be negative but the code will laugh at you and ignore the sign).
1349 %
1350 % To ensure that \Verb+crossing height+ wins, we use a boolean to see if it has been invoked by the user.
1351 % \begin{macrocode}
1352 height/.initial=-1cm,
1353 height/.code={
1354 \bool_if:NTF \l_@@_crossing_height_bool
1355 {
1356 \msg_term:nn {braids} {height}
1357 }
1358 {
1359 \pgfkeyssetvalue{/tikz/braid/height}{#1}
1360 \bool_set_true:N \l_@@_height_bool
1361 }
1362 },
1363 crossing~ height/.code={
1364 \bool_if:NT \l_@@_height_bool
1365 {
1366 \msg_term:nn {braids} {height}
1367 }
1368 \exp_args:Nnx \pgfkeyssetvalue {/tikz/braid/height}
1369 {\dim_eval:n {-\dim_abs:n{#1}}}
1370 \bool_set_true:N \l_@@_crossing_height_bool
1371 },
1372 % \end{macrocode}
1373 % \Verb+width+ is the distance between strands (can be negative).
1374 % \begin{macrocode}
1375 width/.initial=1cm,
1376 % \end{macrocode}
1377 % \Verb+gap+ is for determining the gap in the under-strand of a crossing.
1378 % \begin{macrocode}
1379 gap/.initial=.05,
1380 % \end{macrocode}
1381 % \Verb+border height+ is a length added at the start and end of each strand.
1382 % \begin{macrocode}
1383 border~ height/.initial=.25cm,
1384 % \end{macrocode}
1385 % \Verb+floor border+ is added to the width of any floors
1386 % \begin{macrocode}
1387 floor~ border/.initial=.25cm,
1388 % \end{macrocode}
1389 % \Verb+floors+ is a list of floors to draw, specified as a cslist of coordinates as (x,y,w,h,a) in which the units are numbers of strands and crossing levels.
1390 % The parameters are: coordinates of lower left corner, width, height, (optional) name for styling.
1391 % \begin{macrocode}
1392 add~ floor/.code={
1393 \seq_push:Nn \l_@@_floors_seq {#1}
1394 },
1395 % \end{macrocode}
1396 % \Verb+control factor+ determines the proportion of the \Verb+height+ used for the control points.
1397 % \begin{macrocode}
1398 control~ factor/.initial=.5,
1399 % \end{macrocode}
1400 % \Verb+nudge factor+ is used to compress each crossing slightly within its rectangle.
1401 % \begin{macrocode}
1402 nudge~ factor/.initial=.05
1403 }
1404 % \end{macrocode}
1405 %
1406 % \begin{macro}[internal]{\@@_parse_word:Nn}
1407 % Parse the braid word as a token list and convert it into a sequence.
1408 % \begin{macrocode}
1409 \cs_new_nopar:Npn \@@_parse_word:n #1
1410 {
1411 \seq_clear:N \l_@@_word_seq
1412 \tl_clear:N \l_@@_tmpa_tl
1413 \tl_set:Nn \l_@@_tmpb_tl {#1}
1414
1415 \bool_until_do:nn { \tl_if_empty_p:N \l_@@_tmpb_tl }
1416 {
1417 % \end{macrocode}
1418 % We step through the braid specification, looking for special characters.
1419 % To avoid catcode issues, the comparison is as strings.
1420 % Some actions may involve consuming more tokens from the list so we can't do a simple \Verb+map_inline+ but have to keep stripping off the head token.
1421 %
1422 % The idea is to store information about the current crossing in a token list (noting that it may be specified in a variety of orders) and then when we're sure we have all the information we add it to our sequence of crossings.
1423 % \begin{macrocode}
1424 \str_set:Nx \l_@@_tmpa_str {\tl_head:N \l_@@_tmpb_tl}
1425 \tl_set:Nx \l_@@_tmpb_tl {\tl_tail:N \l_@@_tmpb_tl}
1426 % \end{macrocode}
1427 %
1428 % \begin{macrocode}
1429 \str_case_e:nnTF {\l_@@_tmpa_str}
1430 {
1431 % \end{macrocode}
1432 % Underscore introduces the crossing numbers
1433 % \begin{macrocode}
1434 {_}
1435 {
1436 \tl_put_right:Nx \l_@@_tmpa_tl
1437 {
1438 \exp_not:N \@@_parse_index:n {\tl_head:N \l_@@_tmpb_tl}
1439 }
1440 \tl_set:Nx \l_@@_tmpb_tl {\tl_tail:N \l_@@_tmpb_tl}
1441 }
1442 % \end{macrocode}
1443 % Power is used to indicate inverse.
1444 % \begin{macrocode}
1445 {^}
1446 {
1447 \tl_put_left:Nx \l_@@_tmpa_tl
1448 {
1449 \exp_not:N \@@_parse_exponent:n {\tl_head:N \l_@@_tmpb_tl}
1450 }
1451 \tl_set:Nx \l_@@_tmpb_tl {\tl_tail:N \l_@@_tmpb_tl}
1452 }
1453 % \end{macrocode}
1454 % Bar is for floors.
1455 % \begin{macrocode}
1456 {|}
1457 {
1458 \tl_if_empty:NF \l_@@_tmpa_tl
1459 {
1460 \seq_put_right:NV \l_@@_word_seq \l_@@_tmpa_tl
1461 \tl_clear:N \l_@@_tmpa_tl
1462 }
1463
1464 \tl_set:Nn \l_@@_tmpa_tl {
1465 \bool_set_false:N \l_@@_step_level_bool
1466 \bool_set_true:N \l_@@_floor_bool
1467 }
1468 \seq_put_right:NV \l_@@_word_seq \l_@@_tmpa_tl
1469 \tl_clear:N \l_@@_tmpa_tl
1470 }
1471 % \end{macrocode}
1472 % Hyphen says the next crossing is on the same level as the current one.
1473 % \begin{macrocode}
1474 {-}
1475 {
1476 \tl_put_right:Nn \l_@@_tmpa_tl
1477 {
1478 \bool_set_false:N \l_@@_step_level_bool
1479 }
1480 }
1481 % \end{macrocode}
1482 % \(1\) is for the identity (i.e., no crossing but still have a level).
1483 % We put a nop token on the list so that it is no longer empty.
1484 % \begin{macrocode}
1485 {1}
1486 {
1487 \tl_if_empty:NF \l_@@_tmpa_tl
1488 {
1489 \seq_put_right:NV \l_@@_word_seq \l_@@_tmpa_tl
1490 \tl_clear:N \l_@@_tmpa_tl
1491 }
1492 \tl_put_right:Nn \l_@@_tmpa_tl {\@@_do_identity:}
1493 }
1494 % \end{macrocode}
1495 % Ignore spaces.
1496 % \begin{macrocode}
1497 {~}
1498 {
1499 }
1500 }
1501 {
1502 }
1503 {
1504 % \end{macrocode}
1505 % If we get an unrecognised token, it's our trigger to start accumulating information for the next crossing.
1506 % \begin{macrocode}
1507 \tl_if_empty:NF \l_@@_tmpa_tl
1508 {
1509 \seq_put_right:NV \l_@@_word_seq \l_@@_tmpa_tl
1510 \tl_clear:N \l_@@_tmpa_tl
1511 }
1512 }
1513 }
1514 % \end{macrocode}
1515 % At the end, we also put our current token list on the word sequence.
1516 % \begin{macrocode}
1517 \tl_if_empty:NF \l_@@_tmpa_tl
1518 {
1519 \seq_put_right:NV \l_@@_word_seq \l_@@_tmpa_tl
1520 \tl_clear:N \l_@@_tmpa_tl
1521 }
1522 }
1523 % \end{macrocode}
1524 % \end{macro}
1525 %
1526 % \begin{macro}{\@@_parse_index:n}
1527 % Parse an index, saving it in a sequence with the two indices such that the first goes over the second.
1528 % \begin{macrocode}
1529 \cs_new_nopar:Npn \@@_parse_index:n #1
1530 {
1531 \seq_clear:N \l_@@_crossing_seq
1532 \clist_map_inline:nn {#1}
1533 {
1534 \tl_if_in:nnTF {##1} {-}
1535 {
1536 \seq_set_split:Nnn \l_@@_tmpa_seq {-} {##1}
1537 \int_compare:nTF {\seq_item:Nn \l_@@_tmpa_seq {1} < \seq_item:Nn \l_@@_tmpa_seq {2} }
1538 {
1539 \int_set:Nn \l_@@_tmpa_int {1}
1540 }
1541 {
1542 \int_set:Nn \l_@@_tmpa_int {-1}
1543 }
1544 \int_step_inline:nnnn {\seq_item:Nn \l_@@_tmpa_seq {1}} {\l_@@_tmpa_int} {\seq_item:Nn \l_@@_tmpa_seq {2}}
1545 {
1546 \seq_put_right:Nn \l_@@_crossing_seq {####1}
1547 }
1548 }
1549 {
1550 \seq_put_right:Nn \l_@@_crossing_seq {##1}
1551 }
1552 }
1553 \int_compare:nT {\seq_count:N \l_@@_crossing_seq == 1}
1554 {
1555 \seq_put_right:Nx \l_@@_crossing_seq {\int_eval:n {#1 + 1} }
1556 }
1557 \bool_xor:nnT {\l_@@_swap_crossing_bool} {\l_@@_default_symbol_bool}
1558 {
1559 \seq_reverse:N \l_@@_crossing_seq
1560 }
1561 }
1562 % \end{macrocode}
1563 % \end{macro}
1564 %
1565 % \begin{macro}{\@@_parse_exponent:n}
1566 % Parse an exponent, basically testing to see if it is \(-1\) in which case our crossing numbers should be reversed.
1567 % \begin{macrocode}
1568 \cs_new_nopar:Npn \@@_parse_exponent:n #1
1569 {
1570 \int_compare:nTF {#1 == -1}
1571 {
1572 \bool_set_true:N \l_@@_swap_crossing_bool
1573 }
1574 {
1575 \bool_set_false:N \l_@@_swap_crossing_bool
1576 }
1577 }
1578 % \end{macrocode}
1579 % \end{macro}
1580 %
1581 % \begin{macro}{\@@_do_identity:}
1582 % \begin{macrocode}
1583 \cs_new_nopar:Npn \@@_do_identity:
1584 {
1585 }
1586 % \end{macrocode}
1587 % \end{macro}
1588 %
1589 % \begin{macro}{\@@_count:NNN}
1590 % Work out how big the braid is by counting strands and levels.
1591 % We also figure out the permutation from the start to end of the strands.
1592 % This is useful for labelling various parts of the braid.
1593 % \begin{macrocode}
1594 \cs_new_nopar:Npn \@@_count:
1595 {
1596 \int_zero:N \l_@@_length_int
1597 \int_set:Nn \l_@@_strands_int {\@@_value:n {number~of~strands}}
1598 \prop_clear:N \l_@@_permutation_prop
1599 \prop_clear:N \l_@@_crossing_permutation_prop
1600 \prop_clear:N \l_@@_anchor_prop
1601 \prop_clear:N \l_@@_inverse_prop
1602
1603 \seq_map_inline:Nn \l_@@_word_seq
1604 {
1605 % \end{macrocode}
1606 % Clear the crossing sequence and assume we're going to step the level.
1607 % \begin{macrocode}
1608 \seq_clear:N \l_@@_crossing_seq
1609 \bool_set_true:N \l_@@_step_level_bool
1610 \bool_set_false:N \l_@@_swap_crossing_bool
1611 % \end{macrocode}
1612 % Run the details of this crossing.
1613 % \begin{macrocode}
1614 ##1
1615 % \end{macrocode}
1616 % If we're increasing the level (no hyphen), do so.
1617 % \begin{macrocode}
1618 \bool_if:NT \l_@@_step_level_bool
1619 {
1620 \int_incr:N \l_@@_length_int
1621 }
1622 % \end{macrocode}
1623 % If we have a crossing, check we have enough strands to cover it.
1624 % \begin{macrocode}
1625 \seq_if_empty:NF \l_@@_crossing_seq
1626 {
1627 \seq_map_inline:Nn \l_@@_crossing_seq
1628 {
1629 \int_set:Nn \l_@@_strands_int
1630 {
1631 \int_max:nn {\l_@@_strands_int} {####1}
1632 }
1633 }
1634 }
1635 }
1636 % \end{macrocode}
1637 % Now that we know how many strands we have, we can initialise our permutation props.
1638 % One will hold the overall permutation, the other will keep track of our current permutation.
1639 % \begin{macrocode}
1640 \int_step_inline:nnnn {1} {1} {\l_@@_strands_int}
1641 {
1642 \prop_put:Nnn \l_@@_permutation_prop {##1} {##1}
1643 \prop_put:Nnn \l_@@_anchor_prop {##1} {##1}
1644 \prop_put:Nnn \l_@@_crossing_permutation_prop {##1} {##1}
1645 }
1646 % \end{macrocode}
1647 % Now we step through the braid word again and record the permutations so that we can calculate the overall permutation defined by the braid.
1648 %
1649 % We will also figure out our shift from the anchor, so first we need to get some information about the anchor.
1650 %
1651 % If the anchor specification has a hyphen then it is of the form strand-level, otherwise it is an anchor as if the whole braid were contained in a rectangular node.
1652 % \begin{macrocode}
1653 \tl_set:Nx \l_@@_tmpa_tl {\@@_value:n {anchor}}
1654 \tl_if_in:NnTF \l_@@_tmpa_tl {-}
1655 {
1656 \seq_set_split:NnV \l_@@_anchor_seq {-} \l_@@_tmpa_tl
1657
1658 \tl_set:Nx \l_@@_tmpa_tl {\seq_item:Nn \l_@@_anchor_seq {1}}
1659 \tl_if_eq:VnTF \l_@@_tmpa_tl {rev}
1660 {
1661 \tl_set:Nx \l_@@_anchor_strand_tl {\seq_item:Nn \l_@@_anchor_seq {2}}
1662 \tl_set:Nx \l_@@_anchor_level_tl {\seq_item:Nn \l_@@_anchor_seq {3}}
1663 }
1664 {
1665 \tl_set:Nx \l_@@_anchor_strand_tl {\seq_item:Nn \l_@@_anchor_seq {1}}
1666 \tl_set:Nx \l_@@_anchor_level_tl {\seq_item:Nn \l_@@_anchor_seq {2}}
1667 }
1668 % \end{macrocode}
1669 % The important information is as to the level at which the requested anchor resides.
1670 % If it is at the end or start of a strand, we set the level to \(-1\) so that it never matches a level number.
1671 % \begin{macrocode}
1672 \tl_if_eq:VnTF \l_@@_anchor_level_tl {s}
1673 {
1674 \int_set:Nn \l_@@_anchor_level_int {-1}
1675 }
1676 {
1677 \tl_if_eq:VnTF \l_@@_anchor_level_tl {e}
1678 {
1679 \int_set:Nn \l_@@_anchor_level_int {-1}
1680 }
1681 {
1682 \int_set:Nn \l_@@_anchor_level_int
1683 {\tl_use:N \l_@@_anchor_level_tl}
1684 }
1685 }
1686 }
1687 {
1688 % \end{macrocode}
1689 % There wasn't a hyphen in the anchor specification, so assume it's an anchor on a node surrounding the entire braid.
1690 % For now, set the anchor strand and level to \(-1\).
1691 % \begin{macrocode}
1692 \int_set:Nn \l_@@_anchor_level_int {-1}
1693 \tl_set:Nn \l_@@_anchor_strand_tl {-1}
1694 }
1695
1696 \int_zero:N \l_@@_crossing_int
1697 \int_incr:N \l_@@_crossing_int
1698 % \end{macrocode}
1699 %
1700 % \begin{macrocode}
1701 \seq_map_inline:Nn \l_@@_word_seq
1702 {
1703 \bool_set_true:N \l_@@_step_level_bool
1704 \seq_clear:N \l_@@_crossing_seq
1705 \bool_set_false:N \l_@@_swap_crossing_bool
1706 ##1
1707 \seq_if_empty:NF \l_@@_crossing_seq
1708 {
1709 \int_step_inline:nnn {2} {\seq_count:N \l_@@_crossing_seq}
1710 {
1711 \int_set:Nn \l_@@_tmpa_int {####1}
1712 \int_set:Nn \l_@@_tmpb_int {####1 - 1}
1713
1714 \prop_get:NxN \l_@@_permutation_prop
1715 {
1716 \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}
1717 } \l_@@_tmpa_tl
1718 \prop_get:NxN \l_@@_permutation_prop
1719 {
1720 \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int}
1721 } \l_@@_tmpb_tl
1722
1723 \prop_put:NxV \l_@@_permutation_prop
1724 {
1725 \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int}
1726 } \l_@@_tmpa_tl
1727 \prop_put:NxV \l_@@_permutation_prop
1728 {
1729 \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}
1730 } \l_@@_tmpb_tl
1731 }
1732 }
1733 % \end{macrocode}
1734 % See if the current level is what was requested by the anchor.
1735 % \begin{macrocode}
1736 \int_compare:nT {\l_@@_crossing_int = \l_@@_anchor_level_int}
1737 {
1738 \prop_set_eq:NN \l_@@_anchor_prop \l_@@_permutation_prop
1739 }
1740 \bool_if:NT \l_@@_step_level_bool
1741 {
1742 \int_incr:N \l_@@_crossing_int
1743 }
1744 }
1745 % \end{macrocode}
1746 % This inverts the anchor permutation.
1747 % \begin{macrocode}
1748 \int_step_inline:nnnn {1} {1} {\l_@@_strands_int}
1749 {
1750 \prop_get:NnN \l_@@_anchor_prop {##1} \l_@@_tmpa_tl
1751 \prop_put:NVn \l_@@_inverse_prop \l_@@_tmpa_tl {##1}
1752 }
1753 \prop_set_eq:NN \l_@@_anchor_prop \l_@@_inverse_prop
1754 % \end{macrocode}
1755 % This inverts the full permutation.
1756 % \begin{macrocode}
1757 \int_step_inline:nnnn {1} {1} {\l_@@_strands_int}
1758 {
1759 \prop_get:NnN \l_@@_permutation_prop {##1} \l_@@_tmpa_tl
1760 \prop_put:NVn \l_@@_inverse_prop \l_@@_tmpa_tl {##1}
1761 }
1762 % \end{macrocode}
1763 % Now that we have the inverse, we can figure out our anchor.
1764 % If the strand was recorded as \(-1\), then we want to figure out the position from the braid as a whole so we don't bother with processing.
1765 % \begin{macrocode}
1766 \tl_if_eq:VnF \l_@@_anchor_strand_tl {-1}
1767 {
1768 % \end{macrocode}
1769 % Now, see if we requested a strand by its position at the end of the braid.
1770 % \begin{macrocode}
1771 \tl_set:Nx \l_@@_tmpa_tl {\seq_item:Nn \l_@@_anchor_seq {1}}
1772 \tl_if_eq:VnT \l_@@_tmpa_tl {rev}
1773 {
1774 \prop_get:NVN \l_@@_permutation_prop
1775 \l_@@_anchor_strand_tl \l_@@_anchor_strand_tl
1776 }
1777 \tl_if_eq:VnF \l_@@_anchor_level_tl {s}
1778 {
1779 \tl_if_eq:VnTF \l_@@_anchor_level_tl {e}
1780 {
1781 \prop_get:NVN \l_@@_inverse_prop
1782 \l_@@_anchor_strand_tl \l_@@_anchor_strand_tl
1783 }
1784 {
1785 \prop_get:NVN \l_@@_anchor_prop
1786 \l_@@_anchor_strand_tl \l_@@_anchor_strand_tl
1787 }
1788 }
1789 }
1790 % \end{macrocode}
1791 % \begin{macrocode}
1792 }
1793 % \end{macrocode}
1794 %
1795 %
1796 % \end{macro}
1797 %
1798 %
1799 % \begin{macro}{\@@_dim_value:n, \@@_value:n}
1800 % Extract a length or a value from a PGF key.
1801 % \begin{macrocode}
1802 \cs_new_nopar:Npn \@@_dim_value:n #1
1803 {
1804 \dim_to_fp:n {\pgfkeysvalueof{/tikz/braid/#1}}
1805 }
1806 \cs_new_nopar:Npn \@@_value:n #1
1807 {
1808 \pgfkeysvalueof{/tikz/braid/#1}
1809 }
1810 % \end{macrocode}
1811 % \end{macro}
1812 %
1813 % \begin{macro}{\@@_render:}
1814 % This is the macro that converts the braid word into TikZ paths.
1815 % \begin{macrocode}
1816 \cs_generate_variant:Nn \prop_get:NnN {NxN}
1817 \cs_generate_variant:Nn \prop_put:Nnn {NxV}
1818 \cs_generate_variant:Nn \tl_if_eq:nnTF {VnTF}
1819 \cs_generate_variant:Nn \tl_if_eq:nnF {VnF}
1820 \cs_generate_variant:Nn \tl_if_eq:nnT {VnT}
1821
1822 \cs_new_nopar:Npn \@@_render:
1823 {
1824 % \end{macrocode}
1825 % Start by figuring out our anchor.
1826 % \begin{macrocode}
1827 \tl_if_eq:VnTF \l_@@_anchor_strand_tl {-1}
1828 {
1829 % \end{macrocode}
1830 % The strand is \(-1\) then we're working with the braid as if a node.
1831 % We'll redefine this node later anyway.
1832 % \begin{macrocode}
1833 \tl_set:cn {pgf@sh@ns@temporary braid node}{rectangle}
1834 \tl_set:cx {pgf@sh@np@temporary braid node}{%
1835 \exp_not:N\def
1836 \exp_not:N\southwest
1837 {
1838 \exp_not:N\pgfqpoint
1839 {0pt}
1840 {0pt}
1841 }
1842 \exp_not:N\def
1843 \exp_not:N\northeast
1844 {
1845 \exp_not:N\pgfqpoint
1846 {
1847 \fp_to_dim:n
1848 {
1849 (\l_@@_strands_int - 1)
1850 *
1851 abs(\@@_dim_value:n {width})
1852 }
1853 }
1854 {
1855 \fp_to_dim:n
1856 {
1857 \l_@@_length_int * abs(\@@_dim_value:n {height})
1858 + 2 * \@@_dim_value:n {border~ height}
1859 }
1860 }
1861 }
1862 }%
1863 \pgfgettransform\l_@@_tmpa_tl
1864 \tl_set:cV {pgf@sh@nt@temporary braid node} \l_@@_tmpa_tl
1865 \tl_set:cV {pgf@sh@pi@temporary braid node} \pgfpictureid
1866 \pgfpointanchor{temporary braid node} {\@@_value:n {anchor}}
1867 % \end{macrocode}
1868 % Adjustments due to the possibility of negative widths/heights
1869 % \begin{macrocode}
1870 \fp_set:Nn \l_@@_anchor_x_fp {
1871 - \dim_use:c {pgf@x}
1872 - (1 - sign(\@@_dim_value:n {width})) / 2
1873 * (\l_@@_strands_int - 1)
1874 * \@@_dim_value:n {width}
1875 }
1876 \fp_set:Nn \l_@@_anchor_y_fp {
1877 - \dim_use:c {pgf@y}
1878 - (1 - sign(\@@_dim_value:n {height})) / 2
1879 * (
1880 \l_@@_length_int * abs(\@@_dim_value:n {height})
1881 + 2 * \@@_dim_value:n {border~ height}
1882 ) * sign(\@@_dim_value:n {height})
1883 }
1884 }
1885 {
1886 % \end{macrocode}
1887 % The strand is not \(-1\) so we're setting the anchor via strand and level numbers.
1888 % \begin{macrocode}
1889 \fp_set:Nn \l_@@_anchor_x_fp { - 1 * (\tl_use:N \l_@@_anchor_strand_tl - 1) * \@@_dim_value:n {width} }
1890
1891 \tl_if_eq:VnTF \l_@@_anchor_level_tl {s}
1892 {
1893 \fp_set:Nn \l_@@_anchor_y_fp {0}
1894 }
1895 {
1896 \tl_if_eq:VnTF \l_@@_anchor_level_tl {e}
1897 {
1898 \fp_set:Nn \l_@@_anchor_y_fp {
1899 -1 * \l_@@_length_int * \@@_dim_value:n {height}
1900 - sign(\@@_dim_value:n {height})
1901 * 2 * \@@_dim_value:n {border~ height}
1902 }
1903 }
1904 {
1905 \fp_set:Nn \l_@@_anchor_y_fp {
1906 -1 * \l_@@_anchor_level_tl * \@@_dim_value:n {height}
1907 - sign(\@@_dim_value:n {height})
1908 * \@@_dim_value:n {border~ height}
1909 }
1910 }
1911 }
1912 }
1913
1914 \begin{scope}[
1915 shift={
1916 (\fp_to_decimal:N \l_@@_anchor_x_fp pt,
1917 \fp_to_decimal:N \l_@@_anchor_y_fp pt
1918 )
1919 }
1920 ]
1921 % \end{macrocode}
1922 % Initialise a prop for the individual strands.
1923 % \begin{macrocode}
1924 \prop_clear:N \l_@@_strands_prop
1925 % \end{macrocode}
1926 % Initialise some lengths.
1927 % \begin{macrocode}
1928 \fp_zero:N \l_@@_height_fp
1929 \fp_zero:N \l_@@_nudge_fp
1930 \fp_zero:N \l_@@_control_fp
1931 % \end{macrocode}
1932 % This holds our current \Verb+height+ of our strands.
1933 % \begin{macrocode}
1934 \fp_set:Nn \l_@@_height_fp
1935 {
1936 sign(\@@_dim_value:n {height})
1937 * \@@_dim_value:n {border~ height}
1938 }
1939 % \end{macrocode}
1940 % This holds the total \Verb+width+ of our strands.
1941 % \begin{macrocode}
1942 \fp_set:Nn \l_@@_width_fp
1943 {
1944 (\l_@@_strands_int - 1) * \@@_dim_value:n {width}
1945 + 2 * sign(\@@_dim_value:n{width})
1946 * \@@_dim_value:n {floor~ border}
1947 }
1948 % \end{macrocode}
1949 % Each crossing actually starts a little bit into the crossing space, as defined by the \Verb+nudge factor+.
1950 % \begin{macrocode}
1951 \fp_set:Nn \l_@@_nudge_fp
1952 {
1953 \@@_value:n {nudge~ factor} * \@@_dim_value:n {height}
1954 }
1955 % \end{macrocode}
1956 % This sets where the control points for the crossing curves will be.
1957 % \begin{macrocode}
1958 \fp_set:Nn \l_@@_control_fp
1959 {
1960 \@@_value:n {control~ factor} * \@@_dim_value:n {height}
1961 }
1962 \fp_sub:Nn \l_@@_control_fp {\l_@@_nudge_fp}
1963 % \end{macrocode}
1964 % Initialise our strand paths with a \Verb+\draw+.
1965 % \begin{macrocode}
1966 \int_step_inline:nnnn {1} {1} {\l_@@_strands_int}
1967 {
1968 \prop_get:NnN \l_@@_inverse_prop {##1} \l_@@_tmpa_tl
1969 \prop_put:Nnx \l_@@_strands_prop {##1}
1970 {
1971 \exp_not:N \draw[
1972 braid/every~ strand/.try,
1973 braid/strand~ ##1/.try
1974 ]
1975 \exp_not:N \@@_moveto:nn {
1976 \fp_eval:n {(##1 - 1) * \@@_dim_value:n {width} }
1977 } {0}
1978 \exp_not:N \@@_lineto:nn {
1979 \fp_eval:n {(##1 - 1) * \@@_dim_value:n {width} }
1980 } { \fp_to_decimal:N \l_@@_height_fp}
1981 }
1982 % \end{macrocode}
1983 % Add a load of coordinates at the start of each strand, indexed by both forward and backward strand numbers.
1984 % \begin{macrocode}
1985 \@@_coordinate:xxxx {-##1-s} {-rev-\l_@@_tmpa_tl-s}
1986 {\fp_eval:n {(##1 - 1) * \@@_dim_value:n {width} }} {0}
1987
1988 \@@_coordinate:xxxx {-##1-0} {-rev-\l_@@_tmpa_tl-0}
1989 {\fp_eval:n {(##1 - 1) * \@@_dim_value:n {width} }}
1990 { \fp_to_decimal:N \l_@@_height_fp}
1991 }
1992 % \end{macrocode}
1993 %
1994 % Run through any extra floors requested.
1995 % \begin{macrocode}
1996 \seq_map_inline:Nn \l_@@_floors_seq
1997 {
1998 \tl_set:Nx \l_@@_tmpa_tl {\clist_item:nn {##1} {5}}
1999 \@@_do_floor:Vxxxx \l_@@_tmpa_tl
2000 {\fp_eval:n
2001 {
2002 -1*sign(\@@_dim_value:n{width})
2003 * \@@_dim_value:n {floor~ border}
2004 + (\@@_dim_value:n {width}) * (\clist_item:nn {##1} {1} - 1)
2005 }
2006 pt
2007 }
2008 {\fp_eval:n
2009 {
2010 \l_@@_height_fp + ( \@@_dim_value:n {height} ) * (\clist_item:nn {##1} {2})
2011 }
2012 pt
2013 }
2014 {\fp_eval:n {
2015 ( (\clist_item:nn {##1} {3}) * \@@_dim_value:n {width}
2016 + 2 * sign(\@@_dim_value:n{width})
2017 * \@@_dim_value:n {floor~ border} ) / \dim_to_fp:n {1cm}
2018 }
2019 }
2020 {\fp_eval:n {
2021 (\clist_item:nn {##1} {4}) * ( \@@_dim_value:n {height} ) / \dim_to_fp:n {1cm}
2022 }
2023 }
2024 }
2025 % \end{macrocode}
2026 %
2027 % Keep track of the crossing level for the floor.
2028 % \begin{macrocode}
2029 \int_zero:N \l_@@_crossing_int
2030 \int_incr:N \l_@@_crossing_int
2031
2032 \seq_map_inline:Nn \l_@@_word_seq
2033 {
2034 % \end{macrocode}
2035 % Clear the flags for this segment of the braid word
2036 % \begin{macrocode}
2037 \seq_clear:N \l_@@_crossing_seq
2038 \bool_set_true:N \l_@@_step_level_bool
2039 \bool_set_false:N \l_@@_floor_bool
2040 \bool_set_false:N \l_@@_swap_crossing_bool
2041 ##1
2042 % \end{macrocode}
2043 % If we're drawing a floor, do so straightaway.
2044 % \begin{macrocode}
2045 \bool_if:NT \l_@@_floor_bool
2046 {
2047 \@@_do_floor:Vxxxx \l_@@_crossing_int
2048 {\fp_eval:n
2049 {
2050 -1*sign(\@@_dim_value:n{width})
2051 * \@@_dim_value:n {floor~ border}
2052 }
2053 pt
2054 }
2055 {\fp_to_decimal:N \l_@@_height_fp pt}
2056 {\fp_eval:n { \l_@@_width_fp / \dim_to_fp:n {1cm} }}
2057 {\fp_eval:n { ( \@@_dim_value:n {height} ) / \dim_to_fp:n {1cm}}}
2058 }
2059 % \end{macrocode}
2060 % If we have a crossing, process it.
2061 % \begin{macrocode}
2062 \seq_if_empty:NF \l_@@_crossing_seq
2063 {
2064 \int_set:Nn \l_@@_crossing_long_int
2065 {
2066 % \seq_item:Nn \l_@@_crossing_seq {\seq_count:N \l_@@_crossing_seq}
2067 \seq_item:Nn \l_@@_crossing_seq {1}
2068 }
2069 \int_set:Nn \l_@@_crossing_start_int
2070 {
2071 \int_min:nn
2072 {
2073 \seq_item:Nn \l_@@_crossing_seq {1}
2074 }
2075 {
2076 \seq_item:Nn \l_@@_crossing_seq {\seq_count:N \l_@@_crossing_seq}
2077 }
2078 }
2079 \int_set:Nn \l_@@_crossing_end_int
2080 {
2081 \int_max:nn
2082 {
2083 \seq_item:Nn \l_@@_crossing_seq {1}
2084 }
2085 {
2086 \seq_item:Nn \l_@@_crossing_seq {\seq_count:N \l_@@_crossing_seq}
2087 }
2088
2089 }
2090 \int_set:Nn \l_@@_crossing_width_int
2091 {
2092 \l_@@_crossing_end_int
2093 -
2094 \l_@@_crossing_start_int
2095 }
2096 % \end{macrocode}
2097 % Step through the crossing
2098 % \begin{macrocode}
2099 \int_step_inline:nnn {2} {\seq_count:N \l_@@_crossing_seq}
2100 {
2101 \bool_if:NTF \l_@@_default_crossing_bool
2102 {
2103 \int_set:Nn \l_@@_tmpa_int {####1}
2104 \int_set:Nn \l_@@_tmpb_int {####1 - 1}
2105 }
2106 {
2107 \int_set:Nn \l_@@_tmpa_int {####1 - 1}
2108 \int_set:Nn \l_@@_tmpb_int {####1}
2109 }
2110 % \end{macrocode}
2111 % Keep track of the current permutation.
2112 % \begin{macrocode}
2113 \prop_get:NxN \l_@@_crossing_permutation_prop
2114 {\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}} \l_@@_tmpa_tl
2115 \prop_get:NxN \l_@@_crossing_permutation_prop
2116 {\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int}} \l_@@_tmpb_tl
2117
2118 \prop_put:NxV \l_@@_crossing_permutation_prop
2119 {\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int}} \l_@@_tmpa_tl
2120 \prop_put:NxV \l_@@_crossing_permutation_prop
2121 {\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}} \l_@@_tmpb_tl
2122 % \end{macrocode}
2123 % Now get the strands corresponding to the ones involved in the crossing.
2124 % \begin{macrocode}
2125 \prop_get:NxN \l_@@_strands_prop
2126 {\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}} \l_@@_tmpa_tl
2127 \prop_get:NxN \l_@@_strands_prop
2128 {\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int}} \l_@@_tmpb_tl
2129 % \end{macrocode}
2130 % The over-strand is easy as that's a single curve.
2131 % \begin{macrocode}
2132 % \int_set:Nn \l_@@_crossing_start_factor_int {1}
2133 % \int_set:Nn \l_@@_crossing_end_factor_int {1}
2134 % \int_compare:nT {
2135 % \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}
2136 % =
2137 % \l_@@_crossing_long_int
2138 % }
2139 % {
2140 \int_set:Nn \l_@@_crossing_start_factor_int {0}
2141 \int_set:Nn \l_@@_crossing_end_factor_int {0}
2142
2143 \int_compare:nT {
2144 ####1 = \seq_count:N \l_@@_crossing_seq
2145 }
2146 {
2147 \int_set:Nn \l_@@_crossing_end_factor_int {1}
2148 }
2149 \int_compare:nT {
2150 ####1 = 2
2151 }
2152 {
2153 \int_set:Nn \l_@@_crossing_start_factor_int {1}
2154 }
2155 % }
2156
2157 \tl_put_right:Nx \l_@@_tmpa_tl
2158 {
2159 \exp_not:N \@@_lineto:nn
2160
2161 {\fp_eval:n
2162 {
2163 (\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int} - 1)
2164 * \@@_dim_value:n {width}
2165 }
2166 }
2167 {\fp_eval:n { \l_@@_height_fp + \l_@@_nudge_fp * \l_@@_crossing_start_factor_int
2168 + \@@_dim_value:n {height} * (####1 - 2)/(\seq_count:N \l_@@_crossing_seq - 1)
2169 } }
2170
2171 \exp_not:N \@@_curveto:nnnnnn
2172
2173 {0}
2174 {\fp_eval:n { \l_@@_control_fp
2175 % * \l_@@_crossing_start_factor_int
2176 * 1/(\seq_count:N \l_@@_crossing_seq - 1)}}
2177
2178 {0}
2179 {\fp_eval:n {- \l_@@_control_fp
2180 % * \l_@@_crossing_end_factor_int
2181 * 1/(\seq_count:N \l_@@_crossing_seq - 1)}}
2182
2183 {\fp_eval:n
2184 {
2185 (\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int} - 1)
2186 * \@@_dim_value:n {width}
2187 }
2188 }
2189 {\fp_eval:n
2190 {
2191 \l_@@_height_fp
2192 + \@@_dim_value:n {height} * (####1 - 1)/(\seq_count:N \l_@@_crossing_seq - 1)
2193 - \l_@@_nudge_fp * \l_@@_crossing_end_factor_int
2194 }
2195 }
2196 }
2197 % \end{macrocode}
2198 % The under-strand is a bit more complicated as we need to break it in the middle.
2199 % \begin{macrocode}
2200 % \int_set:Nn \l_@@_crossing_start_factor_int {1}
2201 % \int_set:Nn \l_@@_crossing_end_factor_int {1}
2202 % \int_compare:nT {
2203 % \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int}
2204 % =
2205 % \l_@@_crossing_long_int
2206 % }
2207 % {
2208 \int_set:Nn \l_@@_crossing_start_factor_int {0}
2209 \int_set:Nn \l_@@_crossing_end_factor_int {0}
2210
2211 \int_compare:nT {
2212 ####1 = \seq_count:N \l_@@_crossing_seq
2213 }
2214 {
2215 \int_set:Nn \l_@@_crossing_end_factor_int {1}
2216 }
2217 \int_compare:nT {
2218 ####1 = 2
2219 }
2220 {
2221 \int_set:Nn \l_@@_crossing_start_factor_int {1}
2222 }
2223 % }
2224
2225 \tl_put_right:Nx \l_@@_tmpb_tl
2226 {
2227 \exp_not:N \@@_lineto:nn
2228
2229 {\fp_eval:n
2230 {
2231 (\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int} - 1)
2232 * \@@_dim_value:n {width}
2233 }
2234 }
2235 {\fp_eval:n { \l_@@_height_fp + \l_@@_nudge_fp * \l_@@_crossing_start_factor_int
2236 + \@@_dim_value:n {height} * (####1 - 2)/(\seq_count:N \l_@@_crossing_seq - 1)
2237
2238 } }
2239
2240 \exp_not:N \@@_curveto:nnnnnn
2241
2242 {0}
2243 {
2244 \fp_eval:n {
2245 \l_@@_control_fp * (.5 - \@@_value:n {gap} * (\seq_count:N \l_@@_crossing_seq - 1) ) * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2246 % * \l_@@_crossing_start_factor_int
2247 }
2248 }
2249
2250 {
2251 \fp_eval:n {
2252 - (.5 - \@@_value:n {gap} * (\seq_count:N \l_@@_crossing_seq - 1) ) / 3 *
2253 \@@_bezier_tangent:nnnnn
2254 {.5 - \@@_value:n {gap} * (\seq_count:N \l_@@_crossing_seq - 1) }
2255 {0}
2256 {0}
2257 {
2258 (\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}
2259 - \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int})
2260 * \@@_dim_value:n {width}
2261 }
2262 {
2263 (\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}
2264 - \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int})
2265 * \@@_dim_value:n {width}
2266 }
2267 }
2268 }
2269 {
2270 \fp_eval:n {
2271 -(.5 - \@@_value:n {gap} * (\seq_count:N \l_@@_crossing_seq - 1) ) / 3 *
2272 \@@_bezier_tangent:nnnnn
2273 {.5 - \@@_value:n {gap} * (\seq_count:N \l_@@_crossing_seq - 1) }
2274 {0}
2275 {
2276 \l_@@_control_fp * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2277 % * \l_@@_crossing_start_factor_int
2278 }
2279 {
2280 \@@_dim_value:n {height} * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2281 - \l_@@_nudge_fp * \l_@@_crossing_start_factor_int
2282 - \l_@@_nudge_fp * \l_@@_crossing_end_factor_int
2283 - \l_@@_control_fp * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2284 % * \l_@@_crossing_end_factor_int
2285 }
2286 {
2287 \@@_dim_value:n {height} * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2288 - \l_@@_nudge_fp * \l_@@_crossing_start_factor_int
2289 - \l_@@_nudge_fp * \l_@@_crossing_end_factor_int
2290 }
2291 }
2292 }
2293
2294 {
2295 \fp_eval:n {
2296 (\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int} - 1)
2297 * \@@_dim_value:n {width} +
2298 \@@_bezier_point:nnnnn
2299 {.5 - \@@_value:n {gap} * (\seq_count:N \l_@@_crossing_seq - 1) }
2300 {0}
2301 {0}
2302 {
2303 (\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}
2304 - \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int})
2305 * \@@_dim_value:n {width}
2306 }
2307 {
2308 (\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}
2309 - \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int})
2310 * \@@_dim_value:n {width}
2311 }
2312 }
2313 }
2314 {
2315 \fp_eval:n {
2316 \l_@@_height_fp
2317 + \l_@@_nudge_fp * \l_@@_crossing_start_factor_int
2318 + \@@_dim_value:n {height} * (####1 - 2)/(\seq_count:N \l_@@_crossing_seq - 1)
2319 +
2320 \@@_bezier_point:nnnnn
2321 {.5 - \@@_value:n {gap} * (\seq_count:N \l_@@_crossing_seq - 1) }
2322 {0}
2323 {
2324 \l_@@_control_fp * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2325 % * \l_@@_crossing_start_factor_int
2326 }
2327 {
2328 \@@_dim_value:n {height} * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2329 - \l_@@_nudge_fp * \l_@@_crossing_start_factor_int
2330 - \l_@@_nudge_fp * \l_@@_crossing_end_factor_int
2331 - \l_@@_control_fp * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2332 % * \l_@@_crossing_end_factor_int
2333 }
2334 {\@@_dim_value:n {height} * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2335 - \l_@@_nudge_fp * \l_@@_crossing_start_factor_int
2336 - \l_@@_nudge_fp * \l_@@_crossing_end_factor_int
2337 }
2338 }
2339 }
2340
2341 \exp_not:N \@@_moveto:nn
2342 {
2343 \fp_eval:n {
2344 (\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int} - 1)
2345 * \@@_dim_value:n {width} +
2346 \@@_bezier_point:nnnnn
2347 {.5 + \@@_value:n {gap} * (\seq_count:N \l_@@_crossing_seq - 1) }
2348 {0}
2349 {0}
2350 {
2351 (\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}
2352 - \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int})
2353 * \@@_dim_value:n {width}
2354 }
2355 {
2356 (\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}
2357 - \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int})
2358 * \@@_dim_value:n {width}
2359 }
2360 }
2361 }
2362 {
2363 \fp_eval:n {
2364 \l_@@_height_fp
2365 + \l_@@_nudge_fp * \l_@@_crossing_start_factor_int
2366 + \@@_dim_value:n {height} * (####1 - 2)/(\seq_count:N \l_@@_crossing_seq - 1)
2367 +
2368 \@@_bezier_point:nnnnn
2369 {.5 + \@@_value:n {gap} * (\seq_count:N \l_@@_crossing_seq - 1) }
2370 {0}
2371 {
2372 \l_@@_control_fp * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2373 % * \l_@@_crossing_start_factor_int
2374 }
2375 {
2376 \@@_dim_value:n {height} * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2377 - \l_@@_nudge_fp * \l_@@_crossing_start_factor_int
2378 - \l_@@_nudge_fp * \l_@@_crossing_end_factor_int
2379 - \l_@@_control_fp * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2380 % * \l_@@_crossing_end_factor_int
2381 }
2382 {\@@_dim_value:n {height} * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2383 - \l_@@_nudge_fp * \l_@@_crossing_start_factor_int
2384 - \l_@@_nudge_fp * \l_@@_crossing_end_factor_int
2385 }
2386 }
2387 }
2388
2389 \exp_not:N \@@_curveto:nnnnnn
2390
2391 {
2392 \fp_eval:n {
2393 (.5 - \@@_value:n {gap} * (\seq_count:N \l_@@_crossing_seq - 1) ) / 3 *
2394 \@@_bezier_tangent:nnnnn
2395 {.5 + \@@_value:n {gap} * (\seq_count:N \l_@@_crossing_seq - 1) }
2396 {0}
2397 {0}
2398 {
2399 (\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}
2400 - \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int})
2401 * \@@_dim_value:n {width}
2402 }
2403 {
2404 (\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}
2405 - \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int})
2406 * \@@_dim_value:n {width}
2407 }
2408 }
2409 }
2410 {
2411 \fp_eval:n {
2412 (.5 - \@@_value:n {gap} * (\seq_count:N \l_@@_crossing_seq - 1) ) / 3 *
2413 \@@_bezier_tangent:nnnnn
2414 {.5 + \@@_value:n {gap} * (\seq_count:N \l_@@_crossing_seq - 1) }
2415 {0}
2416 {
2417 \l_@@_control_fp * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2418 % * \l_@@_crossing_start_factor_int
2419 }
2420 {
2421 \@@_dim_value:n {height} * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2422 - \l_@@_nudge_fp * \l_@@_crossing_start_factor_int
2423 - \l_@@_nudge_fp * \l_@@_crossing_end_factor_int
2424 - \l_@@_control_fp * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2425 % * \l_@@_crossing_end_factor_int
2426 }
2427 {\@@_dim_value:n {height} * 1/(\seq_count:N \l_@@_crossing_seq - 1)
2428 - \l_@@_nudge_fp * \l_@@_crossing_start_factor_int
2429 - \l_@@_nudge_fp * \l_@@_crossing_end_factor_int
2430 }
2431 }
2432 }
2433
2434 {0}
2435 {\fp_eval:n {
2436 - \l_@@_control_fp * (.5 - \@@_value:n {gap} * (\seq_count:N \l_@@_crossing_seq - 1) )
2437 % * \l_@@_crossing_end_factor_int
2438 * 1/(\seq_count:N \l_@@_crossing_seq - 1)}
2439 }
2440
2441 {\fp_eval:n
2442 {
2443 (\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int} - 1)
2444 * \@@_dim_value:n {width}
2445 }
2446 }
2447 {\fp_eval:n
2448 {
2449 \l_@@_height_fp
2450 + \@@_dim_value:n {height} * (####1 - 1)/(\seq_count:N \l_@@_crossing_seq - 1)
2451 - \l_@@_nudge_fp * \l_@@_crossing_end_factor_int
2452 }
2453 }
2454
2455 }
2456 % \end{macrocode}
2457 % Now put those new strands back in the prop.
2458 % \begin{macrocode}
2459 \prop_put:NxV \l_@@_strands_prop
2460 {\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int}} \l_@@_tmpa_tl
2461 \prop_put:NxV \l_@@_strands_prop
2462 {\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}} \l_@@_tmpb_tl
2463 % \end{macrocode}
2464 % If the strands are more than one apart, the intermediate strands need to be broken as well.
2465 % \begin{macrocode}
2466 \int_compare:nT
2467 {
2468 \int_max:nn
2469 {
2470 \seq_item:Nn \l_@@_crossing_seq {####1 - 1}
2471 }
2472 {
2473 \seq_item:Nn \l_@@_crossing_seq {####1}
2474 }
2475 -
2476 \int_min:nn
2477 {
2478 \seq_item:Nn \l_@@_crossing_seq {####1 - 1}
2479 }
2480 {
2481 \seq_item:Nn \l_@@_crossing_seq {####1}
2482 }
2483 > 1
2484 }
2485 {
2486 \int_step_inline:nnnn
2487 {
2488 \int_min:nn
2489 {
2490 \seq_item:Nn \l_@@_crossing_seq {####1 - 1}
2491 }
2492 {
2493 \seq_item:Nn \l_@@_crossing_seq {####1}
2494 }
2495 + 1}
2496 {1}
2497 {
2498 \int_max:nn
2499 {
2500 \seq_item:Nn \l_@@_crossing_seq {####1 - 1}
2501 }
2502 {
2503 \seq_item:Nn \l_@@_crossing_seq {####1}
2504 }
2505 - 1
2506 }
2507 {
2508
2509 \prop_get:NnN \l_@@_strands_prop {########1} \l_@@_tmpa_tl
2510 \tl_put_right:Nx \l_@@_tmpa_tl
2511 {
2512 \exp_not:N \@@_lineto:nn
2513 {\fp_eval:n {(########1 - 1) * \@@_dim_value:n {width} }}
2514 {\fp_eval:n
2515 {
2516 \l_@@_height_fp + \l_@@_nudge_fp
2517 + .5 * \l_@@_control_fp / (\seq_count:N \l_@@_crossing_seq - 1)
2518 + \@@_dim_value:n {height} * (####1 - 2)/(\seq_count:N \l_@@_crossing_seq - 1)
2519 }
2520 }
2521 \exp_not:N \@@_moveto:nn
2522 {\fp_eval:n {(########1 - 1) * \@@_dim_value:n {width} }}
2523 {\fp_eval:n
2524 {
2525 \l_@@_height_fp
2526 - \l_@@_nudge_fp - .5 * \l_@@_control_fp / (\seq_count:N \l_@@_crossing_seq - 1)
2527 + \@@_dim_value:n {height} * (####1 - 1)/(\seq_count:N \l_@@_crossing_seq - 1)
2528 }
2529 }
2530 }
2531
2532 \prop_put:NnV \l_@@_strands_prop {########1} \l_@@_tmpa_tl
2533 }
2534 }
2535 % \end{macrocode}
2536 % Reset the current long
2537 % \begin{macrocode}
2538 \int_compare:nTF
2539 {
2540 \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}
2541 =
2542 \l_@@_crossing_long_int
2543 }
2544 {
2545 \int_set:Nn \l_@@_crossing_long_int {\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int}}
2546 }
2547 {
2548 \int_compare:nT
2549 {
2550 \seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpb_int}
2551 =
2552 \l_@@_crossing_long_int
2553 }
2554 {
2555 \int_set:Nn \l_@@_crossing_long_int {\seq_item:Nn \l_@@_crossing_seq {\l_@@_tmpa_int}}
2556 }
2557
2558 }
2559 % \end{macrocode}
2560 % \begin{macrocode}
2561 }
2562 }
2563 % \end{macrocode}
2564 % If we're to step the level, increase the height and add a load of coordinates.
2565 % \begin{macrocode}
2566 \bool_if:NT \l_@@_step_level_bool
2567 {
2568 \fp_add:Nn \l_@@_height_fp { \@@_dim_value:n {height} }
2569
2570 \int_step_inline:nnnn {1} {1} {\l_@@_strands_int}
2571 {
2572 \prop_get:NnN \l_@@_crossing_permutation_prop
2573 {####1} \l_@@_tmpb_tl
2574 \prop_get:NVN \l_@@_inverse_prop
2575 \l_@@_tmpb_tl \l_@@_tmpa_tl
2576
2577 \@@_coordinate:xxxx
2578 {-\l_@@_tmpb_tl-\int_use:N \l_@@_crossing_int}
2579 {-rev-\l_@@_tmpa_tl-\int_use:N \l_@@_crossing_int }
2580 {\fp_eval:n { (####1 - 1) * \@@_dim_value:n {width} }}
2581 {\fp_to_decimal:N \l_@@_height_fp}
2582 }
2583
2584 \int_incr:N \l_@@_crossing_int
2585 }
2586 }
2587
2588 \fp_add:Nn \l_@@_height_fp
2589 {
2590 sign(\@@_dim_value:n {height})
2591 * \@@_dim_value:n {border~ height}
2592 }
2593 % \end{macrocode}
2594 % Add a little bit to the end of each strand, together with some coordinates.
2595 % \begin{macrocode}
2596 \int_step_inline:nnnn {1} {1} {\l_@@_strands_int}
2597 {
2598 \prop_get:NxN \l_@@_strands_prop {##1} \l_@@_tmpa_tl
2599 \prop_get:NxN \l_@@_permutation_prop {##1} \l_@@_tmpb_tl
2600
2601 \tl_put_right:Nx \l_@@_tmpa_tl {
2602 \exp_not:N \@@_lineto:nn
2603 {\fp_eval:n { (##1 - 1) * \@@_dim_value:n {width} }}
2604 {\fp_to_decimal:N \l_@@_height_fp}
2605 coordinate (-rev-##1-e)
2606 coordinate (-\l_@@_tmpb_tl-e)
2607 ;
2608 }
2609
2610 \prop_put:NnV \l_@@_strands_prop {##1} \l_@@_tmpa_tl
2611 }
2612 % \end{macrocode}
2613 % This is where we actually carry out the drawing commands.
2614 % \begin{macrocode}
2615 \int_step_inline:nnnn {1} {1} {\l_@@_strands_int}
2616 {
2617 \prop_get:NnN \l_@@_strands_prop {##1} \l_@@_tmpa_tl
2618 \tl_use:N \l_@@_tmpa_tl
2619 }
2620 % \end{macrocode}
2621 % Finally, put a node around the whole braid if it's been named
2622 % \begin{macrocode}
2623 \tl_if_empty:cF {tikz@fig@name}
2624 {
2625 \tl_gset:cn {pgf@sh@ns@ \tl_use:c{tikz@fig@name} }{rectangle}
2626 \tl_gset:cx {pgf@sh@np@ \tl_use:c{tikz@fig@name} }{%
2627 \exp_not:N\def
2628 \exp_not:N\southwest
2629 {
2630 \exp_not:N\pgfqpoint
2631 {
2632 \fp_to_dim:n
2633 {
2634 min(0,
2635 (\l_@@_strands_int - 1)
2636 *
2637 (\@@_dim_value:n {width})
2638 )
2639 }
2640 }
2641 {
2642 \fp_to_dim:n
2643 {
2644 min(0,
2645 \l_@@_length_int * (\@@_dim_value:n {height})
2646 + 2 * sign(\@@_dim_value:n {height}) *
2647 \@@_dim_value:n {border~ height}
2648 )
2649 }
2650 }
2651 }
2652 \exp_not:N\def
2653 \exp_not:N\northeast
2654 {
2655 \exp_not:N\pgfqpoint
2656 {
2657 \fp_to_dim:n
2658 {
2659 max(0,
2660 (\l_@@_strands_int - 1)
2661 *
2662 (\@@_dim_value:n {width})
2663 )
2664 }
2665 }
2666 {
2667 \fp_to_dim:n
2668 {
2669 max(0,
2670 \l_@@_length_int * (\@@_dim_value:n {height})
2671 + 2 * sign(\@@_dim_value:n {height}) *
2672 \@@_dim_value:n {border~ height}
2673 )
2674 }
2675 }
2676 }
2677 }%
2678 \pgfgettransform\l_@@_tmpa_tl
2679 \tl_gset:cV {pgf@sh@nt@ \tl_use:c{tikz@fig@name} } \l_@@_tmpa_tl
2680 \tl_gset:cV {pgf@sh@pi@ \tl_use:c{tikz@fig@name} } \pgfpictureid
2681 }
2682 \end{scope}
2683 }
2684 % \end{macrocode}
2685 % \end{macro}
2686 %
2687 % \begin{macro}{\@@_moveto:nn, \@@_lineto:nn, \@@_curveto:nnnnnn, \@@_coordinate:nnnn}
2688 % These are our interfaces to the TikZ code.
2689 % \begin{macrocode}
2690 \cs_new_nopar:Npn \@@_moveto:nn #1#2
2691 {
2692 (#1 pt, #2 pt)
2693 }
2694 \cs_new_nopar:Npn \@@_lineto:nn #1#2
2695 {
2696 -- (#1 pt, #2 pt)
2697 }
2698 \cs_new_nopar:Npn \@@_curveto:nnnnnn #1#2#3#4#5#6
2699 {
2700 % -- +(5 pt, 0) -- +(0 pt, 0pt)
2701 % -- +(#1 pt, #2 pt) -- (#5 pt + #3 pt, #6 pt + #4 pt) -- (#5 pt, #6 pt)
2702 .. controls +(#1 pt, #2 pt) and +(#3 pt, #4 pt)
2703 .. (#5 pt, #6 pt)
2704 }
2705 \cs_new_nopar:Npn \@@_coordinate:nnnn #1#2#3#4
2706 {
2707 \coordinate[alias=#2] (#1) at (#3 pt,#4 pt);
2708 }
2709 \cs_generate_variant:Nn \@@_coordinate:nnnn {xxxx}
2710 % \end{macrocode}
2711 % \end{macro}
2712 %
2713 % \begin{macro}[internal]{\@@_bezier_point:nnnnn, \@@_bezier_tangent:nnnnn}
2714 % Used to calculate intermediate points and tangents on a bezier curve.
2715 % \begin{macrocode}
2716 \cs_new_nopar:Npn \@@_bezier_point:nnnnn #1#2#3#4#5
2717 {
2718 \fp_eval:n
2719 {
2720 (1 - (#1)) * (1 - (#1)) * (1 - (#1)) * (#2)
2721 +
2722 3 * (1 - (#1)) * (1 - (#1)) * (#1) * (#3)
2723 +
2724 3 * (1 - (#1)) * (#1) * (#1) * (#4)
2725 +
2726 (#1) * (#1) * (#1) * (#5)
2727 }
2728 }
2729 \cs_new_nopar:Npn \@@_bezier_tangent:nnnnn #1#2#3#4#5
2730 {
2731 \fp_eval:n
2732 {
2733 3 * (1 - (#1)) * (1 - (#1)) * (#3 - (#2))
2734 +
2735 6 * (1 - (#1)) * (#1) * (#4 - (#3))
2736 +
2737 3 * (#1) * (#1) * (#5 - (#4))
2738 }
2739 }
2740 \cs_new_nopar:Npn \@@_do_floor:nnnnn #1#2#3#4#5
2741 {
2742 \pic[pic~ type=floor,
2743 xscale=#4,
2744 yscale=#5,
2745 at={(#2,#3)},
2746 braid/every~ floor/.try,
2747 braid/floor~#1/.try,
2748 ];
2749 }
2750 \cs_generate_variant:Nn \@@_do_floor:nnnnn {Vxxxx}
2751 % \end{macrocode}
2752 % \end{macro}
2753 % \begin{macrocode}
2754 \ExplSyntaxOff
2755 % \end{macrocode}
2756 %
2757 % \iffalse
2758 %</library>
2759 % \fi
2760 %
2761 % \Finale
2762
2763 \endinput