Debian Codegraph @

texmf-dist/source/latex/celtic/celtic.dtx

raw
1 % \iffalse meta-comment
2 %<*internal>
3 \iffalse
4 %</internal>
5 %<*readme>
6 ----------------------------------------------------------------
7 celtic --- TikZ library for drawing Celtic knots
8 E-mail: [email protected]
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 is for the generation of Celtic knots starting from
14 a grid of walls.
15 %</readme>
16 %<*internal>
17 \fi
18 \def\nameofplainTeX{plain}
19 \ifx\fmtname\nameofplainTeX\else
20 \expandafter\begingroup
21 \fi
22 %</internal>
23 %<*install>
24 \input docstrip.tex
25 \keepsilent
26 \askforoverwritefalse
27 \preamble
28 ----------------------------------------------------------------
29 celtic --- TikZ library for producing Celtic knots.
30 E-mail: [email protected]
31 Released under the LaTeX Project Public License v1.3c or later
32 See http://www.latex-project.org/lppl.txt
33 ----------------------------------------------------------------
34
35 \endpreamble
36 \postamble
37
38 Copyright (C) 2014 by Andrew Stacey <[email protected]>
39
40 This work may be distributed and/or modified under the
41 conditions of the LaTeX Project Public License (LPPL), either
42 version 1.3c of this license or (at your option) any later
43 version. The latest version of this license is in the file:
44
45 http://www.latex-project.org/lppl.txt
46
47 This work is "maintained" (as per LPPL maintenance status) by
48 Andrew Stacey.
49
50 This work consists of the files celtic.dtx
51 celtic_doc.tex
52 and the derived files celtic.ins
53 celtic_code.pdf
54 tikzlibraryceltic.code.tex
55 celtic.pdf
56 README
57
58 \endpostamble
59 \usedir{tex/latex/celtic}
60 \generate{
61 \file{tikzlibraryceltic.code.tex}{\from{\jobname.dtx}{library}}
62 }
63 %</install>
64 %<install>\endbatchfile
65 %<*internal>
66 \usedir{source/latex/celtic}
67 \generate{
68 \file{\jobname.ins}{\from{\jobname.dtx}{install}}
69 }
70 \nopreamble\nopostamble
71 \usedir{doc/latex/celtic}
72 \generate{
73 \file{README.txt}{\from{\jobname.dtx}{readme}}
74 }
75 \ifx\fmtname\nameofplainTeX
76 \expandafter\endbatchfile
77 \else
78 \expandafter\endgroup
79 \fi
80 %</internal>
81 %<*driver>
82 \documentclass[full]{l3doc}
83 \usepackage[T1]{fontenc}
84 \usepackage{lmodern}
85 \usepackage{tikz}
86 \usepackage{trace}
87 \usetikzlibrary{celtic}
88 %\traceoff
89 %\usepackage[numbered]{hypdoc}
90 \definecolor{lstbgcolor}{rgb}{0.9,0.9,0.9}
91
92 \usepackage{listings}
93 \lstloadlanguages{[LaTeX]TeX}
94 \lstset{breakatwhitespace=true,breaklines=true,language=TeX}
95
96 \usepackage{fancyvrb}
97
98 \newenvironment{example}
99 {\VerbatimEnvironment
100 \begin{VerbatimOut}[gobble=2]{example.out}}
101 {\end{VerbatimOut}
102 \begin{center}
103 % \setlength{\parindent}{0pt}
104 \fbox{\begin{minipage}{.9\linewidth}
105 \lstset{breakatwhitespace=true,breaklines=true,language=TeX,basicstyle=\small}
106 \lstinputlisting[]{example.out}
107 \end{minipage}}
108
109 \fbox{\begin{minipage}{.9\linewidth}
110 \input{example.out}
111 \end{minipage}}
112 \end{center}
113 }
114 \EnableCrossrefs
115 \CodelineIndex
116 \RecordChanges
117 \begin{document}
118 \DocInput{\jobname.dtx}
119 \end{document}
120 %</driver>
121 % \fi
122 %
123 % \CheckSum{783}
124 %
125 % \CharacterTable
126 % {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
127 % 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
128 % Digits \0\1\2\3\4\5\6\7\8\9
129 % Exclamation \! Double quote \" Hash (number) \#
130 % Dollar \$ Percent \% Ampersand \&
131 % Acute accent \' Left paren \( Right paren \)
132 % Asterisk \* Plus \+ Comma \,
133 % Minus \- Point \. Solidus \/
134 % Colon \: Semicolon \; Less than \<
135 % Equals \= Greater than \> Question mark \?
136 % Commercial at \@ Left bracket \[ Backslash \\
137 % Right bracket \] Circumflex \^ Underscore \_
138 % Grave accent \` Left brace \{ Vertical bar \|
139 % Right brace \} Tilde \~}
140 %
141 %
142 % \changes{1.0}{2014/05/23}{Converted to DTX file}
143 %
144 % \DoNotIndex{\newcommand,\newenvironment}
145 %
146 % \providecommand*{\url}{\texttt}
147 % \title{The \textsf{celtic} package}
148 % \author{Andrew Stacey \\ \url{[email protected]}}
149 % \date{1.1 from 2016/02/19}
150 %
151 %
152 % \maketitle
153 %
154 %
155 % \section{Introduction}
156 %
157 % This is a TikZ library for drawing Celtic knot diagrams.
158 % For user documentation, see the \Verb+celtic.pdf+ file.
159 %
160 % \StopEventually{}
161 %
162 % \section{Implementation}
163 %
164 % \iffalse
165 %<*library>
166 % \fi
167 % \subsection{Initialisation}
168 %
169 % Load the \LaTeX3 basics ...
170 % \begin{macrocode}
171 \usepackage{expl3}
172 \usepackage{xparse}
173 % \end{macrocode}
174 % ... and enter the Realm of the 3rd \LaTeX.
175 % \begin{macrocode}
176 \ExplSyntaxOn
177 % \end{macrocode}
178 % Wrapper around \Verb+\tikz@scan@one@point+ for the \Verb+add=<coord>+ key.
179 % \begin{macrocode}
180 \cs_new_nopar:Npn \celtic_shift:n #1
181 {
182 \use:c{tikz@scan@one@point}\pgftransformshift #1\relax
183 }
184 % \end{macrocode}
185 %
186 % We need one or two variables ...
187 % \begin{macrocode}
188 \int_new:N \l__celtic_max_steps_int
189 \int_new:N \l__celtic_int
190 \int_new:N \l__celtic_flip_int
191 \int_new:N \l__celtic_width_int
192 \int_new:N \l__celtic_height_int
193 \int_new:N \l__celtic_x
194 \int_new:N \l__celtic_y
195 \int_new:N \l__celtic_dx
196 \int_new:N \l__celtic_dy
197 \int_new:N \l__celtic_ox
198 \int_new:N \l__celtic_oy
199 \int_new:N \l__celtic_lout
200 \int_new:N \l__celtic_cross_int
201 \int_new:N \l__celtic_component_int
202 \fp_new:N \l__celtic_clip_fp
203 \fp_new:N \l__celtic_inner_clip_fp
204 \fp_new:N \l__celtic_inner_fp
205 \fp_new:N \l__celtic_outer_fp
206 \seq_new:N \l__celtic_path_seq
207 \seq_new:N \l__celtic_overpath_seq
208 \seq_new:N \l__celtic_component_seq
209 \seq_new:N \l__celtic_crossing_seq
210 \seq_new:N \l__celtic_tmpa_seq
211 \clist_new:N \l__celtic_tmpa_clist
212 \tl_new:N \l__celtic_tmpa_tl
213 \tl_new:N \l__celtic_path_tl
214 %\tl_new:N \c__celtic_colon_tl
215 \tl_new:N \l__celtic_bar_tl
216 \tl_new:N \l__celtic_active_bar_tl
217 \tl_new:N \l__celtic_start_tl
218 \bool_new:N \l__celtic_bounce_bool
219 \bool_new:N \l__celtic_pbounce_bool
220
221 \cs_new_nopar:Npn \tl_split_after:Nnn #1#2#3
222 {
223 \cs_set:Npn \tl_split_aux:nnn ##1#3##2 \q_stop: {#3##2}
224 \tl_set:Nx #1 {\tl_split_aux:nnn #2 \q_stop:}
225 }
226 \cs_generate_variant:Nn \tl_split_after:Nnn {NVn}
227 \cs_new_nopar:Npn \tl_split_before:Nnn #1#2#3
228 {
229 \cs_set:Npn \tl_split_aux:nnn ##1#3##2 \q_stop: {##1#3}
230 \tl_set:Nx #1 {\tl_split_aux:nnn #2 \q_stop:}
231 }
232 \cs_generate_variant:Nn \tl_split_before:Nnn {NVn}
233 % \end{macrocode}
234 % Define our warning message.
235 % \begin{macrocode}
236 \msg_new:nnnn { celtic } { max~ steps } { Limit~ of~ number~ of~ steps~ exceeded~ \msg_line_context:.}
237 { Paths~ may~ not~ be~ correctly~ constructed.~
238 Consider~ raising~ the~ limit~ from \int_use:N \l__celtic_max_steps_int.}
239 % \end{macrocode}
240 % Using a colon for a range separator was possibly not the best idea I ever had, seeing as \LaTeX3 alters its catcode.
241 % So we need to get creative.
242 % \begin{macrocode}
243 \tl_const:Nx \c__celtic_colon_tl { \token_to_str:N : }
244 % \end{macrocode}
245 % Some packages mess with the catcode of \Verb+|+.
246 % \begin{macrocode}
247 \tl_set:Nn \l__celtic_bar_tl {|}
248 \group_begin:
249 \char_set_catcode_active:N \|
250 \tl_gset:Nn \l__celtic_active_bar_tl {|}
251 \group_end:
252 % \end{macrocode}
253 % We need a few variants of standard \LaTeX3 functions.
254 % \begin{macrocode}
255 \cs_generate_variant:Nn \tl_if_single_p:N {c}
256 \cs_generate_variant:Nn \tl_if_single:NTF {cTF}
257 \cs_generate_variant:Nn \tl_if_eq:nnTF {xnTF}
258 \cs_generate_variant:Nn \tl_head:N {c}
259 \cs_generate_variant:Nn \tl_tail:N {c}
260 \cs_generate_variant:Nn \tl_if_eq:nnTF {vnTF}
261 \cs_generate_variant:Nn \tl_if_in:nnTF {nVTF}
262 % \end{macrocode}
263 % Initialise a few variables.
264 % \begin{macrocode}
265 \int_set:Nn \l__celtic_max_steps_int {20}
266 \fp_set:Nn \l__celtic_inner_fp {1}
267 \fp_set:Nn \l__celtic_outer_fp {2}
268 % \end{macrocode}
269 %
270 % The following functions are for parsing and setting the crossing information.
271 % \begin{macro}{\celtic_do_crossing:nnn}
272 % This function sets the information for a particular crossing.
273 % The first argument can be empty, meaning ``ignore this crossing as a starting point'', or it should be one of \Verb+|+ or \Verb+-+ to denote the wall type that is placed at this crossing.
274 % \begin{macrocode}
275 \cs_new_nopar:Npn \celtic_do_crossing:nnn #1#2#3
276 {
277 \tl_if_empty:nTF {#1}
278 {
279 \tl_clear:c {crossing used \int_eval:n {#2} - \int_eval:n {#3}}
280 }
281 {
282 \tl_set:cn {crossing \int_eval:n {#2} - \int_eval:n{#3}}{#1}
283 }
284 }
285 % \end{macrocode}
286 % \end{macro}
287 %
288 % \begin{macro}{\celtic_maybe_symmetric:nnnn}
289 % If a crossing is designated as symmetric, we repeat the action four times.
290 % This macro tests to see if it is symmetric or not and acts accordingly.
291 % \begin{macrocode}
292 \cs_new_nopar:Npn \celtic_maybe_symmetric:nnnn #1#2#3#4
293 {
294 \tl_if_empty:nTF {#1}
295 {
296 \celtic_do_crossing:nnn {#2}{#3}{#4}
297 }
298 {
299 \celtic_do_crossing:nnn {#2}{#3}{#4}
300 \celtic_do_crossing:nnn {#2}{\l__celtic_width_int - #3}{#4}
301 \celtic_do_crossing:nnn {#2}{#3}{\l__celtic_height_int - #4}
302 \celtic_do_crossing:nnn {#2}{\l__celtic_width_int - #3}{\l__celtic_height_int - #4}
303 }
304 }
305 % \end{macrocode}
306 % \end{macro}
307 %
308 % \begin{macro}{\celtic_maybe_xrange:nnnn}
309 % The \Verb+x+-coordinate might be a range.
310 % If it is, it contains a colon (with the normal catcode).
311 % So we test for a colon and act accordingly.
312 % \begin{macrocode}
313 \cs_new_nopar:Npn \celtic_maybe_xrange:nnnn #1#2#3#4
314 {
315 \tl_if_in:nVTF {#3} \c__celtic_colon_tl
316 {
317 \celtic_do_xrange:w {#1}{#2}#3\q_stop{#4}
318 }
319 {
320 \celtic_maybe_yrange:nnnn {#1}{#2}{#3}{#4}
321 }
322 }
323 % \end{macrocode}
324 % \end{macro}
325 %
326 % \begin{macro}{\celtic_maybe_yrange:nnnn}
327 % Same with the \Verb+y+-coordinate.
328 % \begin{macrocode}
329 \cs_new_nopar:Npn \celtic_maybe_yrange:nnnn #1#2#3#4
330 {
331 \tl_if_in:nVTF {#4} \c__celtic_colon_tl
332 {
333 \celtic_do_yrange:w {#1}{#2}{#3}#4\q_stop
334 }
335 {
336 \celtic_maybe_symmetric:nnnn {#1}{#2}{#3}{#4}
337 }
338 }
339 % \end{macrocode}
340 % \end{macro}
341 %
342 % When processing ranges, we need to use colons with the original catcode.
343 % We've stored one in \Verb+\c__celtic_colon_tl+ but we need to use it in actuality.
344 % So we make a token list containing the definitions we want to make, expanding \Verb+\c__celtic_colon_tl+ to its colon, but not expanding anything else.
345 % \begin{macrocode}
346 \tl_set:Nx \l_tmpa_tl
347 {
348 % \end{macrocode}
349 %
350 % \begin{macro}{\celtic_do_xrange:w}
351 % This splits the \Verb+x+-coordinate into a range and repeats the function for each intermediate value.
352 % \begin{macrocode}
353 \exp_not:N \cs_new_nopar:Npn \exp_not:N \celtic_do_xrange:w ##1##2##3\tl_use:N \c__celtic_colon_tl ##4\exp_not:N \q_stop##5
354 {
355 \exp_not:N \int_step_inline:nnnn {##3} {2} {##4}
356 {
357 \exp_not:N \celtic_maybe_yrange:nnnn {##1}{##2} {####1}{##5}
358 }
359 }
360 % \end{macrocode}
361 % \end{macro}
362 %
363 % \begin{macro}{\celtic_do_yrange:w}
364 % Same, for the \Verb+y+-coordinate.
365 % \begin{macrocode}
366 \exp_not:N \cs_new_nopar:Npn \exp_not:N \celtic_do_yrange:w ##1##2##3##4\tl_use:N \c__celtic_colon_tl ##5\exp_not:N \q_stop
367 {
368 \exp_not:N \int_step_inline:nnnn {##4} {2} {##5}
369 {
370 \exp_not:N \celtic_maybe_symmetric:nnnn {##1}{##2}{##3}{####1}
371 }
372 }
373 }
374 % \end{macrocode}
375 % \end{macro}
376 %
377 % Now we use the above token list to make our definitions with the right colon in them.
378 % \begin{macrocode}
379 \tl_use:N \l_tmpa_tl
380 % \end{macrocode}
381 %
382 % The next functions are those that take the individual crossing specifications from the key/value list and begin the process of converting the data to an action to be taken for a specific crossing.
383 % \begin{macro}{\celtic_ignore_crossings:w}
384 % \begin{macrocode}
385 \cs_new_nopar:Npn \celtic_ignore_crossings:w #1,#2\q_stop
386 {
387 \celtic_maybe_xrange:nnnn {}{}{#1}{#2}
388 }
389 % \end{macrocode}
390 % \end{macro}
391 %
392 % \begin{macro}{\celtic_ignore_symmetric_crossings:w}
393 % \begin{macrocode}
394 \cs_new_nopar:Npn \celtic_ignore_symmetric_crossings:w #1,#2\q_stop
395 {
396 \celtic_maybe_xrange:nnnn {s}{}{#1}{#2}
397 }
398 % \end{macrocode}
399 % \end{macro}
400 %
401 % \begin{macro}{\celtic_set_crossings:w}
402 % \begin{macrocode}
403 \cs_new_nopar:Npn \celtic_set_crossings:w #1,#2,#3\q_stop
404 {
405 \celtic_maybe_xrange:nnnn {}{#3}{#1}{#2}
406 }
407 % \end{macrocode}
408 % \end{macro}
409 %
410 % \begin{macro}{\celtic_set_symmetric_crossings:w}
411 % \begin{macrocode}
412 \cs_new_nopar:Npn \celtic_set_symmetric_crossings:w #1,#2,#3\q_stop
413 {
414 \celtic_maybe_xrange:nnnn {s}{#3}{#1}{#2}
415 }
416 % \end{macrocode}
417 % \end{macro}
418 %
419 % \begin{macro}{\celtic_next_crossing:}
420 % This is the function that does all the work.
421 % Starting from an undercrossing, it computes the segment leading to the next undercrossing working out all of the ``bounces'' on the way.
422 % \begin{macrocode}
423 \cs_new_nopar:Npn \celtic_next_crossing:
424 {
425 % \end{macrocode}
426 % Clear our starting conditions.
427 % \begin{macrocode}
428 \int_zero:N \l__celtic_cross_int
429 \tl_clear:N \l__celtic_crossing_tl
430 \tl_clear:N \l__celtic_path_tl
431 \tl_clear:N \l__celtic_overpath_tl
432 \bool_set_false:N \l__celtic_bounce_tl
433 \tl_set:Nx \l__celtic_start_tl {(\int_use:N \l__celtic_x, \int_use:N \l__celtic_y)}
434 % \end{macrocode}
435 % Start our path with a move to the initial point and record our current direction.
436 % \begin{macrocode}
437 \tl_put_right:Nx \l__celtic_path_tl {(\int_use:N \l__celtic_x, \int_use:N \l__celtic_y)}
438 \int_set:Nn \l__celtic_lout {(90 - \l__celtic_dx * 45) * \l__celtic_dy}
439 % \end{macrocode}
440 % We loop until we get to the second crossing on the path (the first will be the overpass).
441 % \begin{macrocode}
442 \bool_do_until:nn {\int_compare_p:n {\l__celtic_cross_int > 1}}
443 {
444 % \end{macrocode}
445 % We keep a record of whether the last bit contained a bounce.
446 % \begin{macrocode}
447 \bool_set_eq:NN \l__celtic_pbounce_bool \l__celtic_bounce_bool
448 \bool_set_false:N \l__celtic_bounce_bool
449 % \end{macrocode}
450 % Move to the next point in our current direction.
451 % \begin{macrocode}
452 \int_add:Nn \l__celtic_x {\l__celtic_dx}
453 \int_add:Nn \l__celtic_y {\l__celtic_dy}
454 % \end{macrocode}
455 % Now we look to see if we should bounce.
456 % Is the crossing defined?
457 % \begin{macrocode}
458 \tl_if_exist:cT {crossing \int_use:N \l__celtic_x - \int_use:N \l__celtic_y}
459 {
460 % \end{macrocode}
461 % Yes, so we bounce.
462 % But which way?
463 % \begin{macrocode}
464 \tl_if_eq:cNTF {crossing \int_use:N \l__celtic_x - \int_use:N \l__celtic_y} \l__celtic_bar_tl
465 {
466 % \end{macrocode}
467 % Vertical wall.
468 % Have we just bounced?
469 % \begin{macrocode}
470 \bool_if:NTF \l__celtic_pbounce_bool
471 {
472 % \end{macrocode}
473 % Yes, so the next part of the path is a right angle.
474 % \begin{macrocode}
475 \tl_put_right:Nn \l__celtic_path_tl { -| }
476 }
477 {
478 % \end{macrocode}
479 % No, so the next part of the path is a curve.
480 % (This is where we use the direction that we recorded earlier.)
481 % \begin{macrocode}
482 \tl_put_right:Nx \l__celtic_path_tl { to[out=\int_eval:n
483 {(90 - 45*\l__celtic_dx)*\l__celtic_dy}, in=\int_eval:n
484 {-90*\l__celtic_dy}] }
485 }
486 % \end{macrocode}
487 % We record the new direction and ``bounce'' our direction vector.
488 % Then we add our new point to the path (which, due to the bounce, is offset).
489 % \begin{macrocode}
490 \int_set:Nn \l__celtic_lout {90*\l__celtic_dy}
491 \int_set:Nn \l__celtic_dx {-\l__celtic_dx}
492 \tl_put_right:Nx \l__celtic_path_tl {(\fp_eval:n {\int_use:N \l__celtic_x + .5 * \int_use:N \l__celtic_dx}, \int_use:N \l__celtic_y)}
493 % \end{macrocode}
494 % We bounced, so record that too.
495 % \begin{macrocode}
496 \bool_set_true:N \l__celtic_bounce_bool
497 }
498 {
499 % \end{macrocode}
500 % At this point, we've bounced but our bounce was horizontal so we do the same as for the vertical but all turned round.
501 % \begin{macrocode}
502 \bool_if:NTF \l__celtic_pbounce_bool
503 {
504 % \end{macrocode}
505 % We're out from a bounce, so turn at right angles.
506 % \begin{macrocode}
507 \tl_put_right:Nn \l__celtic_path_tl { |- }
508 }
509 {
510 % \end{macrocode}
511 % We're not out from a bounce, so we curve ...
512 % \begin{macrocode}
513 \tl_put_right:Nx \l__celtic_path_tl { to[out=\int_eval:n {(90 - 45*\l__celtic_dx)*\l__celtic_dy}, in=\int_eval:n {90 + 90*\l__celtic_dx}] }
514 }
515 % \end{macrocode}
516 % ... and record our new direction and out angle.
517 % \begin{macrocode}
518 \int_set:Nn \l__celtic_lout {90-90*\l__celtic_dx}
519 \int_set:Nn \l__celtic_dy {-\l__celtic_dy}
520 % \end{macrocode}
521 % Now we add our new position (adjusted from the bounce) to the path.
522 % \begin{macrocode}
523 \tl_put_right:Nx \l__celtic_path_tl {(\int_use:N \l__celtic_x, \fp_eval:n {\int_use:N \l__celtic_y + .5 * \int_use:N \l__celtic_dy})}
524 % \end{macrocode}
525 % And record the fact that we've bounced.
526 % \begin{macrocode}
527 \bool_set_true:N \l__celtic_bounce_bool
528 }
529 }
530 % \end{macrocode}
531 % Now we check to see if we're at the edge of the rectangle, starting with the left.
532 % \begin{macrocode}
533 \int_compare:nT {\l__celtic_x == 0}
534 {
535 % \end{macrocode}
536 % Yes, so treat this as a vertical bounce.
537 % \begin{macrocode}
538 \bool_if:NTF \l__celtic_pbounce_bool
539 {
540 % \end{macrocode}
541 % Previous bounce, so right angle.
542 % \begin{macrocode}
543 \tl_put_right:Nn \l__celtic_path_tl { -| }
544 }
545 {
546 % \end{macrocode}
547 % No previous bounce, so curve.
548 % \begin{macrocode}
549 \tl_put_right:Nx \l__celtic_path_tl { to[out=\int_eval:n {(90 - 45*\l__celtic_dx)*\l__celtic_dy}, in=\int_eval:n {-90*\l__celtic_dy}] }
550 }
551 % \end{macrocode}
552 % Record our out angle and change our direction.
553 % \begin{macrocode}
554 \int_set:Nn \l__celtic_lout {90*\l__celtic_dy}
555 \int_set:Nn \l__celtic_dx {-\l__celtic_dx}
556 % \end{macrocode}
557 % Add the correct position to the path.
558 % \begin{macrocode}
559 \tl_put_right:Nx \l__celtic_path_tl {(\fp_eval:n {\int_use:N \l__celtic_x + .5 * \int_use:N \l__celtic_dx}, \int_use:N \l__celtic_y)}
560 % \end{macrocode}
561 % We've bounced.
562 % \begin{macrocode}
563 \bool_set_true:N \l__celtic_bounce_bool
564 }
565 % \end{macrocode}
566 % Same for the right-hand edge.
567 % \begin{macrocode}
568 \int_compare:nT {\l__celtic_x == \l__celtic_width_int}
569 {
570 \bool_if:NTF \l__celtic_pbounce_bool
571 {
572 \tl_put_right:Nn \l__celtic_path_tl { -| }
573 }
574 {
575 \tl_put_right:Nx \l__celtic_path_tl { to[out=\int_eval:n {(90 - 45*\l__celtic_dx)*\l__celtic_dy}, in=\int_eval:n {-90*\l__celtic_dy}] }
576 }
577 \int_set:Nn \l__celtic_lout {90*\l__celtic_dy}
578 \int_set:Nn \l__celtic_dx {-\l__celtic_dx}
579 \tl_put_right:Nx \l__celtic_path_tl {(\fp_eval:n {\int_use:N \l__celtic_x + .5 * \int_use:N \l__celtic_dx}, \int_use:N \l__celtic_y)}
580 \bool_set_true:N \l__celtic_bounce_bool
581 }
582 % \end{macrocode}
583 % Now the lower edge.
584 % \begin{macrocode}
585 \int_compare:nT {\l__celtic_y == 0}
586 {
587 \bool_if:NTF \l__celtic_pbounce_bool
588 {
589 \tl_put_right:Nn \l__celtic_path_tl { |- }
590 }
591 {
592 \tl_put_right:Nx \l__celtic_path_tl { to[out=\int_eval:n {(90 - 45*\l__celtic_dx)*\l__celtic_dy}, in=\int_eval:n {90 + 90*\l__celtic_dx}] }
593 }
594 \int_set:Nn \l__celtic_lout {90-90*\l__celtic_dx}
595 \int_set:Nn \l__celtic_dy {-\l__celtic_dy}
596 \tl_put_right:Nx \l__celtic_path_tl {(\int_use:N \l__celtic_x, \fp_eval:n {\int_use:N \l__celtic_y + .5 * \int_use:N \l__celtic_dy})}
597 \bool_set_true:N \l__celtic_bounce_bool
598 }
599 % \end{macrocode}
600 % And the upper edge.
601 % \begin{macrocode}
602 \int_compare:nT {\l__celtic_y == \l__celtic_height_int}
603 {
604 \bool_if:NTF \l__celtic_pbounce_bool
605 {
606 \tl_put_right:Nn \l__celtic_path_tl { |- }
607 }
608 {
609 \tl_put_right:Nx \l__celtic_path_tl { to[out=\int_eval:n {(90 - 45*\l__celtic_dx)*\l__celtic_dy}, in=\int_eval:n {90 + 90*\l__celtic_dx}] }
610 }
611 \int_set:Nn \l__celtic_lout {-90+90*\l__celtic_dx}
612 \int_set:Nn \l__celtic_dy {-\l__celtic_dy}
613 \tl_put_right:Nx \l__celtic_path_tl {(\int_use:N \l__celtic_x, \fp_eval:n {\int_use:N \l__celtic_y + .5 * \int_use:N \l__celtic_dy})}
614 \bool_set_true:N \l__celtic_bounce_bool
615 }
616 % \end{macrocode}
617 % Did we bounce this time?
618 % \begin{macrocode}
619 \bool_if:NF \l__celtic_bounce_bool
620 {
621 % \end{macrocode}
622 % Did we bounce last time?
623 % \begin{macrocode}
624 \bool_if:NTF \l__celtic_pbounce_bool
625 {
626 % \end{macrocode}
627 % Yes, so the second half is a curve.
628 % \begin{macrocode}
629 \tl_put_right:Nx \l__celtic_path_tl { to[out=\int_use:N \l__celtic_lout,in=\int_eval:n {(-90 - 45 * \l__celtic_dx)*\l__celtic_dy}] }
630 }
631 {
632 % \end{macrocode}
633 % No, so the second half is a straight line.
634 % \begin{macrocode}
635 \tl_put_right:Nn \l__celtic_path_tl { -- }
636 }
637 % \end{macrocode}
638 % The next crossing.
639 % \begin{macrocode}
640 \tl_put_right:Nx \l__celtic_path_tl { (\int_use:N \l__celtic_x, \int_use:N \l__celtic_y)}
641 % \end{macrocode}
642 % If we haven't already gone over a crossing, this is our overcrossing.
643 % \begin{macrocode}
644 \tl_if_empty:NTF \l__celtic_crossing_tl
645 {
646 % \end{macrocode}
647 % So we record this as our overcrossing.
648 % \begin{macrocode}
649 \tl_set:Nx \l__celtic_crossing_tl {(\int_use:N \l__celtic_x, \int_use:N \l__celtic_y)}
650 }
651 {
652 % \end{macrocode}
653 % Otherwise, it's the undercrossing so we note that we've visited this one.
654 % \begin{macrocode}
655 \tl_clear:c {crossing used \int_use:N \l__celtic_x - \int_use:N \l__celtic_y}
656 }
657 % \end{macrocode}
658 % Increment the crossing count.
659 % \begin{macrocode}
660 \int_incr:N \l__celtic_cross_int
661 % \end{macrocode}
662 % Record our outward angle.
663 % \begin{macrocode}
664 \int_set:Nn \l__celtic_lout {(90 - \l__celtic_dx * 45) * \l__celtic_dy}
665 }
666 }
667 % \end{macrocode}
668 % Is our overcrossing one of the undercrossings?
669 % If so, remove the initial or final segment as appropriate.
670 % \begin{macrocode}
671 \tl_set_eq:NN \l__celtic_overpath_tl \l__celtic_path_tl
672 \tl_set:Nx \l__celtic_tmpa_tl {(\int_use:N \l__celtic_x, \int_use:N
673 \l__celtic_y)}
674 \tl_if_eq:NNT \l__celtic_crossing_tl \l__celtic_tmpa_tl
675 {
676 \tl_reverse:N \l__celtic_overpath_tl
677 \tl_set:Nx \l__celtic_overpath_tl {\tl_tail:N \l__celtic_overpath_tl}
678 \tl_split_after:NVn \l__celtic_overpath_tl \l__celtic_overpath_tl {)}
679 \tl_reverse:N \l__celtic_overpath_tl
680 }
681 \tl_if_eq:NNT \l__celtic_crossing_tl \l__celtic_start_tl
682 {
683 \tl_set:Nx \l__celtic_overpath_tl {\tl_tail:N \l__celtic_overpath_tl}
684 \tl_split_after:NVn \l__celtic_overpath_tl \l__celtic_overpath_tl {(}
685 }
686 }
687 % \end{macrocode}
688 % \end{macro}
689 %
690 % Now we set up the keys we'll use.
691 % \begin{macrocode}
692 \keys_define:nn { celtic }
693 {
694 % \end{macrocode}
695 % This sets the maximum number of steps in a path.
696 % \begin{macrocode}
697 max~ steps .int_set:N = \l__celtic_max_steps_int,
698 % \end{macrocode}
699 % This flips the over/under crossings.
700 % \begin{macrocode}
701 flip .code:n = {
702 \int_set:Nn \l__celtic_flip_int {-1}
703 },
704 % \end{macrocode}
705 % These set the size of the knot.
706 % \begin{macrocode}
707 width .int_set:N = \l__celtic_width_int,
708 height .int_set:N = \l__celtic_height_int,
709 size .code:n = {
710 % \end{macrocode}
711 % The size is a CSV so we use a \Verb+clist+ to separate it.
712 % \begin{macrocode}
713 \clist_set:Nn \l__celtic_tmpa_clist {#1}
714 \clist_pop:NN \l__celtic_tmpa_clist \l__celtic_tmpa_tl
715 \int_set:Nn \l__celtic_width_int {\l__celtic_tmpa_tl}
716 \clist_pop:NN \l__celtic_tmpa_clist \l__celtic_tmpa_tl
717 \int_set:Nn \l__celtic_height_int {\l__celtic_tmpa_tl}
718 },
719 % \end{macrocode}
720 % The size keys are placed in a separate group to make it possible to process them before all other keys.
721 % \begin{macrocode}
722 width .groups:n = { size },
723 height .groups:n = { size },
724 size .groups:n = { size },
725 % \end{macrocode}
726 % The next keys set the various crossing behaviours.
727 % \begin{macrocode}
728 crossings .code:n = {
729 \seq_set_split:Nnn \l__celtic_tmpa_seq {;} {#1}
730 \seq_map_inline:Nn \l__celtic_tmpa_seq {
731 \tl_if_empty:nF {##1}
732 {
733 \celtic_set_crossings:w ##1 \q_stop
734 }
735 }
736 },
737 % \end{macrocode}
738 %
739 % \begin{macrocode}
740 symmetric~ crossings .code:n = {
741 \seq_set_split:Nnn \l__celtic_tmpa_seq {;} {#1}
742 \seq_map_inline:Nn \l__celtic_tmpa_seq {
743 \tl_if_empty:nF {##1}
744 {
745 \celtic_set_symmetric_crossings:w ##1 \q_stop
746 }
747 }
748 },
749 % \end{macrocode}
750 %
751 % \begin{macrocode}
752 ignore~ crossings .code:n ={
753 \seq_set_split:Nnn \l__celtic_tmpa_seq {;} {#1}
754 \seq_map_inline:Nn \l__celtic_tmpa_seq {
755 \tl_if_empty:nF {##1}
756 {
757 \celtic_ignore_crossings:w ##1 \q_stop
758 }
759 }
760 },
761 % \end{macrocode}
762 %
763 % \begin{macrocode}
764 ignore~ symmetric~ crossings .code:n ={
765 \seq_set_split:Nnn \l__celtic_tmpa_seq {;} {#1}
766 \seq_map_inline:Nn \l__celtic_tmpa_seq {
767 \tl_if_empty:nF {##1}
768 {
769 \celtic_ignore_symmetric_crossings:w ##1 \q_stop
770 }
771 }
772 },
773 % \end{macrocode}
774 % The \Verb+style+ key is passed on to \Verb+\tikzset+.
775 % \begin{macrocode}
776 style .code:n = {
777 \tikzset {#1}
778 },
779 % \end{macrocode}
780 % This relocates the diagram.
781 % \begin{macrocode}
782 at .code:n = {
783 \celtic_shift:n {#1}
784 },
785 % \end{macrocode}
786 % These set the margin for the clip regions.
787 % \begin{macrocode}
788 inner~ clip .fp_set:N = \l__celtic_inner_fp,
789 outer~ clip .fp_set:N = \l__celtic_outer_fp,
790 }
791 % \end{macrocode}
792 %
793 % \begin{macro}{\CelticDrawPath}
794 % This is the user macro.
795 % Its mandatory argument is a list of key/value pairs.
796 % \begin{macrocode}
797 \DeclareDocumentCommand \CelticDrawPath { m }
798 {
799 % \end{macrocode}
800 % Get a nice clean initial state.
801 % \begin{macrocode}
802 \group_begin:
803 \pgfscope
804 \seq_clear:N \l__celtic_path_seq
805 \seq_clear:N \l__celtic_overpath_seq
806 \seq_clear:N \l__celtic_component_seq
807 \seq_clear:N \l__celtic_crossing_seq
808 \int_set:Nn \l__celtic_flip_int {1}
809 % \end{macrocode}
810 % Figure out if \Verb+|+ is active or not (\Verb+fancyvrb+ sets it active).
811 % \begin{macrocode}
812 \int_compare:nT {\char_value_catcode:n {`\|} = 13}
813 {
814 \tl_set_eq:NN \l__celtic_bar_tl \l__celtic_active_bar_tl
815 }
816 % \end{macrocode}
817 % Clear all the crossing data.
818 % \begin{macrocode}
819 \int_step_inline:nnnn {1} {1} {\l__celtic_height_int-1}
820 {
821 \int_step_inline:nnnn {1 + \int_mod:nn {##1}{2}} {2} {\l__celtic_width_int-1}
822 {
823 \tl_clear_new:c {crossing used ####1 - ##1}
824 \tl_set:cn {crossing used ####1 - ##1} {X}
825 }
826 }
827 % \end{macrocode}
828 % Process the keys relating to the size of the knot.
829 % \begin{macrocode}
830 \keys_set_groups:nnn { celtic } { size } {#1}
831 % \end{macrocode}
832 % Process all other keys.
833 % \begin{macrocode}
834 \keys_set_filter:nnn { celtic } { size } {#1}
835 % \end{macrocode}
836 % Draw (maybe) the outer boundary.
837 % \begin{macrocode}
838 \path[celtic~ bar/.try, celtic~ surround/.try] (0,0) rectangle (\int_use:N \l__celtic_width_int, \int_use:N \l__celtic_height_int);
839 % \end{macrocode}
840 % Draw (maybe) the crossings.
841 % \begin{macrocode}
842 \int_step_inline:nnnn {1} {1} {\l__celtic_height_int-1}
843 {
844 \int_step_inline:nnnn {1 + \int_mod:nn {##1}{2}} {2} {\l__celtic_width_int-1}
845 {
846 \tl_if_exist:cT {crossing ####1 - ##1}
847 {
848 \tl_if_eq:cNTF {crossing ####1 - ##1} \l__celtic_bar_tl
849 {
850 % \end{macrocode}
851 % Vertical crossing.
852 % \begin{macrocode}
853 \path[celtic~ bar/.try] (####1,##1-1) -- (####1,##1+1);
854 }
855 {
856 % \end{macrocode}
857 % Horizontal crossing.
858 % \begin{macrocode}
859 \path[celtic~ bar/.try] (####1-1,##1) -- (####1+1,##1);
860 }
861 }
862 }
863 }
864 % \end{macrocode}
865 % Now we work through the crossings, trying to generate a path starting at each one.
866 % The crossings are at points \((x,y)\) with \(x + y\) odd.
867 % \begin{macrocode}
868 \int_step_inline:nnnn {1} {1} {\l__celtic_height_int-1}
869 {
870 \int_step_inline:nnnn {1 + \int_mod:nn {##1}{2}} {2} {\l__celtic_width_int-1}
871 {
872 % \end{macrocode}
873 % Attempt to generate a path starting from that crossing.
874 % The third argument is to indicate which way the under-path goes from that crossing.
875 % \begin{macrocode}
876 \celtic_generate_path:nnx {####1}{##1}{\int_eval:n {\l__celtic_flip_int*(2*\int_mod:nn{####1}{2} - 1)}}
877 }
878 }
879 % \end{macrocode}
880 % Once we have generated our paths, we render them and close our scope and group.
881 % \begin{macrocode}
882 \celtic_render_path:
883 \endpgfscope
884 \group_end:
885 }
886 % \end{macrocode}
887 % \end{macro}
888 %
889 % \begin{macro}{\celtic_generate_path:nnn}
890 % This macro generates a sequence of path segments.
891 % \begin{macrocode}
892 \cs_new_nopar:Npn \celtic_generate_path:nnn #1#2#3
893 {
894 % \end{macrocode}
895 % First off, we test to see if the given coordinates are allowed as a starting point.
896 % If the crossing has a wall or it is already marked as ``used'' then it isn't.
897 % \begin{macrocode}
898 \bool_if:nF {
899 \tl_if_exist_p:c {crossing #1 - #2}
900 ||
901 \tl_if_empty_p:c {crossing used #1 - #2}
902 }
903 {
904 % \end{macrocode}
905 % Those tests failed, so we proceed.
906 % First, we mark the crossing as used and set our initial data.
907 % Position, original position, and direction.
908 % \begin{macrocode}
909 \tl_clear:c {crossing used #1 - #2}
910 \int_incr:N \l__celtic_component_int
911 \int_set:Nn \l__celtic_x {#1}
912 \int_set:Nn \l__celtic_y {#2}
913 \int_set_eq:NN \l__celtic_ox \l__celtic_x
914 \int_set_eq:NN \l__celtic_oy \l__celtic_y
915 \int_set:Nn \l__celtic_dx {#3}
916 \int_set:Nn \l__celtic_dy {1}
917 % \end{macrocode}
918 % This holds our recursion index so that we can bail out if we look like we're entering a loop (which we shouldn't).
919 % \begin{macrocode}
920 \int_zero:N \l__celtic_int
921 % \end{macrocode}
922 % We stop the loop if we get back where we started or we hit the maximum recursion limit.
923 % \begin{macrocode}
924 \bool_do_until:nn
925 {
926 (\int_compare_p:n {\l__celtic_x == \l__celtic_ox}
927 &&
928 \int_compare_p:n {\l__celtic_y == \l__celtic_oy}
929 )
930 || \int_compare_p:n {\l__celtic_int > \l__celtic_max_steps_int}
931 }
932 {
933 % \end{macrocode}
934 % Increment our counter.
935 % \begin{macrocode}
936 \int_incr:N \l__celtic_int
937 % \end{macrocode}
938 % Create the segment between this crossing and the next one.
939 % \begin{macrocode}
940 \celtic_next_crossing:
941 % \end{macrocode}
942 % Store the segment, its over-crossing, and its component number.
943 % Then return to the start of the loop.
944 % \begin{macrocode}
945 \seq_put_left:NV \l__celtic_path_seq \l__celtic_path_tl
946 \seq_put_left:NV \l__celtic_overpath_seq \l__celtic_overpath_tl
947 \seq_put_left:NV \l__celtic_crossing_seq \l__celtic_crossing_tl
948 \seq_put_left:NV \l__celtic_component_seq \l__celtic_component_int
949 }
950 % \end{macrocode}
951 % If we hit the maximum number of steps, issue a warning.
952 % \begin{macrocode}
953 \int_compare:nT {\l__celtic_int > \l__celtic_max_steps_int}
954 {
955 \msg_warning:nn {celtic} { max~ steps }
956 }
957 }
958 }
959 % \end{macrocode}
960 % \end{macro}
961 %
962 % \begin{macro}{\celtic_generate_path:nnx}
963 % Useful variant.
964 % \begin{macrocode}
965 \cs_generate_variant:Nn \celtic_generate_path:nnn {nnx}
966 % \end{macrocode}
967 % \end{macro}
968 %
969 % \begin{macro}{\celtic_render_path:}
970 % This takes a generated list of path segments and renders them.
971 % \begin{macrocode}
972 \cs_new_nopar:Npn \celtic_render_path:
973 {
974 % \end{macrocode}
975 % First pass through the sequence of segments.
976 % \begin{macrocode}
977 \seq_map_inline:Nn \l__celtic_path_seq
978 {
979 % \end{macrocode}
980 % We need to get the component number, but \Verb+pop+ removes it from the sequence so we put it back at the other end again.
981 % \begin{macrocode}
982 \seq_pop:NN \l__celtic_component_seq \l__celtic_tmpa_tl
983 \seq_put_right:NV \l__celtic_component_seq \l__celtic_tmpa_tl
984 % \end{macrocode}
985 % Draw the path segment, styling by the component number.
986 % \begin{macrocode}
987 \path[celtic~ path/.try, celtic~ path~ \tl_use:N \l__celtic_tmpa_tl/.try] ##1;
988 }
989 % \end{macrocode}
990 % This next bit of code attempts to work out the true thickness of the presumably doubled path.
991 % We do it in a group and scope to limit its effect.
992 % \begin{macrocode}
993 \group_begin:
994 \pgfscope
995 \tikzset{celtic~ path/.try}
996 \tl_use:c {tikz@double@setup}
997 % \end{macrocode}
998 % This gets the resulting line width outside the group and scope.
999 % \begin{macrocode}
1000 \tl_set:Nn \l__celtic_tmpa_tl
1001 {
1002 \endpgfscope
1003 \group_end:
1004 \fp_set:Nn \l__celtic_clip_fp
1005 }
1006 \tl_put_right:Nx \l__celtic_tmpa_tl {{\dim_use:N \pgflinewidth}}
1007 \tl_use:N \l__celtic_tmpa_tl
1008 % \end{macrocode}
1009 % Now we set the inner and outer clip sizes based on that line width.
1010 % \begin{macrocode}
1011 \fp_set:Nn \l__celtic_inner_clip_fp {sqrt(2) * (\l__celtic_clip_fp + \l__celtic_inner_fp)}
1012 \fp_set:Nn \l__celtic_clip_fp {sqrt(2) * (\l__celtic_clip_fp + \l__celtic_outer_fp)}
1013 % \end{macrocode}
1014 %
1015 % This second pass through the segments redraws each one clipped to a diamond neighbourhood of its over-crossing.
1016 % \begin{macrocode}
1017 \seq_map_inline:Nn \l__celtic_overpath_seq
1018 {
1019 % \end{macrocode}
1020 % We get the crossing coordinate.
1021 % \begin{macrocode}
1022 \seq_pop:NN \l__celtic_crossing_seq \l__celtic_crossing_tl
1023 % \end{macrocode}
1024 % Again, we need the component number.
1025 % \begin{macrocode}
1026 \seq_pop:NN \l__celtic_component_seq \l__celtic_tmpa_tl
1027 \seq_put_right:NV \l__celtic_component_seq \l__celtic_tmpa_tl
1028 \pgfscope
1029 % \end{macrocode}
1030 % This is the smaller of the clip regions.
1031 % \begin{macrocode}
1032 \clip \l__celtic_crossing_tl +(-\fp_to_dim:N \l__celtic_inner_clip_fp,0) -- +(0,\fp_to_dim:N \l__celtic_inner_clip_fp) -- +(\fp_to_dim:N \l__celtic_inner_clip_fp,0) -- +(0,-\fp_to_dim:N \l__celtic_inner_clip_fp) -- +(-\fp_to_dim:N \l__celtic_inner_clip_fp,0);
1033 % \end{macrocode}
1034 % We draw just the background part of the (presumably doubled) path.
1035 % \begin{macrocode}
1036 \path[celtic~ path/.try, celtic~ path~ \tl_use:N \l__celtic_tmpa_tl/.try, double~ background] ##1;
1037 \endpgfscope
1038 \pgfscope
1039 % \end{macrocode}
1040 % Noew we apply the larger clip region.
1041 % \begin{macrocode}
1042 \clip \l__celtic_crossing_tl +(-\fp_to_dim:N \l__celtic_clip_fp,0) -- +(0,\fp_to_dim:N \l__celtic_clip_fp) -- +(\fp_to_dim:N \l__celtic_clip_fp,0) -- +(0,-\fp_to_dim:N \l__celtic_clip_fp) -- +(-\fp_to_dim:N \l__celtic_clip_fp,0);
1043 % \end{macrocode}
1044 % And draw the foreground part.
1045 % \begin{macrocode}
1046 \path[celtic~ path/.try, celtic~ path~ \tl_use:N \l__celtic_tmpa_tl/.try,double~ foreground] ##1;
1047 \endpgfscope
1048 }
1049 }
1050 % \end{macrocode}
1051 % \end{macro}
1052 % We are now leaving \LaTeX3 world.
1053 % \begin{macrocode}
1054 \ExplSyntaxOff
1055 % \end{macrocode}
1056 %
1057 % Clipping with doubled paths isn't perfect when anti-aliasing is used as it produces artefacts where the lower path shows through.
1058 % To get round that, we need to draw the two parts of the doubled path separately.
1059 % The following two keys extract the line widths and colours of the two parts of a doubled path and apply it.
1060 % \begin{macrocode}
1061 \tikzset{
1062 % \end{macrocode}
1063 % This sets the stye to that of the under path.
1064 % \begin{macrocode}
1065 double background/.code={%
1066 \begingroup
1067 \tikz@double@setup
1068 \global\pgf@xa=\pgflinewidth
1069 \endgroup
1070 \expandafter\tikz@semiaddlinewidth\expandafter{\the\pgf@xa}%
1071 \tikz@addmode{\tikz@mode@doublefalse}%
1072 },
1073 % \end{macrocode}
1074 % This to the over path.
1075 % \begin{macrocode}
1076 double foreground/.code={%
1077 \begingroup
1078 \tikz@double@setup
1079 \global\pgf@xa=\pgfinnerlinewidth
1080 \endgroup
1081 \expandafter\tikz@semiaddlinewidth\expandafter{\the\pgf@xa}%
1082 \tikz@addmode{\tikz@mode@doublefalse}%
1083 \tikzset{color=\pgfinnerstrokecolor}%
1084 },
1085 }
1086 % \end{macrocode}
1087 %
1088 % \iffalse
1089 %</library>
1090 % \fi
1091 %\Finale
1092 \endinput