~tex-sx/tex-sx/development

« back to all changes in this revision

Viewing changes to braids.dtx

  • Committer: Andrew Stacey (Thargelion)
  • Date: 2011-06-05 20:34:05 UTC
  • Revision ID: stacey@math.ntnu.no-20110605203405-aozimho6cnnyyx29
Started to split off soft path manipulation stuff in to a separate style file, also reimplementing the code a bit

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
----------------------------------------------------------------
12
12
 
13
13
This package defines some commands for drawing braid diagrams with TikZ/PGF.
14
 
It was designed and tested with PGF2.10.
15
 
The initial idea of this package came from a question and answer on the site http://tex.stackexchange.com.
16
14
%</readme>
17
15
%<*internal>
18
16
\fi
48
46
This work is "maintained" (as per LPPL maintenance status) by
49
47
Andrew Stacey.
50
48
 
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.
 
49
This work consists of the file  braids.dtx
 
50
and the derived files           braids.ins,
 
51
                                braids.pdf, and
 
52
                                braids.sty.
58
53
 
59
54
\endpostamble
60
55
\usedir{tex/latex/braids}
81
76
%</internal>
82
77
%<*package>
83
78
\NeedsTeXFormat{LaTeX2e}
84
 
\ProvidesPackage{braids}[2011/10/18 v1.1 Tikz/PGF commands for drawing braid diagrams]
 
79
\ProvidesPackage{braids}[2011/05/07 v1.0 Tikz/PGF commands for drawing braid diagrams]
85
80
%</package>
86
81
%<*driver>
87
82
\documentclass{ltxdoc}
99
94
 
100
95
\usepackage{fancyvrb}
101
96
 
 
97
\newenvironment{example}
 
98
  {\VerbatimEnvironment
 
99
   \begin{VerbatimOut}[gobble=2]{example.out}}
 
100
  {\end{VerbatimOut}
 
101
   \begin{center}
 
102
%   \setlength{\parindent}{0pt}
 
103
   \fbox{\begin{minipage}{.9\linewidth}
 
104
     \lstset{breakatwhitespace=true,breaklines=true,language=TeX,basicstyle=\small}
 
105
     \lstinputlisting[]{example.out}
 
106
   \end{minipage}}
 
107
 
 
108
   \fbox{\begin{minipage}{.9\linewidth}
 
109
     \input{example.out}
 
110
   \end{minipage}}
 
111
\end{center}
 
112
}
102
113
\EnableCrossrefs
103
114
\CodelineIndex
104
115
\RecordChanges
108
119
%</driver>
109
120
% \fi
110
121
%
111
 
% \CheckSum{1078}
112
122
%
113
123
% \CharacterTable
114
124
%  {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
132
142
% \DoNotIndex{\newcommand,\newenvironment}
133
143
%
134
144
% \providecommand*{\url}{\texttt}
135
 
% \GetFileInfo{braids.sty}
136
 
% \title{The \textsf{braids} package: codebase}
 
145
% \GetFileInfo{brqids.dtx}
 
146
% \title{The \textsf{braids} package}
137
147
% \author{Andrew Stacey \\ \url{stacey@math.ntnu.no}}
138
148
% \date{\fileversion~from \filedate}
139
149
141
151
142
152
% \begin{center}
143
153
% \begin{tikzpicture}
144
 
% \braid[line width=3pt,line cap=round,style strands={1}{blue},number of strands=7] s_1 s_2 s_5^{-1};
 
154
% \braid[line width=3pt,line cap=round,style strand={1}{blue},number of strands=7] s_1 s_2 s_5^{-1};
145
155
% \end{tikzpicture}
146
156
% \end{center}
147
157
148
158
% \section{Introduction}
149
159
150
160
% This is a package for drawing braid diagrams using PGF/TikZ.
151
 
% Its inspiration was a question and answer on the website \url{http://tex.stackexchange.com}.
152
 
%
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}
 
161
% An example follows.
 
162
 
163
% %\begin{example}
 
164
% \begin{center}
 
165
% \begin{tikzpicture}[rotate=90]
 
166
% \braid[style strand={1}{red},style strand={2}{blue},style strand={3}{green}] s_1 s_2^{-1} s_1 s_2^{-1} s_1 s_2^{-1};
 
167
% \end{tikzpicture}
 
168
% \end{center}
 
169
% %\end{example}
 
170
 
171
% \section{Usage}
 
172
 
173
% \DescribeMacro{\braid}
 
174
% A braid is specified by the command \Verb+\braid+.
 
175
% The syntax for this command is as follows:
 
176
 
177
% \Verb+\braid[style options] (name) at (coordinate) braid-word;+
 
178
 
179
% The \Verb+braid-word+ is an expression in the braid group, such as \Verb+s_1 s_2^{-1}+.
 
180
% The generator labels are not significant.
 
181
% The exponent can be \Verb+1+, \Verb+{-1}+, or missing (in which case it defaults to \Verb+1+).
 
182
% Certain other symbols are allowed in the \Verb+braid-word+ which control the rendering of the braid.
 
183
% To get crossings to render at the same height, separate them with a hyphen (note: no check is made to ensure that the crossings can legally be put at the same height; \emph{caveat emptor}).
 
184
% To draw a \emph{floor}, precede the braid element by a vertical line.
 
185
% What happens then is that when the braid is rendered, the coordinates of the rectangle behind that crossing (wide enough to encompass all the strands) is passed to a command.
 
186
% The intention is that this command draw something behind the braid.
 
187
% The command is configurable by a key (see \ref{sec:styleopts}).
 
188
 
189
% The (optional) \Verb+name+ acts a little like the \Verb+name+ of a TikZ node.
 
190
% When it is specified, the routine that renders the braid also saves certain coordinates as if they were node anchors.
 
191
% Specifically, \Verb+coordinate+ nodes are placed at the centre of the braid diagram and at the ends of each strand.
 
192
% The centre has the label \Verb+name+, the strands are labelled \Verb+name-number-end+ and \Verb+name-rev-number-end+, where \Verb+name+ is the name given to the braid, \Verb+number+ is the number of the strand counting from the left, and \Verb+end+ is either \Verb+s+ for the start or \Verb+e+ for the end.
 
193
% If the version with \Verb+rev+ is used then the numbers correspond to the \emph{final} positions of the braids.
 
194
% The name can also be specified with the \Verb+name+ key.
 
195
%
 
196
% The (options) \Verb+at (coordinate)+ syntax positions the braid at the \Verb+coordinate+ in the current picture.
 
197
% Due to the implementation, the coordinate has to be known at the start, but the width and height of the braid are only known at the end.
 
198
% Therefore, the braid is positioned so that the start of the first strand is at \Verb+(coordinate)+.
 
199
% This can also be specified using the \Verb+at+ key.
 
200
%
 
201
% The \Verb+style options+ set the style for the braid strands.
 
202
% They can be grouped into three types: options that set up the main parameters for the braid, options that set the default style for the strands, and options that set up styles for individual strands.
 
203
% The options are as follows.
 
204
 
205
% \subsection{Style Options}
 
206
% \label{sec:styleopts}
 
207
 
208
% \DescribeMacro{number of strands} The key \Verb+number of strands+ sets the minimum number of strands for the braid.
 
209
% The number of strands will grow according to the terms in the braid word so this merely sets a lower bound.
 
210
% If not set, the number of strands will be determined by the terms in the braid word.
 
211
 
212
% \DescribeMacro{height}
 
213
% The key \Verb+height+ sets the height of the piece of the braid corresponding to an element in the group.
 
214
 
215
% \DescribeMacro{width}
 
216
% The key \Verb+width+ sets the separation of the strands in the braid.
 
217
 
218
% \DescribeMacro{border height}
 
219
% The key \Verb+border height+ adds a little extra length to the strands at the start and end of the braid.
 
220
 
221
% \DescribeMacro{style strand}
 
222
% The style of the strands are controlled by two types of option.
 
223
% Style options that are set on the \Verb+\braid+ command are passed to every strand.
 
224
% It is also possible to add style options to individual strands using the key \Verb+style strand+.
 
225
% This takes two options, a strand number and a list of options to be applied to that strand.
 
226
% Thus, the syntax is \Verb+style strand={n}{options}+.
 
227
% The strands are numbered by their starting position.
 
228
% Not all of the standard TikZ style options are possible due to the way that the strands are constructed.
 
229
% Basically, the options that are allowed are those that do not require changing the path or drawing it more than once.
 
230
 
231
% \DescribeMacro{floor command}
 
232
% When a floor is requested behind a crossing, the actual way to render it is determined by a command.
 
233
% This key allows the user to define that command.
 
234
% The argument to this key should be the code that should be executed for each floor.
 
235
% To avoid the hassle of getting the number of hashes right, the command should take no arguments.
 
236
% Rather, the coordinates of the rectangle are saved in to macros \Verb+\floorsx+, \Verb+\floorsy+, \Verb+\floorex+, \Verb+\floorey+ (these macros will expand to something like \Verb+10pt+) and the command should use these to position the drawing.
 
237
% The default is to draw a line at the top and at the bottom of the rectangle.
 
238
%
 
239
% \DescribeMacro{style floors}
 
240
% \DescribeMacro{style floor}
 
241
% In the spirit of separating \emph{style} and \emph{content}, the style options for the floors can be specified separately to the command (of course, they could be built in to the command).
 
242
% One advantage of this over building them in to the command is to allow them to be overridden for individual floors.
 
243
% The \Verb+style floors+ sets up options to be used for \emph{all} floors, whilst the \Verb+style floor={n}{options}+ sets up options to be used only for the \(n\)th floor.
 
244
% Anything specified in the \Verb+floor command+ will take precedence over both of these.
 
245
%
 
246
% Any other style options are passed to the underlying TikZ/PGF system and so may influence how the braid is drawn (but note that not all keys make sense due to the implementation).
 
247
%
 
248
%
 
249
% \section{Example}
 
250
%
 
251
% Here is a more detailed example.
 
252
% (Note that the floors are not drawn in the below.
 
253
% I have no idea why that is as they work when this example is put in a standalone file.
 
254
% It is presumably something to do with the documentation packages.)
 
255
%
 
256
%
 
257
% \begin{example}
 
258
% \begin{center}
 
259
% \begin{tikzpicture}
 
260
% \braid[
 
261
%   style floors={fill=yellow},
 
262
%   style floor={1}{dashed,fill=yellow!50!green},
 
263
%   floor command={%
 
264
%   \message{drawing floor}
 
265
%    \fill (\floorsx,\floorsy) rectangle (\floorex,\floorey);
 
266
%    \draw (\floorsx,\floorsy) -- (\floorex,\floorsy);
 
267
%   },
 
268
%   line width=2pt,
 
269
%   style strand={1}{red},
 
270
%   style strand={2}{blue},
 
271
%   style strand={3}{green}
 
272
% ] (braid) at (2,0) | s_1-s_3-s_5 | s_2^{-1}-s_4| s_1-s_4 s_2^{-1} s_1-s_3 s_2^{-1}-s_4^{-1};
 
273
% \fill[yellow] (2,0) circle (4pt);
 
274
% \fill[purple] (braid) circle (4pt);
 
275
% \node[at=(braid-3-s),pin=north west:strand 3] {};
 
276
% \node[at=(braid-3-e),pin=south west:strand 3] {};
 
277
% \node[at=(braid-rev-3-s),pin=north east:strand 3 (from bottom)] {};
 
278
% \node[at=(braid-rev-3-e),pin=south east:strand 3 (from bottom)] {};
 
279
% \end{tikzpicture}
 
280
% \end{center}
 
281
% \end{example}
 
282
%
161
283
% \StopEventually{}
162
284
%
163
285
% \section{Implementation}
166
288
%<*package>
167
289
% \fi
168
290
%
169
 
% Test the version of PGF to see if it's what we expect.
170
 
% If not, issue a warning (but continue anyway; after all, it \emph{might just work}).
171
 
%    \begin{macrocode}
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
177
 
%    \end{macrocode}
178
291
% \begin{macro}{\ge@addto@macro}
179
292
% This is an expanded version of \Verb+\g@addto@macro+.
180
293
% Namely, it adds the \emph{expansion} of the second argument to the first.
188
301
% \end{macro}
189
302
% \begin{macro}{\braid}
190
303
% This is the user command.
191
 
% We start a group to ensure that all our assignments are local, and then call our initialisation code.
192
 
% The optional argument is for any keys to be set.
 
304
% We start a group to ensure that all our assignments are local, and then call our initialisation code
193
305
%    \begin{macrocode}
194
306
\newcommand{\braid}[1][]{%
195
307
  \begingroup
199
311
%
200
312
% \begin{macro}{\braid@process}
201
313
% This is the token swallower.
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.
203
 
% (Incidentally, the code here is heavily influenced by TikZ.
204
 
% That's probably not very surprising.)
 
314
% This takes the next token on the braid specification and passes it to the handler command which decides what to do next.
205
315
%    \begin{macrocode}
206
316
\def\braid@process{%
207
317
  \afterassignment\braid@handle\let\braid@token=%
211
321
%
212
322
% \begin{macro}{\braid@process@start}
213
323
% 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.
214
 
% Specifically, we test for the \Verb+at+ and \Verb+(name)+ possibilities.
215
324
%    \begin{macrocode}
216
325
\def\braid@process@start{%
217
326
  \afterassignment\braid@handle@start\let\braid@token=%
226
335
\def\braid@handle@start{%
227
336
  \let\braid@next=\braid@handle
228
337
  \ifx\braid@token a
229
 
%    \end{macrocode}
230
 
% We got an \Verb+a+ so we might have an \Verb+at (coordinate)+
231
 
%    \begin{macrocode}
232
338
   \let\braid@next=\braid@maybe@locate
233
339
  \else
234
340
  \ifx\braid@token(%)
235
 
%    \end{macrocode}
236
 
% We got an \Verb+(+ so we have a name
237
 
%    \begin{macrocode}
238
341
   \iffalse)\fi %Indentation hack!
239
342
   \let\braid@next=\braid@assign@name
240
343
  \else
241
344
  \ifx\braid@token\@sptoken
242
 
%    \end{macrocode}
243
 
% Space; boring, redo from start
244
 
%    \begin{macrocode}
245
345
   \let\braid@next=\braid@process@start
246
346
  \fi
247
347
  \fi
254
354
% \begin{macro}{\braid@handle}
255
355
% This is the main handler for parsing the braid word.
256
356
% It decides what action to take depending on what the token is.
257
 
% We have to be a bit careful with catcodes, some packages set
258
 
% \Verb+;+ and \Verb+|+ to be active.
259
 
% We should probably also be careful with \Verb+^+ and \Verb+_+.
260
357
%    \begin{macrocode}
261
 
\let\braid@semicolon=;
262
 
\let\braid@bar=|
263
358
\def\braid@handle{%
264
359
  \let\braid@next=\braid@process
265
 
%    \end{macrocode}
266
 
% Start by checking our catcodes to see what we should check against
267
 
%    \begin{macrocode}
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
275
 
%    \end{macrocode}
276
 
% Semicolon, means that we're done reading our braid.
277
 
% It's time to render it.
278
 
%    \begin{macrocode}
 
360
  \ifx\braid@token;
279
361
   \let\braid@next=\braid@render
280
362
  \else
281
363
  \ifx\braid@token^
282
 
%    \end{macrocode}
283
 
% Superscript character, the next token tells us whether it's an over-crossing or an under-crossing.
284
 
%    \begin{macrocode}
285
364
   \let\braid@next=\braid@sup
286
365
  \else
287
366
  \ifx\braid@token_
288
 
%    \end{macrocode}
289
 
% Subscript character, the next token tells us which strands cross.
290
 
%    \begin{macrocode}
291
367
   \let\braid@next=\braid@sub
292
368
  \else
293
369
  \ifx\braid@token-
294
 
%    \end{macrocode}
295
 
% Hyphen, this is so that we can have more than one crossing on the same level.
296
 
%    \begin{macrocode}
297
370
   \braid@increase@levelfalse
298
371
  \else
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[%
311
 
%    \end{macrocode}
312
 
% Open bracket, this means we have some more options to process.
313
 
%    \begin{macrocode}
 
372
  \ifx\braid@token[
314
373
   \let\braid@next=\braid@process@options
315
374
  \else
316
 
  \ifx\braid@token\braid@bar
317
 
%    \end{macrocode}
318
 
% Bar, this tells us that we want a ``floor'' at this point.
319
 
%    \begin{macrocode}
 
375
  \ifx\braid@token|
320
376
   \edef\braid@tmp{,\expandafter\the\value{braid@level}}%
321
377
   \ge@addto@macro\braid@floors\braid@tmp%
322
378
  \else
323
379
  \ifx\braid@token\bgroup
324
 
%    \end{macrocode}
325
 
% Begin group, which we reinterpret as begining a scope.
326
 
%    \begin{macrocode}
327
380
   \braid@beginscope
328
381
  \else
329
382
  \ifx\braid@token\egroup
330
 
%    \end{macrocode}
331
 
% End group, which ends the scope
332
 
%    \begin{macrocode}
333
383
   \braid@endscope
334
384
  \else
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
340
 
  \else
341
 
%    \end{macrocode}
342
 
% Otherwise, we add the token to the braid label.
343
 
%    \begin{macrocode}
344
385
  \ge@addto@macro\braid@label{\braid@token}%
345
386
  \fi
346
387
  \fi
350
391
  \fi
351
392
  \fi
352
393
  \fi
353
 
  \fi
354
 
  \fi
355
 
  \fi
356
394
  \braid@next%
357
395
}
358
396
%    \end{macrocode}
411
449
% \end{macro}
412
450
%
413
451
% \begin{macro}{\braid@location@error}
414
 
% This is our error message for not getting a location.
 
452
% This is our error message for not getting an error
415
453
%    \begin{macrocode}
416
454
\def\braid@location@error{%
417
 
  \PackageWarning{braids}{Could not figure out location for braid}%
 
455
  \errmessage{Could not figure out location for braid}%
418
456
  \braid@process@start%
419
457
}
420
458
%    \end{macrocode}
422
460
%
423
461
% \begin{macro}{\braid@locate}
424
462
% If we reached a \Verb+(+ when looking for a coordinate, everything up to the next \Verb+)+ is that coordinate.
425
 
% Then we parse the coordinate and call the relocation macro.
426
463
%    \begin{macrocode}
427
464
\def\braid@locate#1){%
428
 
  \tikz@scan@one@point\braid@relocate(#1)%
 
465
  \braid@relocate{(#1)}%
429
466
}
430
467
%    \end{macrocode}
431
468
% \end{macro}
432
469
%
433
470
% \begin{macro}{\braid@relocate}
434
471
% This is the macro that actually does the relocation.
 
472
%
 
473
% TODO: Change the method of getting the coordinate to use \Verb+\tikz@scan@one@point+
435
474
%    \begin{macrocode}
436
475
\def\braid@relocate#1{%
437
 
  #1\relax
 
476
  \tikz@scan@one@point\braid@@relocate#1}
 
477
\def\braid@@relocate#1{%
 
478
  #1
438
479
  \advance\pgf@x by -\braid@width
439
480
  \pgftransformshift{\pgfqpoint{\pgf@x}{\pgf@y}}
440
481
  \braid@process@start%
463
504
%    \end{macrocode}
464
505
% \end{macro}
465
506
%
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
 
%
469
507
% \begin{macro}{\braid@sup}
470
508
% This handles braid elements of the form \Verb+a^{-1}_2+.
471
509
%    \begin{macrocode}
495
533
% \end{macro}
496
534
%
497
535
% \begin{macro}{\braid@add@crossing}
498
 
% This is the macro which adds the crossing to the current list of strands.
 
536
% This is the macro which add the crossing to the current list of strands.
499
537
% The strands are stored as \emph{soft paths} (see the TikZ/PGF documentation).
500
538
% So this selects the right strands and then extends them according to the crossing type.
501
539
%    \begin{macrocode}
502
540
\def\braid@add@crossing#1#2{%
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}%
 
541
  \edef\braid@crossing@type{#2}
 
542
  \edef\braid@this@strand{#1}
509
543
  \pgfmathtruncatemacro{\braid@next@strand}{#1+1}
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}
 
544
 
 
545
% increment the counter, if requested
514
546
  \ifbraid@increase@level
515
547
  \stepcounter{braid@level}
516
548
  \fi
517
 
%    \end{macrocode}
518
 
% Default is to request increment so we set it for next time.
519
 
%    \begin{macrocode}
 
549
% request increment next time
520
550
  \braid@increase@leveltrue
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}
 
551
 
 
552
% Coordinates of crossing
531
553
  \braid@tx=\braid@this@strand\braid@width
532
554
  \braid@ty=\value{braid@level}\braid@height
533
555
  \advance\braid@ty by \braid@eh
535
557
  \braid@ny=\braid@ty
536
558
  \advance\braid@nx by \braid@width
537
559
  \advance\braid@ny by \braid@height
538
 
  \advance\braid@ty by \braid@nf\braid@height
539
 
  \advance\braid@ny by -\braid@nf\braid@height
540
560
  \braid@cy=\braid@ty
541
561
  \braid@dy=\braid@ny
542
 
  \advance\braid@cy by \braid@cf\braid@height
543
 
  \advance\braid@dy by -\braid@cf\braid@height
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}
 
562
  \advance\braid@cy by .5\braid@height
 
563
  \advance\braid@dy by -.5\braid@height
 
564
  \advance\braid@ty by .05\braid@height
 
565
  \advance\braid@ny by -.05\braid@height
 
566
 
 
567
% Try to find a starting point for the strand ending here
548
568
  \expandafter\let\expandafter\braid@this@path@origin\csname braid@strand@\braid@this@strand @origin\endcsname
549
 
%    \end{macrocode}
550
 
% If we haven't seen this strand before, that one will be \Verb+\relax+.
551
 
%    \begin{macrocode}
 
569
 
552
570
\ifx\braid@this@path@origin\relax
553
 
%    \end{macrocode}
554
 
% Haven't seen this strand before, so initialise it.
555
 
% Record the initial position of the strand.
556
 
%    \begin{macrocode}
 
571
  % Haven't seen this strand before, so initialise it
 
572
  % Record the initial position of the strand
557
573
  \let\braid@this@path@origin\braid@this@strand
558
 
%    \end{macrocode}
559
 
% Start a new soft path.
560
 
%    \begin{macrocode}
 
574
  % start a new soft path
561
575
  \pgfsyssoftpath@setcurrentpath{\@empty}
562
576
  \pgfpathmoveto{\pgfpoint{\braid@tx}{0pt}}
563
 
%    \end{macrocode}
564
 
% Save the path as \Verb+\braid@this@path+.
565
 
%    \begin{macrocode}
 
577
  % save the path
566
578
  \pgfsyssoftpath@getcurrentpath{\braid@this@path}
567
579
  \else
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}
571
580
  \expandafter\let\expandafter\braid@this@path\csname braid@strand@\braid@this@path@origin\endcsname
572
581
  \fi
573
 
%    \end{macrocode}
574
 
% Now we do the same again with the other strand in the crossing.
575
 
%    \begin{macrocode}
 
582
 
 
583
% Try to find a starting point for the next strand ending here
576
584
  \expandafter\let\expandafter\braid@next@path@origin\csname braid@strand@\braid@next@strand @origin\endcsname
 
585
 
577
586
  \ifx\braid@next@path@origin\relax
 
587
  % Haven't seen this strand before, so initialise it
 
588
  % Record the initial position of the strand
578
589
  \let\braid@next@path@origin\braid@next@strand
 
590
  % start a new soft path
579
591
  \pgfsyssoftpath@setcurrentpath{\@empty}
580
592
  \pgfpathmoveto{\pgfpoint{\braid@nx}{0pt}}
 
593
  % save the path
581
594
  \pgfsyssoftpath@getcurrentpath{\braid@next@path}
582
595
  \else
583
596
  \expandafter\let\expandafter\braid@next@path\csname braid@strand@\braid@next@path@origin\endcsname
584
597
  \fi
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}
 
598
 
 
599
  % Start with the first path
589
600
  \pgfsyssoftpath@setcurrentpath{\braid@this@path}
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}
 
601
  % Draw a line down to the current level
593
602
  \pgfpathlineto{\pgfqpoint{\braid@tx}{\braid@ty}}
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.
597
 
% Our gap is to interrupt at times determined by the gap key.
598
 
%    \begin{macrocode}
599
 
\pgfmathsetmacro{\braid@gst}{0.5 - \pgfkeysvalueof{/pgf/braid/gap}}%
600
 
\pgfmathsetmacro{\braid@gend}{0.5 + \pgfkeysvalueof{/pgf/braid/gap}}%
 
603
  % Curve across to the next position
601
604
\ifx\braid@crossing@type\braid@over@cross
602
 
%    \end{macrocode}
603
 
% We're on the overpass, so just one curve needed.
604
 
%    \begin{macrocode}
605
605
\pgfpathcurveto{\pgfqpoint{\braid@tx}{\braid@cy}}{\pgfqpoint{\braid@nx}{\braid@dy}}{\pgfqpoint{\braid@nx}{\braid@ny}}
606
606
\else
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}
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}}
 
607
\pgfpathcurvebetweentimecontinue{0}{.4}{\pgfqpoint{\braid@tx}{\braid@ty}}{\pgfqpoint{\braid@tx}{\braid@cy}}{\pgfqpoint{\braid@nx}{\braid@dy}}{\pgfqpoint{\braid@nx}{\braid@ny}}
 
608
\pgfpathcurvebetweentime{.6}{1}{\pgfqpoint{\braid@tx}{\braid@ty}}{\pgfqpoint{\braid@tx}{\braid@cy}}{\pgfqpoint{\braid@nx}{\braid@dy}}{\pgfqpoint{\braid@nx}{\braid@ny}}
612
609
\fi
613
 
%    \end{macrocode}
614
 
% We're done with this path, so now we save it.
615
 
%    \begin{macrocode}
 
610
    % Save the path
616
611
  \pgfsyssoftpath@getcurrentpath{\braid@this@path}
617
 
%    \end{macrocode}
618
 
% Now do the same with the second path.
619
 
%    \begin{macrocode}
 
612
 
 
613
  % Now do the same with the second path
620
614
  \pgfsyssoftpath@setcurrentpath{\braid@next@path}
 
615
  % Draw a line down to the current level
621
616
  \pgfpathlineto{\pgfqpoint{\braid@nx}{\braid@ty}}
 
617
  % Curve across to the previous position
622
618
\ifx\braid@crossing@type\braid@over@cross
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}}
 
619
\pgfpathcurvebetweentimecontinue{0}{.4}{\pgfqpoint{\braid@nx}{\braid@ty}}{\pgfqpoint{\braid@nx}{\braid@cy}}{\pgfqpoint{\braid@tx}{\braid@dy}}{\pgfqpoint{\braid@tx}{\braid@ny}}
 
620
\pgfpathcurvebetweentime{.6}{1}{\pgfqpoint{\braid@nx}{\braid@ty}}{\pgfqpoint{\braid@nx}{\braid@cy}}{\pgfqpoint{\braid@tx}{\braid@dy}}{\pgfqpoint{\braid@tx}{\braid@ny}}
625
621
\else
626
622
  \pgfpathcurveto{\pgfqpoint{\braid@nx}{\braid@cy}}{\pgfqpoint{\braid@tx}{\braid@dy}}{\pgfqpoint{\braid@tx}{\braid@ny}}
627
623
\fi
 
624
  % Save the path
628
625
  \pgfsyssoftpath@getcurrentpath{\braid@next@path}
629
 
%    \end{macrocode}
630
 
% Now save the paths to their proper macros again.
631
 
%    \begin{macrocode}
 
626
 
 
627
% Now save the paths again
632
628
  \expandafter\let\csname braid@strand@\braid@this@path@origin \endcsname\braid@this@path
633
629
  \expandafter\let\csname braid@strand@\braid@next@path@origin \endcsname\braid@next@path
634
 
%    \end{macrocode}
635
 
% Now update the origins
636
 
%    \begin{macrocode}
 
630
 
 
631
  % Now update the origins
637
632
  \expandafter\let\csname braid@strand@\braid@this@strand @origin\endcsname\braid@next@path@origin
638
633
  \expandafter\let\csname braid@strand@\braid@next@strand @origin\endcsname\braid@this@path@origin
639
 
%    \end{macrocode}
 
634
 
640
635
% increment the strand counter, if necessary
641
 
%    \begin{macrocode}
642
636
  \pgfmathparse{\value{braid@strands} < \braid@next@strand ? "\noexpand\setcounter{braid@strands}{\braid@next@strand}" : ""}
643
637
  \pgfmathresult
644
 
%    \end{macrocode}
645
 
% And merrily go on our way with the next bit of the braid specification.
646
 
%    \begin{macrocode}
647
 
  \braid@process%
648
 
}
649
 
%    \end{macrocode}
650
 
% \end{macro}
651
 
%
652
 
% \begin{macro}{\braid@olabel@strand}
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.
656
 
% This version takes the strand number as meaning the \emph{original} ordering.
657
 
%    \begin{macrocode}
658
 
\newcommand{\braid@olabel@strand}[3][]{%
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
 
%
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
638
  \braid@process%
684
639
}
685
640
%    \end{macrocode}
694
649
% \end{macro}
695
650
%
696
651
% \begin{macro}{\braid@render@floor}
697
 
% This is the default rendering for floors: it draws a rectangle.
 
652
% This is the default render for floors: it draws a rectangle.
698
653
%    \begin{macrocode}
699
654
\def\braid@render@floor{%
700
655
    \draw (\floorsx,\floorsy) rectangle (\floorex,\floorey);
702
657
%    \end{macrocode}
703
658
% \end{macro}
704
659
%
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
 
%
801
660
% \begin{macro}{\braid@render}
802
661
% 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
662
%    \begin{macrocode}
804
663
\def\braid@render{
805
 
%    \end{macrocode}
806
 
% Check for floors since we do them first.
807
 
%    \begin{macrocode}
808
664
    \ifx\braid@floors\@empty
809
665
    \else
810
 
%    \end{macrocode}
811
 
% Have some floors, start a scope and prepare to render them.
812
 
%    \begin{macrocode}
813
666
    \pgfsys@beginscope
814
 
%    \end{macrocode}
815
 
% Clear the path (just to be sure).
816
 
%    \begin{macrocode}
817
667
    \pgfsyssoftpath@setcurrentpath{\empty}
818
 
%    \end{macrocode}
819
 
% Trim the initial comma off the list of floors.
820
 
%    \begin{macrocode}
821
668
    \edef\braid@floors{\expandafter\braid@floors@trim\braid@floors}
822
 
%    \end{macrocode}
823
 
% Initialise our horizontal coordinates.
824
 
%    \begin{macrocode}
825
669
    \braid@tx=\braid@width
826
670
    \advance\braid@tx by \braid@eh
827
671
    \braid@nx=\value{braid@strands}\braid@width
828
672
    \advance\braid@nx by -\braid@eh
829
 
%    \end{macrocode}
830
 
% Loop over the list of floors.
831
 
%    \begin{macrocode}
832
673
    \foreach \braid@f in \braid@floors {
833
674
      \pgfsys@beginscope
834
 
%    \end{macrocode}
835
 
% Figure out the vertical coordinates for the current floor.
836
 
%    \begin{macrocode}
837
675
      \braid@ty=\braid@f\braid@height
838
676
      \advance\braid@ty by \braid@eh
839
677
      \advance\braid@ty by \braid@height
840
678
      \braid@ny=\braid@ty
841
679
      \advance\braid@ny by \braid@height
842
 
%    \end{macrocode}
843
 
% Save the coordinates for use in the floor rendering macro.
844
 
%    \begin{macrocode}
845
680
      \edef\floorsx{\the\braid@tx}
846
681
      \edef\floorsy{\the\braid@ty}
847
682
      \edef\floorex{\the\braid@nx}
848
683
      \edef\floorey{\the\braid@ny}
849
684
      \let\tikz@options=\pgfutil@empty
850
 
%    \end{macrocode}
851
 
% Load general floor style options.
852
 
%    \begin{macrocode}
 
685
% Load general floor style options
853
686
    \expandafter\tikzset\expandafter{\braid@floors@style}
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}
 
687
% Load any style options specific to this floor
858
688
      \pgfmathtruncatemacro{\braid@ff}{\braid@f+2}
859
 
%    \end{macrocode}
860
 
% Load the relevant floor style, if it exists.
861
 
%    \begin{macrocode}
862
689
    \expandafter\let\expandafter\braid@floor@style\csname braid@options@floor@\braid@ff\endcsname
863
690
    \ifx\braid@floor@style\relax
864
691
    \else
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}%
 
692
    \expandafter\tikzset\expandafter{\braid@floor@style}
869
693
    \fi
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}
873
694
\tikz@options
874
 
%    \end{macrocode}
875
 
% Now we call the rendering code.
876
 
%    \begin{macrocode}
877
 
\braid@render@floor
878
 
%    \end{macrocode}
879
 
% Done!
880
 
% End the scope for \emph{this} floor and go again.
881
 
%    \begin{macrocode}
 
695
      \braid@render@floor
882
696
\pgfsys@endscope
883
697
    }
884
 
%    \end{macrocode}
885
 
% Done rendering floors, end the scope.
886
 
%    \begin{macrocode}
887
698
    \pgfsys@endscope
888
699
    \fi
889
 
%    \end{macrocode}
890
 
% Finished with floors (if we had them), now get on with the strands.
891
 
%    \begin{macrocode}
892
700
  \stepcounter{braid@level}
893
701
  \foreach \braid@k in {1,...,\value{braid@strands}} {
894
 
%    \end{macrocode}
895
702
% Start a local scope to ensure we don't mess with other braids
896
 
%    \begin{macrocode}
897
703
    \pgfsys@beginscope
898
 
%    \end{macrocode}
899
704
% Default is to draw each braid
900
 
%    \begin{macrocode}
901
705
    \tikz@mode@drawtrue%
902
706
    \let\tikz@mode=\pgfutil@empty
903
707
    \let\tikz@options=\pgfutil@empty
904
 
%    \end{macrocode}
905
708
% (x,y) coordinates of bottom of strand
906
 
%    \begin{macrocode}
907
709
    \braid@tx=\braid@k\braid@width
908
710
    \braid@ty=\value{braid@level}\braid@height
909
711
    \advance\braid@ty by 2\braid@eh
910
 
%    \end{macrocode}
911
712
% Try to find the starting point of this strand
912
 
%    \begin{macrocode}
913
713
    \expandafter\let\expandafter\braid@path@origin\csname braid@strand@\braid@k @origin\endcsname
914
714
    \ifx\braid@path@origin\relax
915
 
%    \end{macrocode}
916
715
% If that doesn't exist, we'll just draw a straight line
917
716
% so we move to the top of the current position
918
 
%    \begin{macrocode}
919
717
    \pgfsyssoftpath@setcurrentpath{\@empty}
920
718
    \pgfpathmoveto{\pgfqpoint{\braid@tx}{0pt}}
921
719
    \let\braid@path@origin\braid@k
922
720
    \else
923
 
%    \end{macrocode}
924
721
% If the path does exist, we load it
925
 
%    \begin{macrocode}
926
722
    \expandafter\let\expandafter\braid@path\csname braid@strand@\braid@path@origin\endcsname
927
723
    \pgfsyssoftpath@setcurrentpath{\braid@path}
928
724
    \fi
929
 
%    \end{macrocode}
930
725
% Extend the path to the bottom
931
 
%    \begin{macrocode}
932
726
    \pgflineto{\pgfqpoint{\braid@tx}{\braid@ty}}
933
 
%    \end{macrocode}
934
727
% Load common style options
935
 
%    \begin{macrocode}
936
728
    \expandafter\tikzset\expandafter{\braid@style}
937
 
%    \end{macrocode}
938
729
% Load any style options specific to this strand
939
 
%    \begin{macrocode}
940
730
    \expandafter\let\expandafter\braid@style\csname braid@options@strand@\braid@path@origin\endcsname
941
731
    \ifx\braid@style\relax
942
732
    \else
945
735
\braid@options
946
736
    \tikz@mode
947
737
    \tikz@options
948
 
%    \end{macrocode}
949
 
% This is the command that actually draws the strand.
950
 
%    \begin{macrocode}
951
738
      \edef\tikz@temp{\noexpand\pgfusepath{%
952
739
          \iftikz@mode@draw draw\fi%
953
740
      }}%
954
741
      \tikz@temp
955
 
%    \end{macrocode}
956
 
% If our braid has a name, we label the ends of the strand.
957
 
%    \begin{macrocode}
958
742
\ifx\braid@name\pgfutil@empty
959
743
\else
960
 
%    \end{macrocode}
961
 
% Label the ends of the strand.
962
 
%    \begin{macrocode}
 
744
% label the ends of the strand
963
745
\coordinate (\braid@name-\braid@path@origin-e) at (\braid@tx,\braid@ty);
964
746
\coordinate (\braid@name-rev-\braid@k-e) at (\braid@tx,\braid@ty);
965
747
\braid@nx=\braid@path@origin\braid@width
966
748
\coordinate (\braid@name-\braid@path@origin-s) at (\braid@nx,0pt);
967
749
\coordinate (\braid@name-rev-\braid@k-s) at (\braid@nx,0pt);
968
750
\fi
969
 
%    \end{macrocode}
970
 
% Done with this strand, close the scope and do the next one.
971
 
%    \begin{macrocode}
 
751
% Close the scope
972
752
   \pgfsys@endscope
973
753
  }
974
 
%    \end{macrocode}
975
 
% If our braid has a name, we also want to label the centre.
976
 
%    \begin{macrocode}
977
754
    \ifx\braid@name\pgfutil@empty
978
755
    \else
979
756
    \braid@tx=\value{braid@strands}\braid@width
984
761
    \braid@ty=.5\braid@ty
985
762
    \coordinate (\braid@name) at (\braid@tx,\braid@ty);
986
763
    \fi
987
 
%    \end{macrocode}
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}
995
 
% All done now, close the scope and end the group (which was opened right at the start).
996
 
%    \begin{macrocode}
997
764
    \pgfsys@endscope
998
765
  \endgroup}
999
766
%    \end{macrocode}
1001
768
%
1002
769
% \begin{macro}{\braid@start}
1003
770
% This starts off the braid, initialising a load of stuff.
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..
1005
771
%    \begin{macrocode}
1006
772
\def\braid@start#1{%
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
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
1021
 
  \let\braid@options\tikz@options
 
773
      \pgfsys@beginscope
 
774
      \setcounter{braid@level}{-1}%
 
775
      \let\braid@label\@empty
 
776
      \let\braid@floors\@empty
 
777
      \let\braid@name\empty
 
778
      \pgfkeys{/pgf/braid/.cd,#1}
 
779
      \let\braid@options\tikz@options
1022
780
  \tikz@transform
1023
 
  \setcounter{braid@strands}{\pgfkeysvalueof{/pgf/braid/number of           strands}}%
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}%
1029
 
  \braid@height=-\braid@height
1030
 
  \braid@eh=-\braid@eh
1031
 
  \braid@increase@leveltrue
1032
 
  \braid@process@start
 
781
      \setcounter{braid@strands}{\pgfkeysvalueof{/pgf/braid/number of           strands}}%
 
782
    \braid@width=\pgfkeysvalueof{/pgf/braid/width}
 
783
    \braid@height=\pgfkeysvalueof{/pgf/braid/height}
 
784
    \braid@eh=\pgfkeysvalueof{/pgf/braid/border height}
 
785
    \braid@height=-\braid@height
 
786
    \braid@eh=-\braid@eh
 
787
    \braid@increase@leveltrue
 
788
    \braid@process@start
1033
789
}
1034
790
%    \end{macrocode}
1035
791
% \end{macro}
1051
807
%    \begin{macrocode}
1052
808
\newif\ifbraid@increase@level
1053
809
%    \end{macrocode}
1054
 
% An if to decide whether label indices should be absolute or not
1055
 
%    \begin{macrocode}
1056
 
\newif\ifbraid@strand@labels@origin
1057
 
%    \end{macrocode}
1058
 
%
1059
810
%
1060
811
% Some initial values
1061
812
%    \begin{macrocode}
1073
824
% All the keys we'll use.
1074
825
%    \begin{macrocode}
1075
826
\pgfkeys{
1076
 
%    \end{macrocode}
1077
 
% Handle unknown keys by passing them to \Verb+pgf+ and \Verb+tikz+.
1078
 
%    \begin{macrocode}
1079
827
    /tikz/braid/.search also={/pgf},
1080
828
    /pgf/braid/.search also={/pgf,/tikz},
1081
 
%    \end{macrocode}
1082
 
% Our ``namespace'' is \Verb+/pgf/braid+.
1083
 
%    \begin{macrocode}
1084
829
    /pgf/braid/.cd,
1085
830
    number of strands/.initial=0,
1086
831
    height/.initial=1cm,
1087
832
    width/.initial=1cm,
1088
 
    gap/.initial=.1,
1089
833
    border height/.initial=.25cm,
1090
 
    control factor/.initial=.5,
1091
 
    nudge factor/.initial=.05,
1092
834
    name/.code={%
1093
835
      \def\braid@name{#1}%
1094
836
    },
1098
840
    floor command/.code={%
1099
841
      \def\braid@render@floor{#1}%
1100
842
    },
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={%
 
843
    style strand/.code 2 args={%
 
844
      \expandafter\def\csname braid@options@strand@#1\endcsname{#2}
 
845
    },
 
846
    style floor/.code 2 args={%
 
847
      \expandafter\def\csname braid@options@floor@#1\endcsname{#2}
 
848
    },
 
849
    style floors/.code={%
1116
850
      \def\braid@floors@style{#1}
1117
 
    },
1118
 
    strand label/.style={},
1119
 
    strand label by origin/.is if=braid@strand@labels@origin,
 
851
    }
1120
852
}
1121
853
%    \end{macrocode}
1122
 
% \begin{macro}{\braidset}
1123
 
% Shorthand for setting braid-specific keys.
1124
 
%    \begin{macrocode}
1125
 
\def\braidset#1{%
1126
 
  \pgfkeys{/pgf/braid/.cd,#1}}
1127
 
%    \end{macrocode}
1128
 
% \end{macro}
1129
854
% \iffalse
1130
855
%</package>
1131
856
% \fi