~tex-sx/tex-sx/development

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