1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
|
% \iffalse meta-comment
%<*internal>
\iffalse
%</internal>
%<*readme>
----------------------------------------------------------------
braids --- a style file for drawing braid diagrams with TikZ/PGF
E-mail: stacey@math.ntnu.no
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------
This package defines some commands for drawing braid diagrams with TikZ/PGF.
It was designed and tested with PGF2.10.
The initial idea of this package came from a question and answer on the site http://tex.stackexchange.com.
%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
\expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
----------------------------------------------------------------
braids --- a style file for drawing braid diagrams with TikZ/PGF
E-mail: stacey@math.ntnu.no
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------
\endpreamble
\postamble
Copyright (C) 2011 by Andrew Stacey <stacey@math.ntnu.no>
This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License (LPPL), either
version 1.3c of this license or (at your option) any later
version. The latest version of this license is in the file:
http://www.latex-project.org/lppl.txt
This work is "maintained" (as per LPPL maintenance status) by
Andrew Stacey.
This work consists of the files braids.dtx
braids_doc.tex
and the derived files README.txt,
braids.ins,
braids.pdf,
braids.sty,
braids_doc.pdf.
\endpostamble
\usedir{tex/latex/braids}
\generate{
\file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/braids}
\generate{
\file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\usedir{doc/latex/demopkg}
\generate{
\file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
\expandafter\endbatchfile
\else
\expandafter\endgroup
\fi
%</internal>
%<*package>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{braids}[2011/05/07 v1.0 Tikz/PGF commands for drawing braid diagrams]
%</package>
%<*driver>
\documentclass{ltxdoc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
%\usepackage{morefloats}
\usepackage{tikz}
\usepackage{\jobname}
\usepackage[numbered]{hypdoc}
\definecolor{lstbgcolor}{rgb}{0.9,0.9,0.9}
\usepackage{listings}
\lstloadlanguages{[LaTeX]TeX}
\lstset{breakatwhitespace=true,breaklines=true,language=TeX}
\usepackage{fancyvrb}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{815}
%
% \CharacterTable
% {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
% 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
% Digits \0\1\2\3\4\5\6\7\8\9
% Exclamation \! Double quote \" Hash (number) \#
% Dollar \$ Percent \% Ampersand \&
% Acute accent \' Left paren \( Right paren \)
% Asterisk \* Plus \+ Comma \,
% Minus \- Point \. Solidus \/
% Colon \: Semicolon \; Less than \<
% Equals \= Greater than \> Question mark \?
% Commercial at \@ Left bracket \[ Backslash \\
% Right bracket \] Circumflex \^ Underscore \_
% Grave accent \` Left brace \{ Vertical bar \|
% Right brace \} Tilde \~}
%
%
% \changes{1.0}{2011/05/03}{Converted to DTX file}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \providecommand*{\url}{\texttt}
% \GetFileInfo{braids.sty}
% \title{The \textsf{braids} package: codebase}
% \author{Andrew Stacey \\ \url{stacey@math.ntnu.no}}
% \date{\fileversion~from \filedate}
%
% \maketitle
%
% \begin{center}
% \begin{tikzpicture}
% \braid[line width=3pt,line cap=round,style strands={1}{blue},number of strands=7] s_1 s_2 s_5^{-1};
% \end{tikzpicture}
% \end{center}
%
% \section{Introduction}
%
% This is a package for drawing braid diagrams using PGF/TikZ.
% Its inspiration was a question and answer on the website \url{http://tex.stackexchange.com}.
%
% \StopEventually{}
%
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
%
% Test the version of PGF to see if it's what we expect.
% If not, issue a warning (but continue anyway; after all, it \emph{might just work}).
% \begin{macrocode}
\def\braid@pgfversion{2.10}%
\ifx\pgfversion\braid@pgfversion
\else
\PackageWarning{braids}{This package was designed using PGF2.10; you are using \pgfversion.}%
\fi
% \end{macrocode}
% \begin{macro}{\ge@addto@macro}
% This is an expanded version of \Verb+\g@addto@macro+.
% Namely, it adds the \emph{expansion} of the second argument to the first.
% \begin{macrocode}
\long\def\ge@addto@macro#1#2{%
\begingroup
\toks@\expandafter\expandafter\expandafter{\expandafter#1#2}%
\xdef#1{\the\toks@}%
\endgroup}
% \end{macrocode}
% \end{macro}
% \begin{macro}{\braid}
% This is the user command.
% We start a group to ensure that all our assignments are local, and then call our initialisation code.
% The optional argument is for any keys to be set.
% \begin{macrocode}
\newcommand{\braid}[1][]{%
\begingroup
\braid@start{#1}}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@process}
% This is the token swallower.
% 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.
% (Incidentally, the code here is heavily influenced by TikZ.
% That's probably not very surprising.)
% \begin{macrocode}
\def\braid@process{%
\afterassignment\braid@handle\let\braid@token=%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@process@start}
% 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.
% Specifically, we test for the \Verb+at+ and \Verb+(name)+ possibilities.
% \begin{macrocode}
\def\braid@process@start{%
\afterassignment\braid@handle@start\let\braid@token=%
}
% \end{macrocode}
% \end{macro}
% \begin{macro}{\braid@handle@start}
% This is the handler in use at the start.
% It looks for the tokens \Verb+a+ or \Verb+(+ which (might) signal the start of an \Verb+at (coordinate)+ or \Verb+(name)+.
% 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.
% \begin{macrocode}
\def\braid@handle@start{%
\let\braid@next=\braid@handle
\ifx\braid@token a
% \end{macrocode}
% We got an \Verb+a+ so we might have an \Verb+at (coordinate)+
% \begin{macrocode}
\let\braid@next=\braid@maybe@locate
\else
\ifx\braid@token(%)
% \end{macrocode}
% We got an \Verb+(+ so we have a name
% \begin{macrocode}
\iffalse)\fi %Indentation hack!
\let\braid@next=\braid@assign@name
\else
\ifx\braid@token\@sptoken
% \end{macrocode}
% Space; boring, redo from start
% \begin{macrocode}
\let\braid@next=\braid@process@start
\fi
\fi
\fi
\braid@next%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@handle}
% This is the main handler for parsing the braid word.
% It decides what action to take depending on what the token is.
% We have to be a bit careful with catcodes, some packages set
% \Verb+;+ and \Verb+|+ to be active.
% We should probably also be careful with \Verb+^+ and \Verb+_+.
% \begin{macrocode}
\let\braid@semicolon=;
\let\braid@bar=|
\def\braid@handle{%
\let\braid@next=\braid@process
% \end{macrocode}
% Start by checking our catcodes to see what we should check against
% \begin{macrocode}
\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
% \end{macrocode}
% Semicolon, means that we're done reading our braid.
% It's time to render it.
% \begin{macrocode}
\let\braid@next=\braid@render
\else
\ifx\braid@token^
% \end{macrocode}
% Superscript character, the next token tells us whether it's an over-crossing or an under-crossing.
% \begin{macrocode}
\let\braid@next=\braid@sup
\else
\ifx\braid@token_
% \end{macrocode}
% Subscript character, the next token tells us which strands cross.
% \begin{macrocode}
\let\braid@next=\braid@sub
\else
\ifx\braid@token-
% \end{macrocode}
% Hyphen, this is so that we can have more than one crossing on the same level.
% \begin{macrocode}
\braid@increase@levelfalse
\else
\ifx\braid@token[
% \end{macrocode}
% Open bracket, this means we have some more options to process.
% \begin{macrocode}
\let\braid@next=\braid@process@options
\else
\ifx\braid@token\braid@bar
% \end{macrocode}
% Bar, this tells us that we want a ``floor'' at this point.
% \begin{macrocode}
\edef\braid@tmp{,\expandafter\the\value{braid@level}}%
\ge@addto@macro\braid@floors\braid@tmp%
\else
\ifx\braid@token\bgroup
% \end{macrocode}
% Begin group, which we reinterpret as begining a scope.
% \begin{macrocode}
\braid@beginscope
\else
\ifx\braid@token\egroup
% \end{macrocode}
% End group, which ends the scope
% \begin{macrocode}
\braid@endscope
\else
% \end{macrocode}
% Otherwise, we add the token to the braid label.
% \begin{macrocode}
\ge@addto@macro\braid@label{\braid@token}%
\fi
\fi
\fi
\fi
\fi
\fi
\fi
\fi
\braid@next%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@maybe@locate}
% 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.
% So we examine the next token for a \Verb+t+.
% \begin{macrocode}
\def\braid@maybe@locate{%
\afterassignment\braid@@maybe@locate\let\braid@token=%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@@maybe@locate}
% This is where we test for \Verb+t+ and act appropriately.
% \begin{macrocode}
\def\braid@@maybe@locate{%
\let\braid@next=\braid@handle
\ifx\braid@token t
\let\braid@next=\braid@find@location
\fi
\braid@next%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@find@location}
% This macro starts us looking for a coordinate.
% \begin{macrocode}
\def\braid@find@location{%
\afterassignment\braid@@find@location\let\braid@token=%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@@find@location}
% This is the test for the start of a coordinate.
% If we get a \Verb+(+ that means we've reached the coordinate.
% A space means ``carry on''.
% Anything else is a (non-fatal) error.
% \begin{macrocode}
\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%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@location@error}
% This is our error message for not getting a location.
% \begin{macrocode}
\def\braid@location@error{%
\PackageWarning{braids}{Could not figure out location for braid}%
\braid@process@start%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@locate}
% If we reached a \Verb+(+ when looking for a coordinate, everything up to the next \Verb+)+ is that coordinate.
% Then we parse the coordinate and call the relocation macro.
% \begin{macrocode}
\def\braid@locate#1){%
\tikz@scan@one@point\braid@relocate(#1)%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@relocate}
% This is the macro that actually does the relocation.
% \begin{macrocode}
\def\braid@relocate#1{%
#1\relax
\advance\pgf@x by -\braid@width
\pgftransformshift{\pgfqpoint{\pgf@x}{\pgf@y}}
\braid@process@start%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@assign@name}
% This macro saves our name.
% \begin{macrocode}
\def\braid@assign@name#1){%
\def\braid@name{#1}%
\braid@process@start%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@process@options}
% The intention of this macro is to allow setting of style options mid-braid.
% (At present, this wouldn't make a lot of sense.)
% \begin{macrocode}
\def\braid@process@options#1]{%
\tikzset{#1}%
\braid@process%
}
% \end{macrocode}
% \end{macro}
%
% The next macros handle the actual braid elements.
% Everything has to have a subscript, but the superscript is optional and can come before or after the subscript.
%
% \begin{macro}{\braid@sup}
% This handles braid elements of the form \Verb+a^{-1}_2+.
% \begin{macrocode}
\def\braid@sup#1_#2{\g@addto@macro\braid@label{_{#2}^{#1}}\braid@add@crossing{#2}{#1}}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@sub}
% \begin{macrocode}
% This handles braid elements of the form \Verb+a_1+ or \Verb+a_1^{-1}+.
\def\braid@sub#1{\@ifnextchar^{\braid@@sub{#1}}{\g@addto@macro\braid@label{_{#1}}\braid@add@crossing{#1}{1}}}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@@sub}
% Helper macro for \Verb+\braid@sub+.
% \begin{macrocode}
\def\braid@@sub#1^#2{\g@addto@macro\braid@label{_{#1}^{#2}}\braid@add@crossing{#1}{#2}}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@ne}
% Remember what \Verb+1+ looks like for testing against.
% \begin{macrocode}
\def\braid@ne{1}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@add@crossing}
% This is the macro which adds the crossing to the current list of strands.
% The strands are stored as \emph{soft paths} (see the TikZ/PGF documentation).
% So this selects the right strands and then extends them according to the crossing type.
% \begin{macrocode}
\def\braid@add@crossing#1#2{%
% \end{macrocode}
% Our crossing type, which is \Verb+#2+, is one of \Verb+1+ or \Verb+-1+.
% Our strands are \Verb+#1+ and \Verb-#1+1-.
% \begin{macrocode}
\edef\braid@crossing@type{#2}%
\edef\braid@this@strand{#1}%
\pgfmathtruncatemacro{\braid@next@strand}{#1+1}
% \end{macrocode}
% Increment the level counter, if requested.
% The controls whether the crossing is on the same level as the previous one or is one level further on.
% \begin{macrocode}
\ifbraid@increase@level
\stepcounter{braid@level}
\fi
% \end{macrocode}
% Default is to request increment so we set it for next time.
% \begin{macrocode}
\braid@increase@leveltrue
% \end{macrocode}
% Now we figure out the coordinates of the crossing.
% \Verb+(\braid@tx,\braid@ty)+ is the top-left corner (assuming the braid flows down the page).
% \Verb+(\braid@nx,\braid@ny)+ is the bottom-right corner (assuming the braid flows down the page).
% 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).
% Then from these values, we set \Verb+(\braid@nx,\braid@ny)+ by adding on the appropriate amount.
% The heights \Verb+\braid@cy+ and \Verb+\braid@dy+ are for the control points for the strands as they cross.
% They're actually the same height, but using two gives us the possibility of changing them independently in a later version of this package.
% 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).
% \begin{macrocode}
\braid@tx=\braid@this@strand\braid@width
\braid@ty=\value{braid@level}\braid@height
\advance\braid@ty by \braid@eh
\braid@nx=\braid@tx
\braid@ny=\braid@ty
\advance\braid@nx by \braid@width
\advance\braid@ny by \braid@height
\braid@cy=\braid@ty
\braid@dy=\braid@ny
\advance\braid@cy by .5\braid@height
\advance\braid@dy by -.5\braid@height
\advance\braid@ty by .05\braid@height
\advance\braid@ny by -.05\braid@height
% \end{macrocode}
% Now we try to find a starting point for the strand ending here.
% We might not have used this strand before, so it might not exist.
% \begin{macrocode}
\expandafter\let\expandafter\braid@this@path@origin\csname braid@strand@\braid@this@strand @origin\endcsname
% \end{macrocode}
% If we haven't seen this strand before, that one will be \Verb+\relax+.
% \begin{macrocode}
\ifx\braid@this@path@origin\relax
% \end{macrocode}
% Haven't seen this strand before, so initialise it.
% Record the initial position of the strand.
% \begin{macrocode}
\let\braid@this@path@origin\braid@this@strand
% \end{macrocode}
% Start a new soft path.
% \begin{macrocode}
\pgfsyssoftpath@setcurrentpath{\@empty}
\pgfpathmoveto{\pgfpoint{\braid@tx}{0pt}}
% \end{macrocode}
% Save the path as \Verb+\braid@this@path+.
% \begin{macrocode}
\pgfsyssoftpath@getcurrentpath{\braid@this@path}
\else
% \end{macrocode}
% We have seen this before, so we simply copy the associated path in to \Verb+\braid@this@path+.
% \begin{macrocode}
\expandafter\let\expandafter\braid@this@path\csname braid@strand@\braid@this@path@origin\endcsname
\fi
% \end{macrocode}
% Now we do the same again with the other strand in the crossing.
% \begin{macrocode}
\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
% \end{macrocode}
% Now that we have the paths for our two strands, we extend them to the next level.
% We start by selecting the first path.
% \begin{macrocode}
\pgfsyssoftpath@setcurrentpath{\braid@this@path}
% \end{macrocode}
% 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.
% \begin{macrocode}
\pgfpathlineto{\pgfqpoint{\braid@tx}{\braid@ty}}
% \end{macrocode}
% Curve across to the next position.
% Depending on the crossing type, we either have a single curve or we have to break it in two.
% \begin{macrocode}
\ifx\braid@crossing@type\braid@over@cross
% \end{macrocode}
% We're on the overpass, so just one curve needed.
% \begin{macrocode}
\pgfpathcurveto{\pgfqpoint{\braid@tx}{\braid@cy}}{\pgfqpoint{\braid@nx}{\braid@dy}}{\pgfqpoint{\braid@nx}{\braid@ny}}
\else
% \end{macrocode}
% We're on the underpass, so we need to interrupt our path to allow the other curve to go past.
% The choice of \Verb+.4+ and \Verb+.6+ should probably be configurable.
% \begin{macrocode}
\pgfpathcurvebetweentimecontinue{0}{.4}{\pgfqpoint{\braid@tx}{\braid@ty}}{\pgfqpoint{\braid@tx}{\braid@cy}}{\pgfqpoint{\braid@nx}{\braid@dy}}{\pgfqpoint{\braid@nx}{\braid@ny}}
\pgfpathcurvebetweentime{.6}{1}{\pgfqpoint{\braid@tx}{\braid@ty}}{\pgfqpoint{\braid@tx}{\braid@cy}}{\pgfqpoint{\braid@nx}{\braid@dy}}{\pgfqpoint{\braid@nx}{\braid@ny}}
\fi
% \end{macrocode}
% We're done with this path, so now we save it.
% \begin{macrocode}
\pgfsyssoftpath@getcurrentpath{\braid@this@path}
% \end{macrocode}
% Now do the same with the second path.
% \begin{macrocode}
\pgfsyssoftpath@setcurrentpath{\braid@next@path}
\pgfpathlineto{\pgfqpoint{\braid@nx}{\braid@ty}}
\ifx\braid@crossing@type\braid@over@cross
\pgfpathcurvebetweentimecontinue{0}{.4}{\pgfqpoint{\braid@nx}{\braid@ty}}{\pgfqpoint{\braid@nx}{\braid@cy}}{\pgfqpoint{\braid@tx}{\braid@dy}}{\pgfqpoint{\braid@tx}{\braid@ny}}
\pgfpathcurvebetweentime{.6}{1}{\pgfqpoint{\braid@nx}{\braid@ty}}{\pgfqpoint{\braid@nx}{\braid@cy}}{\pgfqpoint{\braid@tx}{\braid@dy}}{\pgfqpoint{\braid@tx}{\braid@ny}}
\else
\pgfpathcurveto{\pgfqpoint{\braid@nx}{\braid@cy}}{\pgfqpoint{\braid@tx}{\braid@dy}}{\pgfqpoint{\braid@tx}{\braid@ny}}
\fi
\pgfsyssoftpath@getcurrentpath{\braid@next@path}
% \end{macrocode}
% Now save the paths to their proper macros again.
% \begin{macrocode}
\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
% \end{macrocode}
% Now update the origins
% \begin{macrocode}
\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
% \end{macrocode}
% increment the strand counter, if necessary
% \begin{macrocode}
\pgfmathparse{\value{braid@strands} < \braid@next@strand ? "\noexpand\setcounter{braid@strands}{\braid@next@strand}" : ""}
\pgfmathresult
% \end{macrocode}
% And merrily go on our way with the next bit of the braid specification.
% \begin{macrocode}
\braid@process%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@floors@trim}
% The list of floors, if given, will start with a superfluous comma.
% This removes it.
% \begin{macrocode}
\def\braid@floors@trim,{}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@render@floor}
% This is the default rendering for floors: it draws a rectangle.
% \begin{macrocode}
\def\braid@render@floor{%
\draw (\floorsx,\floorsy) rectangle (\floorex,\floorey);
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@render}
% 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.
% \begin{macrocode}
\def\braid@render{
% \end{macrocode}
% Check for floors since we do them first.
% \begin{macrocode}
\ifx\braid@floors\@empty
\else
% \end{macrocode}
% Have some floors, start a scope and prepare to render them.
% \begin{macrocode}
\pgfsys@beginscope
% \end{macrocode}
% Clear the path (just to be sure).
% \begin{macrocode}
\pgfsyssoftpath@setcurrentpath{\empty}
% \end{macrocode}
% Trim the initial comma off the list of floors.
% \begin{macrocode}
\edef\braid@floors{\expandafter\braid@floors@trim\braid@floors}
% \end{macrocode}
% Initialise our horizontal coordinates.
% \begin{macrocode}
\braid@tx=\braid@width
\advance\braid@tx by \braid@eh
\braid@nx=\value{braid@strands}\braid@width
\advance\braid@nx by -\braid@eh
% \end{macrocode}
% Loop over the list of floors.
% \begin{macrocode}
\foreach \braid@f in \braid@floors {
\pgfsys@beginscope
% \end{macrocode}
% Figure out the vertical coordinates for the current floor.
% \begin{macrocode}
\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
% \end{macrocode}
% Save the coordinates for use in the floor rendering macro.
% \begin{macrocode}
\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
% \end{macrocode}
% Load general floor style options.
% \begin{macrocode}
\expandafter\tikzset\expandafter{\braid@floors@style}
% \end{macrocode}
% Load any style options specific to this floor.
% We're actually offset by 2 from what the user thinks the floor level is.
% \begin{macrocode}
\pgfmathtruncatemacro{\braid@ff}{\braid@f+2}
% \end{macrocode}
% Load the relevant floor style, if it exists.
% \begin{macrocode}
\expandafter\let\expandafter\braid@floor@style\csname braid@options@floor@\braid@ff\endcsname
\ifx\braid@floor@style\relax
\else
% \end{macrocode}
% There is a floor style for this level, so process it.
% \begin{macrocode}
\expandafter\tikzset\expandafter{\braid@floor@style}%
\fi
% \end{macrocode}
% The \Verb+\tikzset+ just parses the options, we need to call \Verb+\tikz@options+ to actually set them.
% \begin{macrocode}
\tikz@options
% \end{macrocode}
% Now we call the rendering code.
% \begin{macrocode}
\braid@render@floor
% \end{macrocode}
% Done!
% End the scope for \emph{this} floor and go again.
% \begin{macrocode}
\pgfsys@endscope
}
% \end{macrocode}
% Done rendering floors, end the scope.
% \begin{macrocode}
\pgfsys@endscope
\fi
% \end{macrocode}
% Finished with floors (if we had them), now get on with the strands.
% \begin{macrocode}
\stepcounter{braid@level}
\foreach \braid@k in {1,...,\value{braid@strands}} {
% \end{macrocode}
% Start a local scope to ensure we don't mess with other braids
% \begin{macrocode}
\pgfsys@beginscope
% \end{macrocode}
% Default is to draw each braid
% \begin{macrocode}
\tikz@mode@drawtrue%
\let\tikz@mode=\pgfutil@empty
\let\tikz@options=\pgfutil@empty
% \end{macrocode}
% (x,y) coordinates of bottom of strand
% \begin{macrocode}
\braid@tx=\braid@k\braid@width
\braid@ty=\value{braid@level}\braid@height
\advance\braid@ty by 2\braid@eh
% \end{macrocode}
% Try to find the starting point of this strand
% \begin{macrocode}
\expandafter\let\expandafter\braid@path@origin\csname braid@strand@\braid@k @origin\endcsname
\ifx\braid@path@origin\relax
% \end{macrocode}
% If that doesn't exist, we'll just draw a straight line
% so we move to the top of the current position
% \begin{macrocode}
\pgfsyssoftpath@setcurrentpath{\@empty}
\pgfpathmoveto{\pgfqpoint{\braid@tx}{0pt}}
\let\braid@path@origin\braid@k
\else
% \end{macrocode}
% If the path does exist, we load it
% \begin{macrocode}
\expandafter\let\expandafter\braid@path\csname braid@strand@\braid@path@origin\endcsname
\pgfsyssoftpath@setcurrentpath{\braid@path}
\fi
% \end{macrocode}
% Extend the path to the bottom
% \begin{macrocode}
\pgflineto{\pgfqpoint{\braid@tx}{\braid@ty}}
% \end{macrocode}
% Load common style options
% \begin{macrocode}
\expandafter\tikzset\expandafter{\braid@style}
% \end{macrocode}
% Load any style options specific to this strand
% \begin{macrocode}
\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
% \end{macrocode}
% This is the command that actually draws the strand.
% \begin{macrocode}
\edef\tikz@temp{\noexpand\pgfusepath{%
\iftikz@mode@draw draw\fi%
}}%
\tikz@temp
% \end{macrocode}
% If our braid has a name, we label the ends of the strand.
% \begin{macrocode}
\ifx\braid@name\pgfutil@empty
\else
% \end{macrocode}
% Label the ends of the strand.
% \begin{macrocode}
\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
% \end{macrocode}
% Done with this strand, close the scope and do the next one.
% \begin{macrocode}
\pgfsys@endscope
}
% \end{macrocode}
% If our braid has a name, we also want to label the centre.
% \begin{macrocode}
\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
% \end{macrocode}
% All done now, close the scope and end the group (which was opened right at the start).
% \begin{macrocode}
\pgfsys@endscope
\endgroup}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\braid@start}
% This starts off the braid, initialising a load of stuff.
% 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..
% \begin{macrocode}
\def\braid@start#1{%
\pgfsys@beginscope
\setcounter{braid@level}{-1}%
\let\braid@label\@empty
\let\braid@floors\@empty
\let\braid@name\empty
\pgfkeys{/pgf/braid/.cd,#1}
\let\braid@options\tikz@options
\tikz@transform
\setcounter{braid@strands}{\pgfkeysvalueof{/pgf/braid/number of strands}}%
\braid@width=\pgfkeysvalueof{/pgf/braid/width}
\braid@height=\pgfkeysvalueof{/pgf/braid/height}
\braid@eh=\pgfkeysvalueof{/pgf/braid/border height}
\braid@height=-\braid@height
\braid@eh=-\braid@eh
\braid@increase@leveltrue
\braid@process@start
}
% \end{macrocode}
% \end{macro}
%
% These are the lengths we'll use as we construct the braid
% \begin{macrocode}
\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
% \end{macrocode}
%
% An if to decide whether or not to step to the next level or not
% \begin{macrocode}
\newif\ifbraid@increase@level
% \end{macrocode}
%
% Some initial values
% \begin{macrocode}
\let\braid@style\pgfutil@empty
\let\braid@floors@style\pgfutil@empty
\def\braid@over@cross{1}
% \end{macrocode}
%
% Counters to track the strands and the levels.
% \begin{macrocode}
\newcounter{braid@level}
\newcounter{braid@strands}
% \end{macrocode}
%
% All the keys we'll use.
% \begin{macrocode}
\pgfkeys{
% \end{macrocode}
% Handle unknown keys by passing them to \Verb+pgf+ and \Verb+tikz+.
% \begin{macrocode}
/tikz/braid/.search also={/pgf},
/pgf/braid/.search also={/pgf,/tikz},
% \end{macrocode}
% Our ``namespace'' is \Verb+/pgf/braid+.
% \begin{macrocode}
/pgf/braid/.cd,
number of strands/.initial=0,
height/.initial=1cm,
width/.initial=1cm,
border height/.initial=.25cm,
name/.code={%
\def\braid@name{#1}%
},
at/.code={%
\braid@relocate{#1}%
},
floor command/.code={%
\def\braid@render@floor{#1}%
},
style strands/.code 2 args={%
\def\braid@temp{#2}%
\braidset{style each strand/.list={#1}}%
},
style each strand/.code={%
\expandafter\edef\csname braid@options@strand@#1\endcsname{\braid@temp}%
},
style floors/.code 2 args={%
\def\braid@temp{#2}%
\braidset{style each floor/.list={#1}}%
},
style each floor/.code={%
\expandafter\edef\csname braid@options@floor@#1\endcsname{\braid@temp}%
},
style all floors/.code={%
\def\braid@floors@style{#1}
}
}
% \end{macrocode}
% \begin{macro}{\braidset}
% Shorthand for setting braid-specific keys.
% \begin{macrocode}
\def\braidset#1{%
\pgfkeys{/pgf/braid/.cd,#1}}
% \end{macrocode}
% \end{macro}
% \iffalse
%</package>
% \fi
%
% \Finale
\endinput
|