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