summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/causets/causets.sty
blob: 291537cb9e65ce4d85e9bdef2fa7cea9ae028c1d (plain)
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
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{causets}[2022/05/23 v1.3 Package to draw causal set diagrams]
%% Copyright 2020-2022 by C. Minz
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this licence or (at your option) any later version.
% The latest version of this licence is in
% http://www.latex-project.org/lppl.txt
%
% This work has the LPPL maintenance status "maintained".
% 
% The current version of this package is 1.3.
%
% The current maintainer of this work is C. Minz.
% https://github.com/c-minz
%
% This work consists of the files causets.sty, causets.tex,  
% causets.pdf, causet_tikz_example1.tex, causet_tikz_example1.pdf, 
% causet_tikz_example2.tex, causet_tikz_example2.pdf

\RequirePackage{tikz}
\usepgflibrary{arrows.meta}

%% PGF keys and style macros:
\def\tikzcausetsset#1{\pgfqkeys{/tikz/causets}{#1}}
\pgfkeys{/tikz/causets/.search also={/tikz}}
% Size options:
\tikzcausetsset{%
	size unit/.store in=\causetTileSize,
	size unit/.value required,
	tile size/.store in=\causetTileSize,
	tile size/.value required,
	text font/.store in=\causetTextFont,
	text font/.value required,
	text scale/.store in=\causetTextScale,
	text scale/.value required,
	region line width/.initial=0.07*\causetTileSize,
	grid line width/.initial=0.04*\causetTileSize,
	event size/.initial=0.56*\causetTileSize,
	event outline/.initial=0.04*\causetTileSize,
	link width/.initial=0.14*\causetTileSize,
	broken link gap/.initial=0.42*\causetTileSize,
	tiny/.value forbidden,
	tiny/.style={%
		size unit=0.30ex,
		text font=\tiny,
		text scale=0.30},
	very small/.value forbidden,
	very small/.style={%
		size unit=0.40ex,
		text font=\tiny,
		text scale=0.40},
	small/.value forbidden,
	small/.style={%
		size unit=0.60ex,
		text font=\scriptsize,
		text scale=0.43},
	normal/.value forbidden,
	normal/.style={%
		size unit=0.90ex,
		text font=\footnotesize,
		text scale=0.56},
	large/.value forbidden,
	large/.style={%
		size unit=1.35ex,
		text font=\small,
		text scale=0.75},
	very large/.value forbidden,
	very large/.style={%
		size unit=2.025ex,
		text font=\normalsize,
		text scale=1.02},
	huge/.value forbidden,
	huge/.style={%
		size unit=2.70ex,
		text font=\Large,
		text scale=0.97}
}
% Colour options:
\tikzcausetsset{%
	region color/.initial=black!75,
	grid color/.initial=black!25,
	tile color/.initial=black!10!white,
	event color/.initial=black,
	label color/.initial=black,
	link color/.initial=black!65!white,
	spatial link color/.initial=black!50!white,
	ulabel color/.initial=black!65!white,
	vlabel color/.initial=black!65!white,
	black colors/.style={%
		region color=black,
		grid color=black,
		tile color=white,
		event color=black,
		label color=black,
		link color=black,
		spatial link color=black,
		ulabel color=black,
		vlabel color=black},
	gray colors/.style={%
		region color=black!75,
		grid color=black!25,
		tile color=black!10!white,
		event color=black,
		label color=black,
		link color=black!65!white,
		spatial link color=black!50!white,
		ulabel color=black!65!white,
		vlabel color=black!65!white},
	blue colors/.style={%
		region color=black!75,
		grid color=black!25,
		tile color=cyan!25!black!25!white,
		event color=blue!50!black,
		label color=blue!50!black,
		link color=cyan!75!black,
		spatial link color=blue!75!white,
		ulabel color=black!50,
		vlabel color=black!50},
	neon colors/.style={%
		region color=green,
		grid color=green,
		tile color=yellow,
		event color=blue,
		label color=blue,
		link color=cyan,
		spatial link color=red,
		ulabel color=magenta,
		vlabel color=magenta}
}
% Causet modification options:
\tikzcausetsset{%
	offset/.store in=\causet@Offset,
	every causet/.value forbidden,
	every causet/.style={baseline=(baseline point)}
}
% Graphic component switches:
\newif\ifcausetsDrawPermutation
\newif\ifcausetsDrawLinks
\newif\ifcausetsBreakLinks
\newif\ifcausetsDrawSpatialLinks
\newif\ifcausetsDrawLabels
\newif\ifcausetsDrawULabels
\newif\ifcausetsDrawVLabels
\tikzcausetsset{%
	show permutation/.is if=causetsDrawPermutation,
	show links/.is if=causetsDrawLinks,
	break links/.is if=causetsBreakLinks,
	show spatial links/.is if=causetsDrawSpatialLinks,
	show labels/.is if=causetsDrawLabels,
	show ulabels/.is if=causetsDrawULabels,
	show vlabels/.is if=causetsDrawVLabels,
	permutation/.code={\causetsDrawPermutationtrue},
	linked/.code={\causetsDrawLinkstrue},
	unlinked/.code={\causetsDrawLinksfalse},
	brokenly linked/.code={\causetsBreakLinkstrue},
	continuously linked/.code={\causetsBreakLinksfalse},
	spatially linked/.code={\causetsDrawSpatialLinkstrue},
	spatially unlinked/.code={\causetsDrawSpatialLinksfalse},
	labeled/.code={\causetsDrawLabelstrue},
	unlabeled/.code={\causetsDrawLabelsfalse},
	labeled u/.code={\causetsDrawULabelstrue},
	unlabeled u/.code={\causetsDrawULabelsfalse},
	labeled v/.code={\causetsDrawVLabelstrue},
	unlabeled v/.code={\causetsDrawVLabelsfalse}
}
% Permutation grid styles:
\tikzcausetsset{%
	every region/.value forbidden,
	every region/.style={color=\pgfkeysvalueof{/tikz/causets/region color}, line width=\pgfkeysvalueof{/tikz/causets/region line width}},
	region/.style={show permutation, every region/.append style={#1}},
	every grid/.value forbidden,
	every grid/.style={color=\pgfkeysvalueof{/tikz/causets/grid color}, line width=\pgfkeysvalueof{/tikz/causets/grid line width}, step=\causetTileSize},
	grid/.style={show permutation, every grid/.append style={#1}},
	every tile/.value forbidden,
	every tile/.style={color=\pgfkeysvalueof{/tikz/causets/tile color}, line width=\pgfkeysvalueof{/tikz/causets/grid line width}, fill},
	tiles/.style={show permutation, every tile/.append style={#1}}
}
% Label styles:
\tikzcausetsset{%
	every label base/.value forbidden,
	every label base/.style={text opacity=1, scale=\causetTextScale, font=\causetTextFont, inner sep=0pt},
	all labels/.style={every label base/.append style={#1}},
	at/.initial=center,
	every label/.value forbidden,
	every label/.style={every label base, color=\pgfkeysvalueof{/tikz/causets/label color}, below left, at=south west},
	labels/.style={show labels, every label/.append style={#1}},
	every ulabel/.value forbidden,
	every ulabel/.style={every label base, color=\pgfkeysvalueof{/tikz/causets/ulabel color}, right, rotate=-45, inner sep=1pt, align=left},
	ulabels/.style={show ulabels, every ulabel/.append style={#1}},
	every vlabel/.value forbidden,
	every vlabel/.style={every label base, color=\pgfkeysvalueof{/tikz/causets/vlabel color}, left, rotate=45, inner sep=1pt, align=right}, 
	vlabels/.style={show vlabels, every vlabel/.append style={#1}},
	every replaced label/.value forbidden,
	every replaced label/.style={every label},
	replaced labels/.style={every replaced label/.append style={#1}}
}
% Event styles:
\tikzcausetsset{%
	every event/.value forbidden,
	every event/.style={color=\pgfkeysvalueof{/tikz/causets/event color}, circle, fill, inner sep=0pt, minimum size=\pgfkeysvalueof{/tikz/causets/event size}, line width=\pgfkeysvalueof{/tikz/causets/event outline}},
	events/.value required,
	events/.style={every event/.append style={#1}},
	every replaced event/.value forbidden,
	every replaced event/.style={every label base, circle, minimum size=\pgfkeysvalueof{/tikz/causets/event size}},
	replaced events/.value required,
	replaced events/.style={every replaced event/.append style={#1}}
}
% Link styles:
\tikzcausetsset{%
	Link Start/.tip={},
	Link Pause/.tip={Triangle Cap[] . Fast Triangle[] . Fast Triangle[]},
	Link Resume/.tip={Triangle Cap[reversed] . Fast Triangle[reversed] . Fast Triangle[reversed]},
	Link End/.tip={},
	every link/.value forbidden,
	every link/.style={color=\pgfkeysvalueof{/tikz/causets/link color}, line width=\pgfkeysvalueof{/tikz/causets/link width}, {Link Start[]}-{Link End[]}},
	links/.style={show links, every link/.append style={#1}},
	link starts/.value required,
	link starts/.style={Link Start/.tip={#1}},
	link pauses/.value required,
	link pauses/.style={Link Pause/.tip={#1}},
	link resumes/.value required,
	link resumes/.style={Link Resume/.tip={#1}},
	link ends/.value required,
	link ends/.style={Link End/.tip={#1}},
	cap links/.value forbidden,
	cap links/.style={link starts={Triangle Cap[reversed]}, link ends={Triangle Cap[]}},
	arrow links/.value forbidden,
	arrow links/.style={link ends={Stealth[round, open, length=4*\pgfkeysvalueof{/tikz/causets/link width}, line width=\pgfkeysvalueof{/tikz/causets/link width}/3]}},
	every spatial link/.value forbidden,
	every spatial link/.style={every link, color=\pgfkeysvalueof{/tikz/causets/spatial link color}, dashed},
	spatial links/.style={show spatial links, every spatial link/.append style={#1}}
}

%% Package options:
\tikzcausetsset{normal, offset=0}
\causetsDrawLinkstrue
\causetsBreakLinkstrue
\causetsDrawULabelstrue
\causetsDrawVLabelstrue
\DeclareOption{tiny}{\tikzcausetsset{tiny}}
\DeclareOption{smaller}{\tikzcausetsset{very small}}
\DeclareOption{small}{\tikzcausetsset{small}}
\DeclareOption{large}{\tikzcausetsset{large}}
\DeclareOption{larger}{\tikzcausetsset{very large}}
\DeclareOption{huge}{\tikzcausetsset{huge}}
\DeclareOption{gray}{\tikzcausetsset{gray colors}}
\DeclareOption{black}{\tikzcausetsset{black colors}}
\DeclareOption{blue}{\tikzcausetsset{blue colors}}
\DeclareOption{neon}{\tikzcausetsset{neon colors}}
\DeclareOption{linked}{\causetsDrawLinkstrue}
\DeclareOption{unlinked}{\causetsDrawLinksfalse}
\DeclareOption{caps}{\tikzcausetsset{cap links}}
\DeclareOption{arrows}{\tikzcausetsset{arrow links}}
\DeclareOption{continuous}{\causetsBreakLinksfalse}
\DeclareOption{spatial}{\causetsDrawSpatialLinkstrue}
\DeclareOption{permutation}{\causetsDrawPermutationtrue}
\DeclareOption{labeled}{\causetsDrawLabelstrue}
\DeclareOption{unlabeled}{\causetsDrawLabelsfalse}
\DeclareOption{ulabeled}{\causetsDrawULabelstrue}
\DeclareOption{uunlabeled}{\causetsDrawULabelsfalse}
\DeclareOption{vlabeled}{\causetsDrawVLabelstrue}
\DeclareOption{vunlabeled}{\causetsDrawVLabelsfalse}
\DeclareOption*{\PackageWarning{causets}{The package causets does not support the option: \CurrentOption}}
\ProcessOptions\relax

%% Package functions:
\newif\ifcauset@ItemFoundInList

\newcommand*{\causets@prepareCauset}[1]{%
% Count the number of events (\causet@N), find the minimal value in the list argument (\causet@Start), and define baseline point.
\foreach \causet@E@i/\causet@Label/\causet@Text [count=\causet@i] in {#1}{%
	\ifnum\causet@i=1
		\pgfmathparse{int(\causet@E@i)}
	\else
		\pgfmathparse{int(min(\causet@Start, \causet@E@i))}
	\fi
	\xdef\causet@Start{\pgfmathresult}
	\xdef\causet@N{\causet@i}
}
\pgfmathsetmacro\causet@Start{int(\causet@Start + \causet@Offset)}
\coordinate (baseline point) at (0, -0.707*\causetTileSize);
}

\newcommand*{\causets@drawEvent}[2][every event]{%
% Draws the event node for \causet@E@i. By default the event style (optional argument #1) is that of every event. The text of the node is #2.
\pgfmathsetmacro\causet@u{\causet@E@i - \causet@Start + 0.5}
\pgfmathsetmacro\causet@v{\causet@i - 0.5}
\node[causets/.cd, #1] (E\causet@E@i) 
	at ( \causet@u * \causetTileSize, \causet@v * \causetTileSize ) 
	{#2};
}

\newcommand*{\causets@drawEvents}[1]{%
% Draws a permutation tile and defines and draws an event node (E#) on top of the tile, for each event number # in the list argument.
\foreach \causet@E@i/\causet@Label/\causet@Text [count=\causet@i] in {#1}{%
	\pgfmathsetmacro\causet@E@i{int(\causet@E@i + \causet@Offset)}
	\pgfmathsetmacro\causet@u{\causet@E@i - \causet@Start}
	\pgfmathsetmacro\causet@v{\causet@i - 1.0}
	\ifcausetsDrawPermutation
		\path[causets/.cd, every tile] 
			( \causet@u * \causetTileSize, \causet@v * \causetTileSize ) 
			rectangle +( \causetTileSize, \causetTileSize );
	\fi
	\ifx\causet@Text\causet@Label
		\causets@drawEvent{}
	\else
		\causets@drawEvent[every replaced event]{\causet@Text}
	\fi
}
}

\newcommand*{\causets@drawPermutation}[1]{%
% Draws the permutation grid and adds the u- and v-coordinate labels.
\ifcausetsDrawPermutation
	\draw[causets/.cd, every grid] ( 0, 0 ) 
		grid ( \causet@N * \causetTileSize, \causet@N * \causetTileSize );
	\draw[causets/.cd, every region] ( 0, 0 ) 
		rectangle ( \causet@N * \causetTileSize, \causet@N * \causetTileSize );
	\foreach \causet@E@i/\causet@Label/\causet@Text [count=\causet@i] in {#1}{%
		\pgfmathsetmacro\causet@E@i{int(\causet@E@i + \causet@Offset)}
		\ifcausetsDrawULabels
			\pgfmathsetmacro\causet@u{int(\causet@i + \causet@Start - 1)}
			\node[causets/.cd, every ulabel] (EUL\causet@E@i) 
				at ( \causet@i * \causetTileSize - 0.5 * \causetTileSize, 0 ) 
				{\causet@u};
		\fi
		\ifcausetsDrawVLabels
			\node[causets/.cd, every vlabel] (EVL\causet@E@i) 
				at ( 0, \causet@i * \causetTileSize - 0.5 * \causetTileSize ) 
				{\causet@E@i};
		\fi
	}
\fi
}

\newcommand*{\causets@drawSpatialLinks}[1]{%
% Draws the spatial links between the events (only for \drawpcauset and \drawrcauset).
\ifcausetsDrawSpatialLinks
	\foreach \causet@E@i/\causet@Label/\causet@Text [count=\causet@i] in {#1}{%
		\pgfmathsetmacro\causet@E@i{int(\causet@E@i + \causet@Offset)}
		% \causet@Bound stores the lower bound to find spatially linked events.
		\edef\causet@Bound{\causet@Offset}
		\foreach \causet@E@j/\causet@Label/\causet@Text [count=\causet@j] in {#1}{%
			\ifnum\causet@j>\causet@i
				\pgfmathsetmacro\causet@E@j{int(\causet@E@j + \causet@Offset)}
				\ifnum\causet@E@j>\causet@Bound
					\ifnum\causet@E@i>\causet@E@j
						% Add spatial link and remember new lower bound:
						\draw[causets/.cd, every spatial link] (E\causet@E@j) -- (E\causet@E@i);
						\xdef\causet@Bound{\causet@E@j}
					\fi
				\fi
			\fi
		}
	}
\fi
}

\newcommand*{\causets@drawEventLabel}[1][every label]{%
% Draws the event label for \causet@E@i (shifted by \causet@Offset). By default the label style is assumed that of a standard event label.
\pgfmathsetmacro\causet@E@i{int(\causet@E@i + \causet@Offset)}
\node[causets/.cd, #1] (EL\causet@E@i)
	at (E\causet@E@i.\pgfkeysvalueof{/tikz/causets/at}) {\causet@Label};
}

\newcommand*{\causets@drawEventLabels}[1]{%
% Adds event labels to the events.
\foreach \causet@E@i/\causet@Label/\causet@Text in {#1}{%
	\ifx\causet@E@i\causet@Label
		\ifcausetsDrawLabels
			\causets@drawEventLabel
		\fi
	\else
		\ifx\causet@Label\empty
			\relax% Skip empty labels
		\else
			\causets@drawEventLabel[every replaced label]
		\fi
	\fi
}
}

\newcommand*{\drawpcauset}[1]{%
% Draws a causet from a permutation.
\causets@prepareCauset{#1}
\begin{scope}[rotate=45]
	\begin{scope}[xshift=-\causet@N * \causetTileSize / 2, 
	              yshift=-\causet@N * \causetTileSize / 2]
		\causets@drawEvents{#1}
		\causets@drawPermutation{#1}
		\ifcausetsDrawLinks
			\foreach \causet@E@i/\causet@Label/\causet@Text [count=\causet@i] in {#1}{%
				\pgfmathsetmacro\causet@E@i{int(\causet@E@i + \causet@Offset)}
				% \causet@Bound stores the upper bound to find linked events.
				\pgfmathsetmacro\causet@Bound{int(\causet@N + \causet@Start)}
				\foreach \causet@E@j/\causet@Label/\causet@Text [count=\causet@j] in {#1}{%
					\ifnum\causet@j>\causet@i
						\pgfmathsetmacro\causet@E@j{int(\causet@E@j + \causet@Offset)}
						\ifnum\causet@E@j<\causet@Bound
							\ifnum\causet@E@i<\causet@E@j
								% Add link and remember new upper bound:
								\draw[causets/.cd, every link] (E\causet@E@i) -- (E\causet@E@j);
								\xdef\causet@Bound{\causet@E@j}
							\fi
						\fi
					\fi
				}
			}
		\fi
		\causets@drawSpatialLinks{#1}
		\causets@drawEventLabels{#1}
	\end{scope}
\end{scope}
}

\newcommand*{\drawcauset}[2]{%
% Draws a causet from a permutation and a link-pair list.
\causets@prepareCauset{#1}
\begin{scope}[rotate=45]
	\begin{scope}[xshift=-\causet@N * \causetTileSize / 2, 
	              yshift=-\causet@N * \causetTileSize / 2]
		\causets@drawEvents{#1}
		\causets@drawPermutation{#1}
		\ifcausetsDrawLinks
			\ifcausetsBreakLinks
				% Broken links are allowed, so test every link if it crosses over an unlinked event:
				\foreach \causet@From/\causet@To in {#2}{%
					% In this function, \causet@u and \causet@v store the link distance in the u- and in the v-direction, respectively. Since these are coordinate differences, they are computed without adding the offset.
					\pgfmathsetmacro\causet@u{int(\causet@To) - int(\causet@From)}
					\foreach \causet@E@i/\causet@Label/\causet@Text [count=\causet@i] in {#1}{%
						\ifnum\causet@E@i=\causet@From
							\xdef\causet@v{\causet@i}
						\fi
						\ifnum\causet@E@i=\causet@To
							\pgfmathparse{int(\causet@i - \causet@v)}
							\xdef\causet@v{\pgfmathresult}
							\breakforeach
						\fi
					}
					% Now add offset to events:
					\pgfmathsetmacro\causet@From{int(\causet@From + \causet@Offset)}
					\pgfmathsetmacro\causet@To{int(\causet@To + \causet@Offset)}
					% In this function, \causet@E@j stores the last breaking point and \causet@j the index of \causet@From.
					\pgfmathsetmacro\causet@E@j{int(\causet@From - 1)}
					\edef\causet@j{0}
					\foreach \causet@E@i/\causet@Label/\causet@Text [count=\causet@i] in {#1}{%
						\pgfmathsetmacro\causet@E@i{int(\causet@E@i + \causet@Offset)}
						\ifnum\causet@E@i=\causet@To
							\edef\causet@j{0}
							\breakforeach
						\fi
						\ifnum\causet@j>0
							% If the u-/v-coordinate ratio is the same, the event \causet@E@i falls along the link and the link needs to break. The ratios are compared via cross-multiplication, which is faster and does not cause rounding errors:
							\pgfmathequal{int((\causet@E@i - \causet@From) * \causet@v)}{int(\causet@u * (\causet@i - \causet@j)))}
							\ifnum\pgfmathresult=1
								\ifnum\causet@E@j<\causet@From
									% Link is crossing over another event, so draw the link up to the (first) breaking point:
									\draw[causets/.cd, every link, {Link Start}-{Link Pause}, shorten >=\pgfkeysvalueof{/tikz/causets/broken link gap}]
										(E\causet@From) -- (E\causet@E@i);
								\else
									% Link is crossing over yet another event, so continue from last breaking point to next breaking point:
									\draw[causets/.cd, every link, {Link Resume}-{Link Pause}, shorten <=\pgfkeysvalueof{/tikz/causets/broken link gap}, shorten >=\pgfkeysvalueof{/tikz/causets/broken link gap}]
										(E\causet@E@j) -- (E\causet@E@i);
								\fi
								\xdef\causet@E@j{\causet@E@i}
							\fi
						\fi
						\ifnum\causet@E@i=\causet@From
							\xdef\causet@j{\causet@i}
						\fi
					}
					\ifnum\causet@E@j>\causet@From
						% Link did cross over another event, so finish the link from last breaking point to final event:
						\draw[causets/.cd, every link, {Link Resume}-{Link End}, shorten <=\pgfkeysvalueof{/tikz/causets/broken link gap}]
							(E\causet@E@j) -- (E\causet@To);
					\else
						% Link does not cross over another event, so just draw it:
						\draw[causets/.cd, every link]
							(E\causet@From) -- (E\causet@To);
					\fi
				}
			\else
				% Broken links are not allowed, so just draw the links:
				\foreach \causet@From/\causet@To in {#2}{%
					\pgfmathsetmacro\causet@From{int(\causet@From + \causet@Offset)}
					\pgfmathsetmacro\causet@To{int(\causet@To + \causet@Offset)}
					\draw[causets/.cd, every link] (E\causet@From) -- (E\causet@To);
				}
			\fi
		\fi
		\causets@drawEventLabels{#1}
	\end{scope}
\end{scope}
}

\newcommand*{\drawrcauset}[2]{%
% Draws a causet from a permutation and ignoring links given in the second list argument (of link-pairs).
\causets@prepareCauset{#1}
\begin{scope}[rotate=45]
	\begin{scope}[xshift=-\causet@N * \causetTileSize / 2, 
	              yshift=-\causet@N * \causetTileSize / 2]
		\causets@drawEvents{#1}
		\causets@drawPermutation{#1}
		\ifcausetsDrawLinks
			\foreach \causet@E@i/\causet@Label/\causet@Text [count=\causet@i] in {#1}{%
				\pgfmathsetmacro\causet@E@i{int(\causet@E@i + \causet@Offset)}
				% \causet@Bound stores the upper bound to find linked events.
				\pgfmathsetmacro\causet@Bound{int(\causet@N + \causet@Start)}
				\foreach \causet@E@j/\causet@Label/\causet@Text [count=\causet@j] in {#1}{%
					\ifnum\causet@j>\causet@i
						\pgfmathsetmacro\causet@E@j{int(\causet@E@j + \causet@Offset)}
						\ifnum\causet@E@j<\causet@Bound
							\ifnum\causet@E@i<\causet@E@j
								% Check second list argument if the link has to be ignored:
								\global\causet@ItemFoundInListfalse
								\foreach \causet@From/\causet@To in {#2}{%
									\pgfmathparse{and(int(\causet@From + \causet@Offset) == int(\causet@E@i), int(\causet@To + \causet@Offset) == int(\causet@E@j))}
									\ifnum\pgfmathresult=1
										\global\causet@ItemFoundInListtrue
									\fi
								}
								\ifcauset@ItemFoundInList
									\relax% Skip link as it is included in the second argument.
								\else
									% Draw link as it is not included in the second argument:
									\draw[causets/.cd, every link] (E\causet@E@i) -- (E\causet@E@j);
								\fi
								% Remember new upper bound:
								\xdef\causet@Bound{\causet@E@j}
							\fi
						\fi
					\fi
				}
			}
		\fi
		\causets@drawSpatialLinks{#1}
		% Add omitted links as spatial links:
		\ifcausetsDrawSpatialLinks
			\foreach \causet@From/\causet@To in {#2}{%
				\pgfmathsetmacro\causet@From{int(\causet@From + \causet@Offset)}
				\pgfmathsetmacro\causet@To{int(\causet@To + \causet@Offset)}
				\draw[causets/.cd, every spatial link] (E\causet@From) -- (E\causet@To);
			}
		\fi
		\causets@drawEventLabels{#1}
	\end{scope}
\end{scope}
}

\newcommand*{\pcauset}[2][]{%
% Inserts a TikZ picture with a causet, created from a permutation.
\begin{tikzpicture}[causets/.cd, every causet, #1]\drawpcauset{#2}\end{tikzpicture}}

\newcommand*{\causet}[3][]{%
% Inserts a TikZ picture with a causet, created from a permutation and a link-pair list.
\begin{tikzpicture}[causets/.cd, every causet, #1]\drawcauset{#2}{#3}\end{tikzpicture}}

\newcommand*{\rcauset}[3][]{%
% Inserts a TikZ picture with a causet, created from a permutation and a link-pair list of links to remove.
\begin{tikzpicture}[causets/.cd, every causet, #1]\drawrcauset{#2}{#3}\end{tikzpicture}}

%% Short-hand and pre-computed causet functions:
\providecommand{\causetspath}{.}

\newcommand*{\causetfile}[2][]{%
\includegraphics[#1]{\causetspath/#2}}

\newcommand*{\pcausetP}[2][]{%
{\causetsDrawPermutationtrue\causetsDrawLabelsfalse\pcauset[#1]{#2}}}

\newcommand*{\pcausetL}[2][]{%
{\causetsDrawPermutationfalse\causetsDrawLabelstrue\pcauset[#1]{#2}}}

\newcommand*{\pcausetX}[2][]{%
{\causetsDrawPermutationtrue\causetsDrawLabelstrue\pcauset[#1]{#2}}}
	
\newcommand*{\causetP}[3][]{%
{\causetsDrawPermutationtrue\causetsDrawLabelsfalse\causet[#1]{#2}{#3}}}

\newcommand*{\causetL}[3][]{%
{\causetsDrawPermutationfalse\causetsDrawLabelstrue\causet[#1]{#2}{#3}}}

\newcommand*{\causetX}[3][]{%
{\causetsDrawPermutationtrue\causetsDrawLabelstrue\causet[#1]{#2}{#3}}}
	
\newcommand*{\rcausetP}[3][]{%
{\causetsDrawPermutationtrue\causetsDrawLabelsfalse\rcauset[#1]{#2}{#3}}}

\newcommand*{\rcausetL}[3][]{%
{\causetsDrawPermutationfalse\causetsDrawLabelstrue\rcauset[#1]{#2}{#3}}}

\newcommand*{\rcausetX}[3][]{%
{\causetsDrawPermutationtrue\causetsDrawLabelstrue\rcauset[#1]{#2}{#3}}}

\newcommand*{\causetFence}[2][]{%
\ifcase#2%
\or%=1
\pcauset[#1]{1,2}%
\or%=2
\pcauset[#1]{3,1,4,2}%
\or%=3
\pcauset[#1]{5,3,6,1,4,2}%
\or%=4
\pcauset[#1]{7,5,8,3,6,1,4,2}%
\or%=5
\pcauset[#1]{9,7,10,5,8,3,6,1,4,2}%
\or%=6
\pcauset[#1]{11,9,12,7,10,5,8,3,6,1,4,2}%
\or%=7
\pcauset[#1]{13,11,14,9,12,7,10,5,8,3,6,1,4,2}%
\or%=8
\pcauset[#1]{15,13,16,11,14,9,12,7,10,5,8,3,6,1,4,2}%
\or%=9
\pcauset[#1]{17,15,18,13,16,11,14,9,12,7,10,5,8,3,6,1,4,2}%
\or%=10
\pcauset[#1]{19,17,20,15,18,13,16,11,14,9,12,7,10,5,8,3,6,1,4,2}%
\fi%
}

\newcommand*{\causetClosedFence}[2][]{%
\ifcase#2%
\or%=1
\pcauset[#1]{1,2}%
\or%=2
\pcauset[#1]{2,1,4,3}%
\or%=3
\rcauset[#1]{4,2,6,1,5,3}{2/5}%
\or%=4
\rcauset[#1]{6,4,8,2,7,1,5,3}{2/5,4/7}%
\or%=5
\rcauset[#1]{8,6,10,4,9,2,7,1,5,3}{2/5,4/7,6/9}%
\or%=6
\rcauset[#1]{10,8,12,6,11,4,9,2,7,1,5,3}{2/5,4/7,6/9,8/11}%
\or%=7
\rcauset[#1]{12,10,14,8,13,6,11,4,9,2,7,1,5,3}{2/5,4/7,6/9,8/11,10/13}%
\or%=8
\rcauset[#1]{14,12,16,10,15,8,13,6,11,4,9,2,7,1,5,3}{2/5,4/7,6/9,8/11,10/13,12/15}%
\or%=9
\rcauset[#1]{16,14,18,12,17,10,15,8,13,6,11,4,9,2,7,1,5,3}{2/5,4/7,6/9,8/11,10/13,12/15,14/17}%
\or%=10
\rcauset[#1]{18,16,20,14,19,12,17,10,15,8,13,6,11,4,9,2,7,1,5,3}{2/5,4/7,6/9,8/11,10/13,12/15,14/17,16/19}%
\fi%
}

\newcommand*{\causetCrown}[1][]{%
\causetClosedFence[#1]{3}%
}