Debian Codegraph @

texmf-dist/source/latex/tqft/tqft_code.dtx

raw
1 % \iffalse meta-comment
2 %<*internal>
3 \iffalse
4 %</internal>
5 %<*readme>
6 ----------------------------------------------------------------
7 tqft --- a TikZ library for drawing TQFT diagrams
8 bundle version: v2.3 2024/05/31
9 E-mail: Andrew Stacey <[email protected]>
10 Released under the LaTeX Project Public License v1.3c or later
11 See http://www.latex-project.org/lppl.txt
12 ----------------------------------------------------------------
13
14 This package defines some shapes useful for drawing TQFT diagrams with TikZ/PGF.
15 The initial idea of this package came from a question and answer on the site https://tex.stackexchange.com.
16
17 The package comes in two versions: a package (tqft.sty) and a TikZ library (tikzlibrarytqft.code.tex). The original package (tqft.sty) is depreciated and has been frozen at version 2.1. The TikZ library is maintained and should be used in preference, except for legacy code.
18 %</readme>
19 %<*internal>
20 \fi
21 \def\nameofplainTeX{plain}
22 \ifx\fmtname\nameofplainTeX\else
23 \expandafter\begingroup
24 \fi
25 %</internal>
26 %<*install>
27 \input docstrip.tex
28 \keepsilent
29 \askforoverwritefalse
30 \preamble
31 ----------------------------------------------------------------
32 tqft --- a TikZ library for drawing TQFT diagrams
33 E-mail: [email protected]
34 Released under the LaTeX Project Public License v1.3c or later
35 See http://www.latex-project.org/lppl.txt
36 ----------------------------------------------------------------
37
38 \endpreamble
39 \postamble
40
41 Copyright (C) 2011-2024 by Andrew Stacey <[email protected]>
42
43 This work may be distributed and/or modified under the
44 conditions of the LaTeX Project Public License (LPPL), either
45 version 1.3c of this license or (at your option) any later
46 version. The latest version of this license is in the file:
47
48 http://www.latex-project.org/lppl.txt
49
50 This work is "maintained" (as per LPPL maintenance status) by
51 Andrew Stacey.
52
53 This work consists of the files tqft_code.dtx,
54 tqft.tex,
55 and the derived files tqft_code.ins,
56 tqft_code.pdf,
57 tqft.pdf,
58 tqft.sty, and
59 tikzlibrarytqft.code.tex.
60
61 \endpostamble
62 \usedir{tex/latex/tqft}
63 \generate{
64 \file{tqft.sty}{\from{\jobname.dtx}{package}}
65 }
66 \usedir{tex/latex/tqft}
67 \generate{
68 \file{tikzlibrarytqft.code.tex}{\from{\jobname.dtx}{library}}
69 }
70 %</install>
71 %<install>\endbatchfile
72 %<*internal>
73 \usedir{source/latex/tqft}
74 \generate{
75 \file{tqft_code.ins}{\from{\jobname.dtx}{install}}
76 }
77 \nopreamble\nopostamble
78 \usedir{doc/latex/demopkg}
79 \generate{
80 \file{README.txt}{\from{\jobname.dtx}{readme}}
81 }
82 \ifx\fmtname\nameofplainTeX
83 \expandafter\endbatchfile
84 \else
85 \expandafter\endgroup
86 \fi
87 %</internal>
88 %<*package>
89 \NeedsTeXFormat{LaTeX2e}
90 \ProvidesPackage{tqft}[2017/06/01 v2.1 Tikz/PGF commands for drawing TQFT diagrams]
91 %</package>
92 %<*driver>
93 \documentclass{ltxdoc}
94 \usepackage[T1]{fontenc}
95 \usepackage{lmodern}
96 %\usepackage{morefloats}
97 \usepackage{tikz}
98 \usepackage{tqft}
99 \usepackage[numbered]{hypdoc}
100 \definecolor{lstbgcolor}{rgb}{0.9,0.9,0.9}
101
102 \usepackage{listings}
103 \lstloadlanguages{[LaTeX]TeX}
104 \lstset{breakatwhitespace=true,breaklines=true,language=TeX}
105
106 \usepackage{fancyvrb}
107 \EnableCrossrefs
108 \CodelineIndex
109 \RecordChanges
110 \begin{document}
111 \DocInput{\jobname.dtx}
112 \end{document}
113 %</driver>
114 % \fi
115 %
116 % \CheckSum{2623}
117 %
118 % \CharacterTable
119 % {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
120 % 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
121 % Digits \0\1\2\3\4\5\6\7\8\9
122 % Exclamation \! Double quote \" Hash (number) \#
123 % Dollar \$ Percent \% Ampersand \&
124 % Acute accent \' Left paren \( Right paren \)
125 % Asterisk \* Plus \+ Comma \,
126 % Minus \- Point \. Solidus \/
127 % Colon \: Semicolon \; Less than \<
128 % Equals \= Greater than \> Question mark \?
129 % Commercial at \@ Left bracket \[ Backslash \\
130 % Right bracket \] Circumflex \^ Underscore \_
131 % Grave accent \` Left brace \{ Vertical bar \|
132 % Right brace \} Tilde \~}
133 %
134 %
135 % \changes{1.0}{2011/05/03}{Converted to DTX file}
136 % \changes{2.0}{2014/04/07}{Converted nodes to pics}
137 % \changes{2.1}{2017/06/01}{Can skip boundary components}
138 % \changes{2.2}{2022/08/24}{Twisted shapes}
139 % \changes{2.3}{2022/08/24}{File incorrectly named}
140 %
141 % \DoNotIndex{\newcommand,\newenvironment}
142 % \pdfstringdefDisableCommands{%
143 % \def\\{}%
144 % \def\url#1{<#1>}%
145 % }
146 %
147 % \providecommand*{\url}{\texttt}
148 % \GetFileInfo{tikzlibrarytqft.code.tex}
149 % \title{The \textsf{tqft} package: codebase}
150 % \author{Andrew Stacey \\ \url{[email protected]}}
151 % \date{\fileversion~from \filedate}
152 %
153 % \maketitle
154 %
155 %
156 % \begin{tikzpicture}[every node/.style={tqft/cobordism style={draw,thick,red}}]
157 % \node[
158 % tqft,
159 % fill=orange,
160 % fill opacity=.5,
161 % boundary style={fill=purple},
162 % cobordism style={draw,thick,red},
163 % boundary lower style={draw,dashed,thick,blue},
164 % boundary upper style={draw,green,thick},
165 % incoming boundary components=4,
166 % outgoing boundary components=6,
167 % offset=-1.5,
168 % ] (a) {};
169 % \node[pin=north:1] at (a.incoming boundary 1) {};
170 % \node[pin=north:3] at (a.incoming boundary 3) {};
171 % \node[pin=south:1] at (a.outgoing boundary 1) {};
172 % \node[pin=south:4] at (a.outgoing boundary 4) {};
173 % \node[pin=south:6] at (a.outgoing boundary 6) {};
174 % \end{tikzpicture}
175 %
176 % \section{Introduction}
177 %
178 % This is a package for drawing TQFT diagrams using PGF/TikZ.
179 % Its inspiration was a question and answer on the website \url{http://tex.stackexchance.com}.
180 %
181 %
182 % \StopEventually{}
183 %
184 % \section{Implementation}
185 %
186 % \subsection{Old Version: Node Shapes}
187 %
188 % \iffalse
189 %<*package>
190 % \fi
191 % \begin{macrocode}
192 \RequirePackage{pgfkeys}
193 \RequirePackage{pgf}
194 % \end{macrocode}
195 %
196 % We can view the cobordisms from the \emph{input} or \emph{output} ends, the implementation of the choice is to draw an arc from 0 to 180 or from 0 to -180 so we just need to track minus signs.
197 % These macros are for that.
198 % \begin{macrocode}
199 \def\pgf@tqft@minus{-}
200 \let\pgf@tqft@upper\@empty
201 \let\pgf@tqft@lower\pgf@tqft@minus
202 % \end{macrocode}
203 % Some helpful extra functions.
204 %
205 % \Verb+\tqftset+ is our equivalent of \Verb+\tikzset+.
206 % \begin{macrocode}
207 \def\tqftset#1{\pgfkeys{/pgf/tqft/.cd,#1}}
208 % \end{macrocode}
209 % \begin{macro}{\tqft@process}
210 % This macro applies our flow transformation to the given coordinates, % with the result stored in \Verb+\pgf@x+ and \Verb+\pgf@y+.
211 % \begin{macrocode}
212 \def\tqft@process#1#2{%
213 \edef\tqft@px{#1}
214 \edef\tqft@py{#2}
215 \pgf@process{
216 \pgftransformreset
217 \let\tikz@transform=\pgfutil@empty
218 \expandafter\tikzset\expandafter{\tqft@transformation}
219 \tikz@transform
220 \pgfpointtransformed{\pgfqpoint{\tqft@px}{\tqft@py}}
221 }
222 }
223 % \end{macrocode}
224 % \end{macro}
225 %
226 % Declare some dimension registers to hold the specifications of the cobordism.
227 % \begin{macrocode}
228 \newdimen\tqft@xa
229 \newdimen\tqft@xb
230 \newdimen\tqft@c
231 \newdimen\tqft@ch
232 \newdimen\tqft@h
233 \newdimen\tqft@s
234 \newdimen\tqft@w
235 \newif\iftqft@within@node
236 % \end{macrocode}
237 % Now we set up all the keys that we'll need in the course of this shape
238 % \begin{macrocode}
239 \pgfkeys{
240 % \end{macrocode}
241 % Add a key to switch between the two versions.
242 % \begin{macrocode}
243 /tikz/tqft/use nodes/.is choice,
244 /tikz/tqft/use nodes/true/.code={%
245 \tikzset{
246 % \end{macrocode}
247 % If using nodes, set the defaults
248 % \begin{macrocode}
249 tqft/.style={%
250 /tikz/shape=tqft cobordism,
251 /pgf/tqft,
252 /tikz/every tqft/.try
253 },
254 % \end{macrocode}
255 % Unknowns go to \Verb+/pgf/tqft+
256 % \begin{macrocode}
257 tqft/.unknown/.code={%
258 \let\tqft@searchname=\pgfkeyscurrentname%
259 \pgfkeys{%
260 /pgf/tqft/\tqft@searchname={##1}
261 }
262 },
263 }%
264 },
265 % \end{macrocode}
266 % If not using nodes, set the defaults for the library
267 % \begin{macrocode}
268 /tikz/tqft/use nodes/false/.code={%
269 \tikzset{
270 tqft/.style={%
271 pic type=cobordism,
272 tqft/.cd,
273 every tqft/.try,
274 },
275 % \end{macrocode}
276 % Pass unknown keys on to TikZ.
277 % \begin{macrocode}
278 tqft/.unknown/.code={%
279 \let\tqft@searchname=\pgfkeyscurrentname%
280 \pgfkeys{%
281 /tikz/\tqft@searchname={##1}
282 }
283 },
284 }%
285 },
286 /tikz/tqft/use nodes=true,
287 % \end{macrocode}
288 % This deals with unknown keys, passing them on to TikZ.
289 % \begin{macrocode}
290 /pgf/tqft/.unknown/.code={%
291 \let\tqft@searchname=\pgfkeyscurrentname%
292 \pgfkeysalso{%
293 /tikz/\tqft@searchname={#1}
294 }
295 },
296 % \end{macrocode}
297 % Let's play happy families!
298 % \begin{macrocode}
299 /pgf/tqft/.is family,
300 /pgf/tqft,
301 % \end{macrocode}
302 % This sets our shape to be the boundary circle
303 % \begin{macrocode}
304 boundary circle/.style={
305 /tikz/shape=tqft boundary circle
306 },
307 % \end{macrocode}
308 % These set our number of boundary components
309 % \begin{macrocode}
310 incoming boundary components/.initial=5,
311 outgoing boundary components/.initial=4,
312 % \end{macrocode}
313 % This is the ``horizontal'' offset of the first outgoing component from the first incoming one.
314 % \begin{macrocode}
315 offset/.initial=0,
316 % \end{macrocode}
317 % This is the ``vertical'' separation between boundary components.
318 % \begin{macrocode}
319 cobordism height/.initial=2cm,
320 % \end{macrocode}
321 % This is the ``horizontal'' separation between boundary components.
322 % \begin{macrocode}
323 boundary separation/.initial=2cm,
324 % \end{macrocode}
325 % These are the ``horizontal'' and ``vertical'' radii, respectively, of the boundary components (perhaps poorly named!).
326 % \begin{macrocode}
327 circle width/.initial=10pt,
328 circle depth/.initial=5pt,
329 % \end{macrocode}
330 % These control the separation between the node and its anchors.
331 % \begin{macrocode}
332 outer xsep/.initial=0pt,
333 outer ysep/.initial=0pt,
334 outer sep/.style={
335 outer xsep=#1,
336 outer ysep=#1
337 },
338 % \end{macrocode}
339 % This is our flow control. The \Verb+flow+ key installs a transformation to be applied to our node shape.
340 % The possible transformations are stored in the following keys.
341 % They aren't just rotations so that the numbering is always ``top to bottom'' or ``left to right''.
342 % \begin{macrocode}
343 flow/.code={%
344 \pgfkeys{/pgf/tqft/flow transformation/.expand twice/.expand once=\pgfkeysvalueof{/pgf/tqft/flow transformation #1}}
345 },
346 flow transformation south/.initial={},
347 flow transformation north/.initial={%
348 xscale=-1,rotate=180
349 },
350 flow transformation east/.initial={%
351 rotate=90,xscale=-1
352 },
353 flow transformation west/.initial={%
354 rotate=270
355 },
356 flow transformation/.initial={},
357 % \end{macrocode}
358 % These control the direction from which we view the cobordism.
359 % \begin{macrocode}
360 view from/.is choice,
361 view from/incoming/.code={%
362 \let\pgf@tqft@upper\pgf@tqft@minus
363 \let\pgf@tqft@lower\@empty
364 },
365 view from/outgoing/.code={%
366 \let\pgf@tqft@lower\pgf@tqft@minus
367 \let\pgf@tqft@upper\@empty
368 },
369 % \end{macrocode}
370 % The next set of keys are for styling the different pieces of a cobordism.
371 % \begin{macrocode}
372 boundary lower style contents/.initial={},
373 boundary lower style/.code={%
374 \pgfkeys{/pgf/tqft/boundary lower style contents/.style={%
375 /tikz/.cd,#1
376 }
377 }
378 },
379 boundary style contents/.initial={},
380 boundary style/.code={%
381 \pgfkeys{/pgf/tqft/boundary style contents/.style={%
382 /tikz/.cd,#1
383 }
384 }
385 },
386 boundary upper style contents/.initial={},
387 boundary upper style/.code={%
388 \pgfkeys{/pgf/tqft/boundary upper style contents/.style={%
389 /tikz/.cd,#1
390 }
391 }
392 },
393 cobordism style contents/.initial={},
394 cobordism style/.code={%
395 \pgfkeys{/pgf/tqft/cobordism style contents/.style={%
396 /tikz/.cd,#1%
397 }
398 }
399 },
400 % \end{macrocode}
401 % The next set of keys define some default shapes.
402 % \begin{macrocode}
403 pair of pants/.style={
404 /tikz/tqft,
405 incoming boundary components=1,
406 outgoing boundary components=2,
407 offset=-.5
408 },
409 /tikz/tqft pair of pants/.style={
410 /pgf/tqft/pair of pants,
411 },
412 reverse pair of pants/.style={
413 /tikz/tqft,
414 incoming boundary components=2,
415 outgoing boundary components=1,
416 offset=.5
417 },
418 /tikz/tqft reverse pair of pants/.style={
419 /pgf/tqft/reverse pair of pants,
420 },
421 cylinder to prior/.style={
422 /tikz/tqft,
423 incoming boundary components=1,
424 outgoing boundary components=1,
425 offset=-.5
426 },
427 /tikz/tqft cylinder to prior/.style={
428 /pgf/tqft/cylinder to prior,
429 },
430 cylinder to next/.style={
431 /tikz/tqft,
432 incoming boundary components=1,
433 outgoing boundary components=1,
434 offset=.5
435 },
436 /tikz/tqft cylinder to next/.style={
437 /pgf/tqft/cylinder to next,
438 },
439 cylinder/.style={
440 /tikz/tqft,
441 incoming boundary components=1,
442 outgoing boundary components=1
443 },
444 /tikz/tqft cylinder/.style={
445 /pgf/tqft/cylinder,
446 },
447 cup/.style={
448 /tikz/tqft,
449 incoming boundary components=1,
450 outgoing boundary components=0
451 },
452 /tikz/tqft cup/.style={
453 /pgf/tqft/cup,
454 },
455 cap/.style={
456 /tikz/tqft,
457 incoming boundary components=0,
458 outgoing boundary components=1
459 },
460 /tikz/tqft cap/.style={
461 /pgf/tqft/cap,
462 },
463 }
464 % \end{macrocode}
465 %
466 % \begin{macro}{tqft shape}
467 % This is a generic cobordism shape
468 % \begin{macrocode}
469 \pgfdeclareshape{tqft cobordism}{
470 % \end{macrocode}
471 % Save our specifications: incoming and outgoing boundary components
472 % \begin{macrocode}
473 \savedmacro{\tqft@incoming}{\edef\tqft@incoming{\pgfkeysvalueof{/pgf/tqft/incoming boundary components}}}
474 \savedmacro{\tqft@outgoing}{\edef\tqft@outgoing{\pgfkeysvalueof{/pgf/tqft/outgoing boundary components}}}
475 % \end{macrocode}
476 % and the offset (in units of boundary components) between the leading incoming and outgoing components (regarded as a shift of the outgoing components relative to the incoming)
477 % \begin{macrocode}
478 \savedmacro{\tqft@offset}{\edef\tqft@offset{\pgfkeysvalueof{/pgf/tqft/offset}}}
479 % \end{macrocode}
480 % Now we save our dimensions: height, separation, the radii of the boundary circles, and outer seps, and the heights of the control points.
481 % \begin{macrocode}
482 \saveddimen{\tqft@height}{\pgf@x=\pgfkeysvalueof{/pgf/tqft/cobordism height}}
483 \saveddimen{\tqft@separation}{\pgf@x=\pgfkeysvalueof{/pgf/tqft/boundary separation}}
484 \saveddimen{\tqft@width}{\pgf@x=\pgfkeysvalueof{/pgf/tqft/circle width}}
485 \saveddimen{\tqft@depth}{\pgf@x=\pgfkeysvalueof{/pgf/tqft/circle depth}}
486 \saveddimen{\tqft@outerxsep}{\pgf@x=\pgfkeysvalueof{/pgf/tqft/outer xsep}}
487 \saveddimen{\tqft@outerysep}{\pgf@x=\pgfkeysvalueof{/pgf/tqft/outer ysep}}
488 \saveddimen{\tqft@control}{
489 \pgfkeysgetvalue{/pgf/tqft/cobordism height}{\tqft@tempa}
490 \pgfkeysgetvalue{/pgf/tqft/circle depth}{\tqft@tempb}
491 \pgfmathsetlength{\pgf@x}{.5 * \tqft@tempa - 4 * \tqft@tempb}
492 }
493 % \end{macrocode}
494 % This is the internal transformation that is in place
495 % \begin{macrocode}
496 \savedmacro{\tqft@transformation}{%
497 \pgfkeysgetvalue{/pgf/tqft/flow transformation}{\tqft@transformation}
498 }
499 % \end{macrocode}
500 % For the externally available anchors, we need to save a few things as well.
501 %
502 % Position of first incoming boundary in internal coordinates
503 % \begin{macrocode}
504 \savedanchor{\tqft@start@incoming}{%
505 \pgfmathsetlength{\pgf@x}{-(max(\pgfkeysvalueof{/pgf/tqft/incoming boundary components} - 1,\pgfkeysvalueof{/pgf/tqft/outgoing boundary components} - 1 + \pgfkeysvalueof{/pgf/tqft/offset}) + min(0,\pgfkeysvalueof{/pgf/tqft/offset}) )*\pgfkeysvalueof{/pgf/tqft/boundary separation}/2}
506 \pgfmathsetlength{\pgf@y}{.5 * \pgfkeysvalueof{/pgf/tqft/cobordism height}}
507 }
508 % \end{macrocode}
509 % Position of first outgoing boundary in internal coordinates
510 % \begin{macrocode}
511 \savedanchor{\tqft@start@outgoing}{%
512 \pgfmathsetlength{\pgf@x}{-(max(\pgfkeysvalueof{/pgf/tqft/incoming boundary components} - 1,\pgfkeysvalueof{/pgf/tqft/outgoing boundary components} - 1 + \pgfkeysvalueof{/pgf/tqft/offset}) + min(0,\pgfkeysvalueof{/pgf/tqft/offset})- 2*\pgfkeysvalueof{/pgf/tqft/offset})*\pgfkeysvalueof{/pgf/tqft/boundary separation}/2}
513 \pgfmathsetlength{\pgf@y}{-.5 * \pgfkeysvalueof{/pgf/tqft/cobordism height}}
514 }
515 % \end{macrocode}
516 % For completeness, we record the size of the text box (not that we expect any text, but you never know)
517 % \begin{macrocode}
518 \savedanchor{\tqft@textsize}{%
519 \pgf@y=-.5\ht\pgfnodeparttextbox%
520 \pgf@x=-.5\wd\pgfnodeparttextbox%
521 }
522 % \end{macrocode}
523 %
524 % These are our externally available anchors
525 % \begin{macrocode}
526 \anchor{centre}{\pgfpointorigin}
527 \anchor{center}{\pgfpointorigin}
528 \anchor{text}{
529 \tqft@textsize
530 }
531 \anchor{north}{%
532 \pgf@ya=\tqft@height\relax
533 \pgf@yb=.5\pgf@ya
534 \advance\pgf@yb by \tqft@outerysep\relax
535 \tqft@process{0pt}{\the\pgf@yb}
536 }
537 \anchor{south}{%
538 \pgf@yb=\tqft@height\relax
539 \pgf@ya=.5\pgf@yb
540 \advance\pgf@ya by \tqft@outerysep\relax
541 \pgf@yb=-\pgf@ya
542 \tqft@process{0pt}{\the\pgf@yb}
543 }
544 \anchor{west}{%
545 \tqft@start@incoming
546 \pgf@xa=\pgf@x
547 \advance\pgf@xa by -\tqft@width
548 \pgf@ya=\pgf@y
549 \tqft@start@outgoing
550 \pgf@xb=\pgf@x
551 \advance\pgf@xb by -\tqft@width
552 \pgf@yb=\pgf@y
553 \pgf@xc=.5\pgf@xa
554 \advance\pgf@xc by .5\pgf@xb
555 \pgf@yc=.5\pgf@ya
556 \advance\pgf@yc by .5\pgf@yb
557 \advance\pgf@xc by -\tqft@outerxsep\relax
558 \tqft@process{\the\pgf@xc}{\the\pgf@yc}
559 }
560 \anchor{east}{%
561 \tqft@start@incoming
562 \pgf@xa=\pgf@x
563 \pgfmathsetlength{\pgf@xa}{\pgf@xa + (\tqft@incoming - 1) * \tqft@separation}
564 \advance\pgf@xa by \tqft@width\relax
565 \pgf@ya=\pgf@y
566 \tqft@start@outgoing
567 \pgf@xb=\pgf@x
568 \pgfmathsetlength{\pgf@xb}{\pgf@xb + (\tqft@outgoing - 1) * \tqft@separation}
569 \advance\pgf@xb by \tqft@width\relax
570 \pgf@yb=\pgf@y
571 \pgf@xc=.5\pgf@xa
572 \advance\pgf@xc by .5\pgf@xb
573 \pgf@yc=.5\pgf@ya
574 \advance\pgf@yc by .5\pgf@yb
575 \advance\pgf@xc by \tqft@outerxsep\relax
576 \tqft@process{\the\pgf@xc}{\the\pgf@yc}
577 }
578 \anchor{north west}{
579 \tqft@start@incoming
580 \pgf@xc=\pgf@x
581 \pgf@yc=\pgf@y
582 \advance\pgf@xc by -\tqft@width\relax
583 \advance\pgf@yc by \tqft@outerysep\relax
584 \advance\pgf@xc by -\tqft@outerxsep\relax
585 \tqft@process{\the\pgf@xc}{\the\pgf@yc}
586 }
587 \anchor{south west}{
588 \tqft@start@outgoing
589 \pgf@xc=\pgf@x
590 \pgf@yc=\pgf@y
591 \advance\pgf@xc by -\tqft@width\relax
592 \advance\pgf@yc by -\tqft@outerysep\relax
593 \advance\pgf@xc by -\tqft@outerxsep\relax
594 \tqft@process{\the\pgf@xc}{\the\pgf@yc}
595 }
596 \anchor{north east}{
597 \tqft@start@incoming
598 \pgf@xc=\pgf@x
599 \pgfmathsetlength{\pgf@xc}{\pgf@xc + (\tqft@incoming - 1)*\tqft@separation}
600 \pgf@yc=\pgf@y
601 \advance\pgf@xc by \tqft@width\relax
602 \advance\pgf@yc by \tqft@outerysep\relax
603 \advance\pgf@xc by \tqft@outerxsep\relax
604 \tqft@process{\the\pgf@xc}{\the\pgf@yc}
605 }
606 \anchor{south east}{
607 \tqft@start@outgoing
608 \pgf@xc=\pgf@x
609 \pgfmathsetlength{\pgf@xc}{\pgf@xc + (\tqft@outgoing - 1)*\tqft@separation}
610 \pgf@yc=\pgf@y
611 \advance\pgf@xc by \tqft@width\relax
612 \advance\pgf@yc by -\tqft@outerysep\relax
613 \advance\pgf@xc by \tqft@outerxsep\relax
614 \tqft@process{\the\pgf@xc}{\the\pgf@yc}
615 }
616 % \end{macrocode}
617 % To define anchors at the boundary components requires a bit of trickery borrowed from the ``regular polygon'' shape.
618 % \begin{macrocode}
619 \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@tqft cobordism\endcsname{%
620 \c@pgf@counta\tqft@incoming\relax%
621 \pgfmathloop%
622 \ifnum\c@pgf@counta>0\relax%
623 \pgfutil@ifundefined{pgf@anchor@tqft cobordism@incoming boundary\space\the\c@pgf@counta}{%
624 \expandafter\xdef\csname pgf@anchor@tqft cobordism@incoming boundary\space\the\c@pgf@counta\endcsname{%
625 \noexpand\tqft@start@incoming
626 \noexpand\pgfmathsetlength{\noexpand\pgf@y}{\noexpand\pgf@y + \noexpand\tqft@outerysep}
627 \noexpand\pgfmathsetlength{\noexpand\pgf@x}{\noexpand\pgf@x + (\the\c@pgf@counta - 1) * \noexpand\tqft@separation}
628 \noexpand\tqft@process{\noexpand\the\noexpand\pgf@x}{\noexpand\the\noexpand\pgf@y}
629 }
630 }{\c@pgf@counta0\relax}%
631 \advance\c@pgf@counta-1\relax%
632 \repeatpgfmathloop%
633 }
634 \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@tqft cobordism\endcsname{%
635 \c@pgf@counta\tqft@outgoing\relax%
636 \pgfmathloop%
637 \ifnum\c@pgf@counta>0\relax%
638 \pgfutil@ifundefined{pgf@anchor@tqft cobordism@outgoing boundary\space\the\c@pgf@counta}{%
639 \expandafter\xdef\csname pgf@anchor@tqft cobordism@outgoing boundary\space\the\c@pgf@counta\endcsname{%
640 \noexpand\tqft@start@outgoing
641 \noexpand\pgfmathsetlength{\noexpand\pgf@y}{\noexpand\pgf@y - \noexpand\tqft@outerysep}
642 \noexpand\pgfmathsetlength{\noexpand\pgf@x}{\noexpand\pgf@x + (\the\c@pgf@counta - 1) * \noexpand\tqft@separation}
643 \noexpand\tqft@process{\noexpand\the\noexpand\pgf@x}{\noexpand\the\noexpand\pgf@y}
644 }
645 }{\c@pgf@counta0\relax}%
646 \advance\c@pgf@counta-1\relax%
647 \repeatpgfmathloop%
648 }
649 \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@tqft cobordism\endcsname{%
650 \c@pgf@counta\tqft@incoming\relax%
651 \advance\c@pgf@counta-1\relax
652 \pgfmathloop%
653 \ifnum\c@pgf@counta>0\relax%
654 \pgfutil@ifundefined{pgf@anchor@tqft cobordism@after incoming boundary\space\the\c@pgf@counta}{%
655 \expandafter\xdef\csname pgf@anchor@tqft cobordism@after incoming boundary\space\the\c@pgf@counta\endcsname{%
656 \noexpand\tqft@start@incoming
657 \noexpand\pgfmathsetlength{\noexpand\pgf@y}{.25 * \noexpand\pgf@y +.75 * \noexpand\tqft@control + \noexpand\tqft@outerysep}
658 \noexpand\pgfmathsetlength{\noexpand\pgf@x}{\noexpand\pgf@x + (\the\c@pgf@counta - .5) * \noexpand\tqft@separation}
659 \noexpand\tqft@process{\noexpand\the\noexpand\pgf@x}{\noexpand\the\noexpand\pgf@y}
660 }
661 }{\c@pgf@counta0\relax}%
662 \advance\c@pgf@counta-1\relax%
663 \repeatpgfmathloop%
664 }
665 \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@tqft cobordism\endcsname{%
666 \c@pgf@counta\tqft@outgoing\relax%
667 \advance\c@pgf@counta-1\relax
668 \pgfmathloop%
669 \ifnum\c@pgf@counta>0\relax%
670 \pgfutil@ifundefined{pgf@anchor@tqft cobordism@after outgoing boundary\space\the\c@pgf@counta}{%
671 \expandafter\xdef\csname pgf@anchor@tqft cobordism@after outgoing boundary\space\the\c@pgf@counta\endcsname{%
672 \noexpand\tqft@start@outgoing
673 \noexpand\pgfmathsetlength{\noexpand\pgf@y}{.25 * \noexpand\pgf@y -.75 * \noexpand\tqft@control - \noexpand\tqft@outerysep}
674 \noexpand\pgfmathsetlength{\noexpand\pgf@x}{\noexpand\pgf@x + (\the\c@pgf@counta - .5) * \noexpand\tqft@separation}
675 \noexpand\tqft@process{\noexpand\the\noexpand\pgf@x}{\noexpand\the\noexpand\pgf@y}
676 }
677 }{\c@pgf@counta0\relax}%
678 \advance\c@pgf@counta-1\relax%
679 \repeatpgfmathloop%
680 }
681 % \end{macrocode}
682 % Now we define the background path.
683 % This is the upper part of the cobordism.
684 % \begin{macrocode}
685 \backgroundpath{
686 % \end{macrocode}
687 % Apply the internal transformation
688 % \begin{macrocode}
689 \let\tikz@transform=\pgfutil@empty
690 \expandafter\tikzset\expandafter{\tqft@transformation}
691 \tikz@transform
692 % \end{macrocode}
693 % Convert the boundary separation and width to lengths
694 % \begin{macrocode}
695 \pgfmathsetlength{\tqft@s}{\tqft@separation}
696 \pgfmathsetlength{\tqft@w}{2*\tqft@width}
697 % \end{macrocode}
698 % Compute the starting position of the incoming boundary components so that we get the centre anchor on the centre of the cobordism
699 % \begin{macrocode}
700 \tqft@start@incoming
701 \tqft@xa=\pgf@x
702 \advance\tqft@xa by -.5\tqft@w\relax
703 \tqft@h=\pgf@y
704 \tqft@xb=\tqft@xa
705 \advance\tqft@xb by \tqft@w\relax
706 \tqft@c=\tqft@control\relax
707 % \end{macrocode}
708 % Do we have any incoming boundary components at all?
709 % \begin{macrocode}
710 \ifnum\tqft@incoming>0
711 % \end{macrocode}
712 % Yes, so move to the position of the first and draw it
713 % \begin{macrocode}
714 \pgfpathmoveto{\pgfqpoint{\tqft@xa}{\tqft@h}}
715 \pgfpatharc{\pgf@tqft@upper180}{0}{\tqft@width and \tqft@depth}
716 % \end{macrocode}
717 % Do we have any more incoming boundary components?
718 % \begin{macrocode}
719 \ifnum\tqft@incoming>1
720 % \end{macrocode}
721 % Yes, so iterate over the remaining incoming boundary components
722 % \begin{macrocode}
723 \foreach \tqft@k in {2,...,\tqft@incoming} {
724 \advance\tqft@xa by \tqft@k\tqft@s
725 \advance\tqft@xb by \tqft@k\tqft@s
726 \advance\tqft@xb by -2\tqft@s
727 \advance\tqft@xa by -\tqft@s
728 \pgfpathcurveto{\pgfqpoint{\tqft@xb}{\tqft@c}}{\pgfqpoint{\tqft@xa}{\tqft@c}}{\pgfqpoint{\tqft@xa}{\tqft@h}}
729 \pgfpatharc{\pgf@tqft@upper180}{0}{\tqft@width and \tqft@depth}
730 }
731 \fi
732 % \end{macrocode}
733 % If we don't have any outgoing boundary components, may as well close up now.
734 % \begin{macrocode}
735 \ifnum\tqft@outgoing=0
736 \advance\tqft@xb by \tqft@incoming\tqft@s
737 \advance\tqft@xb by -\tqft@s
738 \pgfmathsetlength{\tqft@ch}{min(0,max(-\tqft@h,\tqft@h - (\tqft@h - \tqft@c) * ((abs(\tqft@xb - \tqft@xa) - \tqft@w)/\tqft@s + 1)))}
739 \pgfpathcurveto{\pgfqpoint{\tqft@xb}{\tqft@ch}}{\pgfqpoint{\tqft@xa}{\tqft@ch}}{\pgfqpoint{\tqft@xa}{\tqft@h}}
740 \fi
741 \fi
742 % \end{macrocode}
743 % Shift down to the outgoing components, if we have any
744 % \begin{macrocode}
745 \ifnum\tqft@outgoing>0
746 \advance\tqft@xb by \tqft@incoming\tqft@s
747 \advance\tqft@xb by -\tqft@s
748 \pgfmathsetlength{\tqft@xa}{\tqft@xa + (\tqft@outgoing - 1 + \tqft@offset) * \tqft@separation + 2*\tqft@width}
749 % \end{macrocode}
750 % If we had incoming boundaries, this is a curveto, otherwise it's a moveto
751 % \begin{macrocode}
752 \ifnum\tqft@incoming>0
753 \pgfmathsetlength{\tqft@ch}{min(0,max(-\tqft@h,\tqft@h - (\tqft@h - \tqft@c) * ((abs(\tqft@xb - \tqft@xa) - \tqft@w)/\tqft@s + 1)))}
754 \pgfpathcurveto{\pgfqpoint{\tqft@xb}{\tqft@ch}}{\pgfqpoint{\tqft@xa}{-\tqft@ch}}{\pgfqpoint{\tqft@xa}{-\tqft@h}}
755 \else
756 \pgfpathmoveto{\pgfqpoint{\tqft@xa}{-\tqft@h}}
757 \fi
758 \tqft@xb=\tqft@xa
759 \advance\tqft@xb by -\tqft@w
760 % \end{macrocode}
761 % Now draw the lower components
762 % \begin{macrocode}
763 \pgfpatharc{0}{\pgf@tqft@upper180}{\tqft@width and \tqft@depth}
764 % \end{macrocode}
765 % Now iterate over the remaining outgoing boundary components
766 % \begin{macrocode}
767 \ifnum\tqft@outgoing>1
768 \foreach \tqft@k in {2,...,\tqft@outgoing} {
769 \advance\tqft@xa by -\tqft@k\tqft@s
770 \advance\tqft@xb by -\tqft@k\tqft@s
771 \advance\tqft@xb by 2\tqft@s
772 \advance\tqft@xa by \tqft@s
773 \pgfpathcurveto{\pgfqpoint{\tqft@xb}{-\tqft@c}}{\pgfqpoint{\tqft@xa}{-\tqft@c}}{\pgfqpoint{\tqft@xa}{-\tqft@h}}
774 \pgfpatharc{0}{\pgf@tqft@upper180}{\tqft@width and \tqft@depth}
775 }
776 \fi
777 % \end{macrocode}
778 % Shift back up to the incoming components, if we had any, otherwise arc back to our starting point
779 % \begin{macrocode}
780 \advance\tqft@xb by -\tqft@outgoing\tqft@s
781 \advance\tqft@xb by \tqft@s
782 \ifnum\tqft@incoming>0
783 \pgfmathsetlength{\tqft@xa}{\tqft@xa - (\tqft@outgoing -1 + \tqft@offset) * \tqft@separation - 2*\tqft@width}
784 \pgfmathsetlength{\tqft@ch}{min(0,max(-\tqft@h,\tqft@h - (\tqft@h - \tqft@c) * ((abs(\tqft@xb - \tqft@xa) - \tqft@w)/\tqft@s + 1)))}
785 \pgfpathcurveto{\pgfqpoint{\tqft@xb}{-\tqft@ch}}{\pgfqpoint{\tqft@xa}{\tqft@ch}}{\pgfqpoint{\tqft@xa}{\tqft@h}}
786 \else
787 \pgfmathsetlength{\tqft@ch}{min(0,max(-\tqft@h,\tqft@h - (\tqft@h - \tqft@c) * ((abs(\tqft@xb - \tqft@xa) - \tqft@w)/\tqft@s + 1)))}
788 \pgfpathcurveto{\pgfqpoint{\tqft@xb}{-\tqft@ch}}{\pgfqpoint{\tqft@xa}{-\tqft@ch}}{\pgfqpoint{\tqft@xa}{-\tqft@h}}
789 \fi
790 \fi
791 % \end{macrocode}
792 % Close the path
793 % \begin{macrocode}
794 \pgfpathclose
795 }
796 % \end{macrocode}
797 % End of background path
798 % Now we define the behind background path.
799 % This is the lower part of the boundary circles.
800 % \begin{macrocode}
801 \behindbackgroundpath{
802 % \end{macrocode}
803 % Apply the internal transformation
804 % \begin{macrocode}
805 \let\tikz@transform=\pgfutil@empty
806 \expandafter\tikzset\expandafter{\tqft@transformation}
807 \tikz@transform
808 % \end{macrocode}
809 % Convert the boundary separation and width to lengths
810 % \begin{macrocode}
811 \pgfmathsetlength{\tqft@s}{\tqft@separation}
812 \pgfmathsetlength{\tqft@w}{2*\tqft@width}
813 % \end{macrocode}
814 % Compute the starting position of the incoming boundary components so that we get the centre anchor on the centre of the cobordism
815 % \begin{macrocode}
816 \pgfmathsetlength{\tqft@xa}{-(max(\tqft@incoming - 1,\tqft@outgoing - 1 + \tqft@offset) + min(0,\tqft@offset) + 2)*\tqft@separation/2}
817 \pgfmathsetlength{\tqft@h}{.5 * \tqft@height}
818 % \end{macrocode}
819 % This section draws the boundary circles
820 % \begin{macrocode}
821 {
822 % \end{macrocode}
823 % Initialise the TikZ path settings and read in the style options for the boundary
824 % \begin{macrocode}
825 \tikz@mode@fillfalse%
826 \tikz@mode@drawfalse%
827 \let\tikz@mode=\pgfutil@empty
828 \let\tikz@options=\pgfutil@empty
829 \tqftset{boundary style contents}
830 \tikz@mode
831 \tikz@options
832 % \end{macrocode}
833 % Do we have any incoming boundary components at all?
834 % \begin{macrocode}
835 \ifnum\tqft@incoming>0
836 % \end{macrocode}
837 % Yes, so iterate over them
838 % \begin{macrocode}
839 \foreach \tqft@k in {1,...,\tqft@incoming} {
840 \advance\tqft@xa by \tqft@k\tqft@s
841 \pgfpathellipse{\pgfqpoint{\tqft@xa}{\tqft@h}}{\pgfqpoint{\tqft@width}{0pt}}{\pgfqpoint{0pt}{\tqft@depth}}
842 }
843 \fi
844 % \end{macrocode}
845 % Now iterate over the outgoing boundary components, if we have any
846 % \begin{macrocode}
847 \ifnum\tqft@outgoing>0
848 \pgfmathsetlength{\tqft@xa}{\tqft@xa + (\tqft@outgoing + \tqft@offset + 1) * \tqft@separation}
849 \foreach \tqft@k in {1,...,\tqft@outgoing} {
850 \advance\tqft@xa by -\tqft@k\tqft@s
851 % \advance\tqft@xa by \tqft@s
852 \pgfpathellipse{\pgfqpoint{\tqft@xa}{-\tqft@h}}{\pgfqpoint{\tqft@width}{0pt}}{\pgfqpoint{0pt}{\tqft@depth}}
853 }
854 \fi
855 % \end{macrocode}
856 % \begin{macrocode}
857 \edef\tikz@temp{\noexpand\pgfusepath{%
858 \iftikz@mode@fill fill,\fi%
859 \iftikz@mode@draw draw\fi%
860 }}%
861 \tikz@temp
862 }
863 % \end{macrocode}
864 % This section draws the lower parts of the boundary circles
865 % \begin{macrocode}
866 {
867 % \end{macrocode}
868 % Initialise the TikZ path settings and read in the style options for the boundary
869 % \begin{macrocode}
870 \tikz@mode@fillfalse%
871 \tikz@mode@drawfalse%
872 \let\tikz@mode=\pgfutil@empty
873 \let\tikz@options=\pgfutil@empty
874 \tqftset{boundary lower style contents}
875 \tikz@mode
876 \tikz@options
877 \advance\tqft@xa by .5\tqft@w
878 % \end{macrocode}
879 % Do we have any incoming boundary components at all?
880 % \begin{macrocode}
881 \ifnum\tqft@incoming>0
882 % \end{macrocode}
883 % Yes, so iterate over them
884 % \begin{macrocode}
885 \foreach \tqft@k in {1,...,\tqft@incoming} {
886 \advance\tqft@xa by \tqft@k\tqft@s
887 \pgfpathmoveto{\pgfqpoint{\tqft@xa}{\tqft@h}}
888 \pgfpatharc{0}{\pgf@tqft@lower180}{\tqft@width and \tqft@depth}
889 }
890 \fi
891 % \end{macrocode}
892 % Now iterate over the outgoing boundary components, if we have any
893 % \begin{macrocode}
894 \ifnum\tqft@outgoing>0
895 \pgfmathsetlength{\tqft@xa}{\tqft@xa + (\tqft@outgoing + \tqft@offset + 1) * \tqft@separation}
896 \foreach \tqft@k in {1,...,\tqft@outgoing} {
897 \advance\tqft@xa by -\tqft@k\tqft@s
898 % \advance\tqft@xa by \tqft@s
899 \pgfpathmoveto{\pgfqpoint{\tqft@xa}{-\tqft@h}}
900 \pgfpatharc{0}{\pgf@tqft@lower180}{\tqft@width and \tqft@depth}
901 }
902 \fi
903 % \end{macrocode}
904 % \begin{macrocode}
905 \edef\tikz@temp{\noexpand\pgfusepath{%
906 \iftikz@mode@fill fill,\fi%
907 \iftikz@mode@draw draw\fi%
908 }}%
909 \tikz@temp
910 }
911 }
912 % \end{macrocode}
913 % End of behind background path.
914 %
915 % Now we define the before background path.
916 % This is the upper part of the boundary circles and the cobordism edge.
917 % \begin{macrocode}
918 \beforebackgroundpath{
919 % \end{macrocode}
920 % We \emph{don't} apply the internal transformation as it is already in place from the \Verb+\backgroundpath+.
921 % Convert the boundary separation and width to lengths
922 % \begin{macrocode}
923 \pgfmathsetlength{\tqft@s}{\tqft@separation}
924 \pgfmathsetlength{\tqft@w}{2*\tqft@width}
925 % \end{macrocode}
926 % Compute the starting position of the incoming boundary components so that we get the centre anchor on the centre of the cobordism
927 % \begin{macrocode}
928 \pgfmathsetlength{\tqft@xa}{-(max(\tqft@incoming - 1,\tqft@outgoing - 1 + \tqft@offset) + min(0,\tqft@offset))*\tqft@s/2 - \tqft@width}
929 \tqft@xb=\tqft@xa
930 \advance\tqft@xb by \tqft@w
931 \tqft@c=\tqft@control\relax
932 \pgfmathsetlength{\tqft@h}{.5 * \tqft@height}
933 % \end{macrocode}
934 % This section draws the non-boundary part of the cobordism.
935 % \begin{macrocode}
936 {
937 % \end{macrocode}
938 % Initialise the TikZ path settings and read in the style options for the boundary
939 % \begin{macrocode}
940 \tikz@mode@fillfalse%
941 \tikz@mode@drawfalse%
942 \let\tikz@mode=\pgfutil@empty
943 \let\tikz@options=\pgfutil@empty
944 \tqftset{cobordism style contents}
945 \tikz@mode
946 \tikz@options
947 % Do we have any incoming boundary components at all?
948 % \begin{macrocode}
949 \ifnum\tqft@incoming>0
950 % \end{macrocode}
951 % Do we have more than one?
952 % \begin{macrocode}
953 \ifnum\tqft@incoming>1
954 % \end{macrocode}
955 % Yes, so iterate over the remaining incoming boundary components
956 % \begin{macrocode}
957 \foreach \tqft@k in {2,...,\tqft@incoming} {
958 \advance\tqft@xa by \tqft@k\tqft@s
959 \advance\tqft@xb by \tqft@k\tqft@s
960 \advance\tqft@xb by -2\tqft@s
961 \advance\tqft@xa by -\tqft@s
962 \pgfpathmoveto{\pgfqpoint{\tqft@xb}{\tqft@h}}
963 \pgfpathcurveto{\pgfqpoint{\tqft@xb}{\tqft@c}}{\pgfqpoint{\tqft@xa}{\tqft@c}}{\pgfqpoint{\tqft@xa}{\tqft@h}}
964 }
965 \fi
966 % \end{macrocode}
967 % If we don't have any outgoing boundary components, may as well close up now.
968 % \begin{macrocode}
969 \ifnum\tqft@outgoing=0
970 \advance\tqft@xb by \tqft@incoming\tqft@s
971 \advance\tqft@xb by -\tqft@s
972 \pgfmathsetlength{\tqft@ch}{min(0,max(-\tqft@h,\tqft@h - (\tqft@h - \tqft@c) * ((abs(\tqft@xb - \tqft@xa) - \tqft@w)/\tqft@s + 1)))}
973 \pgfpathmoveto{\pgfqpoint{\tqft@xb}{\tqft@h}}
974 \pgfpathcurveto{\pgfqpoint{\tqft@xb}{\tqft@ch}}{\pgfqpoint{\tqft@xa}{\tqft@ch}}{\pgfqpoint{\tqft@xa}{\tqft@h}}
975 \fi
976 \fi
977 % \end{macrocode}
978 % Shift down to the outgoing components, if we have any
979 % \begin{macrocode}
980 \ifnum\tqft@outgoing>0
981 \advance\tqft@xb by \tqft@incoming\tqft@s
982 \advance\tqft@xb by -\tqft@s
983 \pgfmathsetlength{\tqft@xa}{\tqft@xa + (\tqft@outgoing - 1 + \tqft@offset) * \tqft@separation + 2*\tqft@width}
984 % \end{macrocode}
985 % If we had incoming boundaries, this is a curveto, otherwise it's a moveto
986 % \begin{macrocode}
987 \ifnum\tqft@incoming>0
988 \pgfmathsetlength{\tqft@ch}{min(0,max(-\tqft@h,\tqft@h - (\tqft@h - \tqft@c) * ((abs(\tqft@xb - \tqft@xa) - \tqft@w)/\tqft@s + 1)))}
989 \pgfpathmoveto{\pgfqpoint{\tqft@xb}{\tqft@h}}
990 \pgfpathcurveto{\pgfqpoint{\tqft@xb}{\tqft@ch}}{\pgfqpoint{\tqft@xa}{-\tqft@ch}}{\pgfqpoint{\tqft@xa}{-\tqft@h}}
991 \else
992 \pgfpathmoveto{\pgfqpoint{\tqft@xa}{-\tqft@h}}
993 \fi
994 \tqft@xb=\tqft@xa
995 \advance\tqft@xb by -\tqft@w
996 % \end{macrocode}
997 % Now draw the lower components
998 % \begin{macrocode}
999 \pgfpathmoveto{\pgfqpoint{\tqft@xb}{-\tqft@h}}
1000 % \end{macrocode}
1001 % Now iterate over the remaining outgoing boundary components
1002 % \begin{macrocode}
1003 \ifnum\tqft@outgoing>1
1004 \foreach \tqft@k in {2,...,\tqft@outgoing} {
1005 \advance\tqft@xa by -\tqft@k\tqft@s
1006 \advance\tqft@xb by -\tqft@k\tqft@s
1007 \advance\tqft@xb by 2\tqft@s
1008 \advance\tqft@xa by \tqft@s
1009 \pgfpathcurveto{\pgfqpoint{\tqft@xb}{-\tqft@c}}{\pgfqpoint{\tqft@xa}{-\tqft@c}}{\pgfqpoint{\tqft@xa}{-\tqft@h}}
1010 \advance\tqft@xa by -\tqft@w
1011 \pgfpathmoveto{\pgfqpoint{\tqft@xa}{-\tqft@h}}
1012 }
1013 \fi
1014 % \end{macrocode}
1015 % Shift back up to the incoming components, if we had any, otherwise arc back to our starting point
1016 % \begin{macrocode}
1017 \advance\tqft@xb by -\tqft@outgoing\tqft@s
1018 \advance\tqft@xb by \tqft@s
1019 \ifnum\tqft@incoming>0
1020 \pgfmathsetlength{\tqft@xa}{\tqft@xa - (\tqft@outgoing -1 + \tqft@offset) * \tqft@separation - 2*\tqft@width}
1021 \pgfmathsetlength{\tqft@ch}{min(0,max(-\tqft@h,\tqft@h - (\tqft@h - \tqft@c) * ((abs(\tqft@xb - \tqft@xa) - \tqft@w)/\tqft@s + 1)))}
1022 \pgfpathcurveto{\pgfqpoint{\tqft@xb}{-\tqft@ch}}{\pgfqpoint{\tqft@xa}{\tqft@ch}}{\pgfqpoint{\tqft@xa}{\tqft@h}}
1023 \else
1024 \pgfmathsetlength{\tqft@ch}{min(0,max(-\tqft@h,\tqft@h - (\tqft@h - \tqft@c) * ((abs(\tqft@xb - \tqft@xa) - \tqft@w)/\tqft@s + 1)))}
1025 \pgfpathcurveto{\pgfqpoint{\tqft@xb}{-\tqft@ch}}{\pgfqpoint{\tqft@xa}{-\tqft@ch}}{\pgfqpoint{\tqft@xa}{-\tqft@h}}
1026 \fi
1027 \fi
1028 \edef\tikz@temp{\noexpand\pgfusepath{%
1029 \iftikz@mode@fill fill,\fi%
1030 \iftikz@mode@draw draw\fi%
1031 }}%
1032 \tikz@temp
1033 }
1034 % \end{macrocode}
1035 % This section draws the upper parts of the boundary circles
1036 % \begin{macrocode}
1037 {
1038 % \end{macrocode}
1039 % Initialise the TikZ path settings and read in the style options for the boundary
1040 % \begin{macrocode}
1041 \let\tqft@bdry@path=\pgfutil@empty
1042 \let\tqft@bdry@node@path=\pgfutil@empty
1043 \pgfsyssoftpath@setcurrentpath{\tqft@bdry@path}
1044 \tikz@mode@fillfalse%
1045 \tikz@mode@drawfalse%
1046 \let\tikz@mode=\pgfutil@empty
1047 \let\tikz@options=\pgfutil@empty
1048 \tqftset{boundary upper style contents}
1049 \tikz@mode
1050 \tikz@options
1051 \advance\tqft@xa by -\tqft@s
1052 \advance\tqft@xa by \tqft@w
1053 % \end{macrocode}
1054 % Do we have any incoming boundary components at all?
1055 % \begin{macrocode}
1056 \ifnum\tqft@incoming>0
1057 % \end{macrocode}
1058 % Yes, so iterate over them
1059 % \begin{macrocode}
1060 \foreach \tqft@k in {1,...,\tqft@incoming} {
1061 \advance\tqft@xa by \tqft@k\tqft@s
1062 \pgfpathmoveto{\pgfqpoint{\tqft@xa}{\tqft@h}}
1063 \pgfpatharc{0}{\pgf@tqft@upper180}{\tqft@width and \tqft@depth}
1064 \ifx\tikz@fig@name\pgfutil@empty
1065 \else
1066 {
1067 \advance\tqft@xa by -\tqft@width
1068 \pgftransformshift{\pgfqpoint{\tqft@xa}{\tqft@h}}
1069 \tqft@within@nodetrue
1070 \pgfsyssoftpath@getcurrentpath{\tqft@bdry@path}
1071 \pgfsyssoftpath@setcurrentpath{\tqft@bdry@node@path}
1072 \pgfnode{tqft boundary circle}{centre}{}{\tikz@fig@name\space incoming \tqft@k}{}
1073 \pgfsyssoftpath@getcurrentpath{\tqft@bdry@node@path}
1074 \pgfsyssoftpath@setcurrentpath{\tqft@bdry@path}
1075 }
1076 \fi
1077 }
1078 \fi
1079 % \end{macrocode}
1080 % Now iterate over the outgoing boundary components, if we have any
1081 % \begin{macrocode}
1082 \ifnum\tqft@outgoing>0
1083 \pgfmathsetlength{\tqft@xa}{\tqft@xa + (\tqft@outgoing + \tqft@offset + 1) * \tqft@separation}
1084 \foreach \tqft@k in {1,...,\tqft@outgoing} {
1085 \advance\tqft@xa by -\tqft@k\tqft@s
1086 % \advance\tqft@xa by \tqft@s
1087 \pgfpathmoveto{\pgfqpoint{\tqft@xa}{-\tqft@h}}
1088 \pgfpatharc{0}{\pgf@tqft@upper180}{\tqft@width and \tqft@depth}
1089 \ifx\tikz@fig@name\pgfutil@empty
1090 \else
1091 {
1092 \pgfmathtruncatemacro{\tqft@l}{\tqft@outgoing + 1 - \tqft@k}
1093 \advance\tqft@xa by -\tqft@width
1094 \pgftransformshift{\pgfqpoint{\tqft@xa}{-\tqft@h}}
1095 \tqft@within@nodetrue
1096 \pgfsyssoftpath@getcurrentpath{\tqft@bdry@path}
1097 \pgfsyssoftpath@setcurrentpath{\tqft@bdry@node@path}
1098 \pgfnode{tqft boundary circle}{centre}{}{\tikz@fig@name\space outgoing \tqft@l}{}
1099 \pgfsyssoftpath@getcurrentpath{\tqft@bdry@node@path}
1100 \pgfsyssoftpath@setcurrentpath{\tqft@bdry@path}
1101 }
1102 \fi
1103 }
1104 \fi
1105 % \end{macrocode}
1106 % \begin{macrocode}
1107 \edef\tikz@temp{\noexpand\pgfusepath{%
1108 \iftikz@mode@fill fill,\fi%
1109 \iftikz@mode@draw draw\fi%
1110 }}%
1111 \tikz@temp
1112 }
1113 }
1114 }
1115 % \end{macrocode}
1116 % \end{macro}
1117 %
1118 % \begin{macro}{boundary circle shape}
1119 % This is a the shape of the boundary circles
1120 % \begin{macrocode}
1121 \pgfdeclareshape{tqft boundary circle}{
1122 % \end{macrocode}
1123 % Now we save our dimensions: height, separation, and the radii of the boundary circles
1124 % \begin{macrocode}
1125 \saveddimen{\tqft@height}{\pgf@x=\pgfkeysvalueof{/pgf/tqft/cobordism height}}
1126 \saveddimen{\tqft@separation}{\pgf@x=\pgfkeysvalueof{/pgf/tqft/boundary separation}}
1127 \saveddimen{\tqft@width}{\pgf@x=\pgfkeysvalueof{/pgf/tqft/circle width}}
1128 \saveddimen{\tqft@depth}{\pgf@x=\pgfkeysvalueof{/pgf/tqft/circle depth}}
1129 % \end{macrocode}
1130 %
1131 % For the externally available anchors, we need to save the declared transformation; we save the actual transformation, not the macro that points to it.
1132 % If we're called within the main cobordism shape, the transformation is already applied so we ignore it.
1133 % \begin{macrocode}
1134 \savedmacro{\tqft@transformation}{%
1135 \iftqft@within@node
1136 \let\tqft@transformation=\pgfutil@empty
1137 \else
1138 \pgfkeysgetvalue{/pgf/tqft/flow transformation}{\tqft@transformation}
1139 \fi
1140 }
1141 % \end{macrocode}
1142 % \begin{macrocode}
1143 \savedanchor{\tqft@centre}{%
1144 \pgfpointorigin}
1145 % \end{macrocode}
1146 % For completeness, we record the size of the text box (not that we expect any text, but you never know)
1147 % \begin{macrocode}
1148 \savedanchor{\tqft@textsize}{%
1149 \pgf@y=-.5\ht\pgfnodeparttextbox%
1150 \pgf@x=-.5\wd\pgfnodeparttextbox%
1151 }
1152 % \end{macrocode}
1153 % These are our externally available anchors
1154 % \begin{macrocode}
1155 \anchor{centre}{\tqft@centre}
1156 \anchor{center}{\tqft@centre}
1157 \anchor{text}{
1158 \tqft@textsize
1159 }
1160 \anchor{next}{%
1161 \tqft@process{\tqft@separation}{0pt}}%
1162 % \end{macrocode}
1163 % \begin{macrocode}
1164 \anchor{prior}{%
1165 \tqft@process{-\tqft@separation}{0pt}}%
1166 % \end{macrocode}
1167 % \begin{macrocode}
1168 \anchor{above}{%
1169 \tqft@process{0pt}{\tqft@height}}%
1170 % \end{macrocode}
1171 % \begin{macrocode}
1172 \anchor{below}{%
1173 \tqft@process{0pt}{-\tqft@height}}%
1174 % \end{macrocode}
1175 % The anchor border is the ellipse, but we need to take into account the possible transformation.
1176 % (This isn't right if the origin is shifted.)
1177 % At the moment, '0 degrees' is interpreted in the transformed coordinate system.
1178 % Should provide a system whereby that can be intepreted in the main coordinate system.
1179 % \begin{macrocode}
1180 \anchorborder{
1181 % \end{macrocode}
1182 % This next \Verb+\pgf@process+ makes the angles absolute.
1183 % Comment it out to make the angles relative.
1184 % \begin{macrocode}
1185 \tqft@process{\the\pgf@x}{\the\pgf@y}
1186 \edef\tqft@marshal{%
1187 \noexpand\pgfpointborderellipse
1188 {\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}
1189 {\noexpand\pgfqpoint{\tqft@width}{\tqft@depth}}
1190 }%
1191 \tqft@marshal
1192 \tqft@process{\the\pgf@x}{\the\pgf@y}
1193 }
1194 % \end{macrocode}
1195 % Now we define the background path.
1196 % This is the upper part of the cobordism.
1197 % \begin{macrocode}
1198 \backgroundpath{
1199 % \end{macrocode}
1200 % Apply the internal transformation if we're not within a node
1201 % \begin{macrocode}
1202 \let\tikz@transform=\pgfutil@empty
1203 \expandafter\tikzset\expandafter{\tqft@transformation}
1204 \tikz@transform
1205 % \end{macrocode}
1206 % Draw the boundary circle
1207 % \begin{macrocode}
1208 \pgfpathellipse{\pgfqpoint{0pt}{0pt}}{\pgfqpoint{\tqft@width}{0pt}}{\pgfqpoint{0pt}{\tqft@depth}}
1209 }
1210 % \end{macrocode}
1211 % We draw the upper and lower arcs again with the appropriate styles
1212 % \begin{macrocode}
1213 \beforebackgroundpath{
1214 \iftqft@within@node
1215 \else
1216 \tikz@mode@fillfalse%
1217 \tikz@mode@drawfalse%
1218 \let\tikz@mode=\pgfutil@empty
1219 \let\tikz@options=\pgfutil@empty
1220 {
1221 \pgfsys@beginscope
1222 \tqftset{boundary lower style contents}
1223 \tikz@mode
1224 \tikz@options
1225 \pgfpathmoveto{\pgfqpoint{\tqft@width}{0pt}}
1226 \pgfpatharc{0}{\pgf@tqft@lower180}{\tqft@width and \tqft@depth}
1227 \edef\tikz@temp{\noexpand\pgfusepath{%
1228 \iftikz@mode@fill fill,\fi%
1229 \iftikz@mode@draw draw\fi%
1230 }}%
1231 \tikz@temp
1232 \pgfsys@endscope
1233 }
1234 {
1235 \pgfsys@beginscope
1236 \tqftset{boundary upper style contents}
1237 \tikz@mode
1238 \tikz@options
1239 \pgfpathmoveto{\pgfqpoint{\tqft@width}{0pt}}
1240 \pgfpatharc{0}{\pgf@tqft@upper180}{\tqft@width and \tqft@depth}
1241 \edef\tikz@temp{\noexpand\pgfusepath{%
1242 \iftikz@mode@fill fill,\fi%
1243 \iftikz@mode@draw draw\fi%
1244 }}%
1245 \tikz@temp
1246 \pgfsys@endscope
1247 }
1248 \fi
1249 }
1250 }
1251 % \end{macrocode}
1252 % \end{macro}
1253 % \iffalse
1254 %</package>
1255 % \fi
1256 %
1257 % \subsection{New Version: Picture Shapes}
1258 %
1259 % \iffalse
1260 %<*library>
1261 \ProvidesFile{tikzlibrarytqft.code.tex}[2024/05/31 v2.3 Tikz/PGF commands for drawing TQFT diagrams]
1262 % \fi
1263 %
1264 % Issue a warning if the pic syntax is not available.
1265 %
1266 % \begin{macrocode}
1267 \ifcsname pgfk@/handlers/.pic/.@cmd\endcsname
1268 \else
1269 \pgfwarning{This library only works with TikZ 3.0 or later; for earlier versions of TikZ use the TQFT package}
1270 \fi
1271 % \end{macrocode}
1272 % For the boundaries, we need elliptical node shapes.
1273 % \begin{macrocode}
1274 \usetikzlibrary{shapes.geometric}
1275 % \end{macrocode}
1276 % We can view the cobordisms from the \emph{input} or \emph{output} ends, the implementation of the choice is to draw an arc from 0 to 180 or from 0 to -180 so we just need to track minus signs.
1277 % These macros are for that.
1278 % \begin{macrocode}
1279 \def\pgf@tqft@minus{-}
1280 \let\pgf@tqft@upper\@empty
1281 \let\pgf@tqft@lower\pgf@tqft@minus
1282 % \end{macrocode}
1283 % Should we twist the cobordism?
1284 % \begin{macrocode}
1285 \newif\iftqft@twisted
1286 % \end{macrocode}
1287 % Split an anchoring coordinate.
1288 % The \(y\)--value is simply multiplied by the cobordism height (but pointing downwards, so that \(1\) is level with the outgoing boundary).
1289 % The \(x\)--value is multiplied by the boundary separation, but is shifted so that at the incoming boundary level, or above, then it is in line with the incoming boundaries and similarly at the outgoing boundary level, or below, it is in line with the outgoing boundaries.
1290 % \begin{macrocode}
1291 \def\tqft@split(#1,#2){%
1292 \pgfmathsetmacro\tqft@y{#2 * (-\tqft@val{cobordism height})}%
1293 \pgfmathsetmacro\tqft@x{(#1 - 1 + max(min(#2,1),0)*\tqft@val{offset}) * \tqft@val{boundary separation}}%
1294 \def\tqft@shift{(\tqft@x pt, \tqft@y pt)}%
1295 }%
1296 % \end{macrocode}
1297 % Now we set up all the keys that we'll need in the course of this shape
1298 % \begin{macrocode}
1299 \tikzset{
1300 % \end{macrocode}
1301 % Fix for the fact that the \verb+alias+ key doesn't use the prefix and suffix.
1302 % \begin{macrocode}
1303 pic alias/.code={%
1304 \tikz@fig@mustbenamed
1305 \expandafter\def\expandafter\tikz@alias\expandafter{\tikz@alias\pgfnodealias{\tikz@pp@name{#1}}{\tikz@fig@name}}%
1306 },
1307 % \end{macrocode}
1308 % This key is our basic installer key, setting the pic and putting us in the right key family.
1309 % \begin{macrocode}
1310 tqft/.style={%
1311 pic type=cobordism,
1312 every tqft/.try,
1313 tqft/.cd,
1314 },
1315 % \end{macrocode}
1316 % This deals with unknown keys, passing them on to TikZ.
1317 % \begin{macrocode}
1318 tqft/.unknown/.code={%
1319 \let\tqft@searchname=\pgfkeyscurrentname%
1320 \pgfkeysalso{%
1321 /tikz/\tqft@searchname={#1}
1322 }
1323 },
1324 % \end{macrocode}
1325 % Let's play happy families!
1326 % \begin{macrocode}
1327 tqft/.cd,
1328 % \end{macrocode}
1329 % These set our number of boundary components and genus.
1330 % \begin{macrocode}
1331 incoming boundary components/.initial=5,
1332 outgoing boundary components/.initial=4,
1333 skip incoming boundary components/.initial={},
1334 skip outgoing boundary components/.initial={},
1335 genus/.initial = 0,
1336 % \end{macrocode}
1337 % This is the ``horizontal'' offset of the first outgoing component from the first incoming one.
1338 % \begin{macrocode}
1339 offset/.initial=0,
1340 % \end{macrocode}
1341 % This is the ``vertical'' separation between boundary components.
1342 % \begin{macrocode}
1343 cobordism height/.initial=2cm,
1344 % \end{macrocode}
1345 % This is the ``horizontal'' separation between boundary components.
1346 % \begin{macrocode}
1347 boundary separation/.initial=2cm,
1348 % \end{macrocode}
1349 % These are the ``horizontal'' and ``vertical'' radii, respectively, of the boundary components.
1350 % \begin{macrocode}
1351 circle x radius/.initial=10pt,
1352 circle y radius/.initial=5pt,
1353 % \end{macrocode}
1354 % These control the direction from which we view the cobordism.
1355 % \begin{macrocode}
1356 view from/.is choice,
1357 view from/incoming/.code={%
1358 \let\pgf@tqft@upper\pgf@tqft@minus
1359 \let\pgf@tqft@lower\@empty
1360 },
1361 view from/outgoing/.code={%
1362 \let\pgf@tqft@lower\pgf@tqft@minus
1363 \let\pgf@tqft@upper\@empty
1364 },
1365 % \end{macrocode}
1366 % Should we twist the cobordism?
1367 % \begin{macrocode}
1368 twisted/.is if=tqft@twisted,
1369 % \end{macrocode}
1370 % We simulate node placement using the following key.
1371 % \begin{macrocode}
1372 anchor/.initial = none,
1373 % \end{macrocode}
1374 % The next set of keys define some default shapes.
1375 % \begin{macrocode}
1376 pair of pants/.style={
1377 /tikz/tqft,
1378 incoming boundary components=1,
1379 outgoing boundary components=2,
1380 offset=-.5
1381 },
1382 /tikz/tqft pair of pants/.style={
1383 /tikz/tqft/pair of pants,
1384 },
1385 reverse pair of pants/.style={
1386 /tikz/tqft,
1387 incoming boundary components=2,
1388 outgoing boundary components=1,
1389 offset=.5
1390 },
1391 /tikz/tqft reverse pair of pants/.style={
1392 /tikz/tqft/reverse pair of pants,
1393 },
1394 cylinder to prior/.style={
1395 /tikz/tqft,
1396 incoming boundary components=1,
1397 outgoing boundary components=1,
1398 offset=-.5
1399 },
1400 /tikz/tqft cylinder to prior/.style={
1401 /tikz/tqft/cylinder to prior,
1402 },
1403 cylinder to next/.style={
1404 /tikz/tqft,
1405 incoming boundary components=1,
1406 outgoing boundary components=1,
1407 offset=.5
1408 },
1409 /tikz/tqft cylinder to next/.style={
1410 /tikz/tqft/cylinder to next,
1411 },
1412 cylinder/.style={
1413 /tikz/tqft,
1414 incoming boundary components=1,
1415 outgoing boundary components=1
1416 },
1417 /tikz/tqft cylinder/.style={
1418 /tikz/tqft/cylinder,
1419 },
1420 cup/.style={
1421 /tikz/tqft,
1422 incoming boundary components=1,
1423 outgoing boundary components=0
1424 },
1425 /tikz/tqft cup/.style={
1426 /tikz/tqft/cup,
1427 },
1428 cap/.style={
1429 /tikz/tqft,
1430 incoming boundary components=0,
1431 outgoing boundary components=1
1432 },
1433 /tikz/tqft cap/.style={
1434 /tikz/tqft/cap,
1435 },
1436 }
1437 % \end{macrocode}
1438 %
1439 % This is a little helper macro for getting the values of tqft keys.
1440 % \begin{macrocode}
1441 \def\tqft@val#1{\pgfkeysvalueof{/tikz/tqft/#1}}
1442 % \end{macrocode}
1443 %
1444 % Now we define the code for the actual cobordism shape.
1445 % \begin{macrocode}
1446 \tikzset{
1447 cobordism/.pic={
1448 % \end{macrocode}
1449 % Defining the cobordism paths.
1450 % This holds the full boundary path of the cobordism shape.
1451 % \begin{macrocode}
1452 \gdef\tqft@fullpath{}%
1453 % \end{macrocode}
1454 % This is a list of the edge pieces without the boundary circles.
1455 % \begin{macrocode}
1456 \global\let\tqft@blist\pgfutil@gobble%
1457 % \end{macrocode}
1458 % This punches the holes (if there are any) in the cobordism shape.
1459 % \begin{macrocode}
1460 \gdef\tqft@gclip{}%
1461 % \end{macrocode}
1462 % This is a list of the paths for drawing the holes.
1463 % \begin{macrocode}
1464 \global\let\tqft@glist\pgfutil@gobble%
1465 % \end{macrocode}
1466 % This collects any coordinates that are to be defined (it appears to be difficult to define them as we go along).
1467 % \begin{macrocode}
1468 \global\let\tqft@clist\pgfutil@gobble%
1469 % \end{macrocode}
1470 % This collects any coordinates that can be used to shift the shape that aren't to be defined using \verb+\tqft@clist+.
1471 % \begin{macrocode}
1472 \global\let\tqft@alist\pgfutil@gobble
1473 % \end{macrocode}
1474 % These will be lists of the boundary components, divided into sets as to whether or not they are rendered. For the outgoing ones, we need two lists because they are rendered in the opposite order to how they are labelled.
1475 % \begin{macrocode}
1476 \global\let\tqft@ibdrylist=\pgfutil@gobble
1477 \global\let\tqft@cibdrylist=\pgfutil@gobble
1478 \global\let\tqft@obdrylist=\pgfutil@gobble
1479 \global\let\tqft@cobdrylist=\pgfutil@gobble
1480 \global\let\tqft@robdrylist=\pgfutil@gobble
1481 \global\let\tqft@rcobdrylist=\pgfutil@gobble
1482 % \end{macrocode}
1483 % Is the cobordism twisted? If so, we need to reverse the order of the
1484 % outgoing boundary components.
1485 % \begin{macrocode}
1486 \iftqft@twisted
1487 \pgfmathsetmacro\tqft@outgoing@end{0}%
1488 \pgfmathsetmacro\tqft@outgoing@dir{-1}%
1489 \else
1490 \pgfmathsetmacro\tqft@outgoing@end{1}%
1491 \pgfmathsetmacro\tqft@outgoing@dir{1}%
1492 \fi
1493 % \end{macrocode}
1494 % The first stage is to iterate over the incoming boundary components (if there are any), building up the various paths.
1495 % \begin{macrocode}
1496 \ifnum\tqft@val{incoming boundary components}>0\relax
1497 % \end{macrocode}
1498 % We have some so draw the half circle for the first component.
1499 % Note that we use \verb+\pgf@tqft@upper+ to flip the sign of the start angle depending on the \verb+view from+ setting.
1500 % \begin{macrocode}
1501 \xdef\tqft@fullpath{%
1502 \tqft@fullpath
1503 (-\tqft@val{circle x radius},0) arc[start angle=\pgf@tqft@upper180, end angle=0, x radius=\tqft@val{circle x radius}, y radius=\tqft@val{circle y radius}]
1504 }%
1505 % \end{macrocode}
1506 % And add the centre to the list for available shifts.
1507 % \begin{macrocode}
1508 \xdef\tqft@alist{%
1509 \tqft@alist,-incoming boundary 1/{(0,0)},-incoming boundary/{(0,0)}%
1510 }%
1511 % \end{macrocode}
1512 % If there are more than one then for each subsequent one we add the curve between them and the corresponding arc of the boundary circle.
1513 % \begin{macrocode}
1514 \ifnum\tqft@val{incoming boundary components}>1\relax
1515 \foreach \k in {2,...,\tqft@val{incoming boundary components}} {
1516 \edef\tqft@temp{\noexpand\pgfutil@in@{,\k,}{,\tqft@val{skip incoming boundary components},}}
1517 \tqft@temp
1518 \ifpgfutil@in@
1519 \xdef\tqft@cibdrylist{\tqft@cibdrylist,\k}
1520 \else
1521 \xdef\tqft@ibdrylist{\tqft@ibdrylist,\k}
1522 \fi
1523 }
1524 \ifx\tqft@ibdrylist\pgfutil@gobble
1525 \else
1526 \foreach \k [
1527 remember=\k as \kmo (initially 1),
1528 evaluate=\k as \xpos using (\k-1)*\tqft@val{boundary separation} -\tqft@val{circle x radius},
1529 ] in \tqft@ibdrylist {
1530 \pgfmathsetmacro\xppos{(\kmo - 1)*\tqft@val{boundary separation} + \tqft@val{circle x radius}}
1531 \pgfmathsetmacro\cpos{(\xpos + \xppos)/2}
1532 % \end{macrocode}
1533 % Add the curve and the arc.
1534 % \begin{macrocode}
1535 \xdef\tqft@fullpath{%
1536 \tqft@fullpath
1537 .. controls +(0,-\tqft@val{cobordism height}/3) and +(0,-\tqft@val{cobordism height}/3) .. (\xpos pt,0) arc[start angle=\pgf@tqft@upper180, end angle=0, x radius=\tqft@val{circle x radius}, y radius=\tqft@val{circle y radius}]
1538 }%
1539 % \end{macrocode}
1540 % But for the edge path, just add the curve to the list.
1541 % \begin{macrocode}
1542 \xdef\tqft@blist{%
1543 \tqft@blist,incoming boundary \k/incoming/{%
1544 (\xppos pt,0) .. controls +(0,-\tqft@val{cobordism height}/3) and +(0,-\tqft@val{cobordism height}/3) .. (\xpos pt,0)}%
1545 }%
1546 % \end{macrocode}
1547 % We add a coordinate at the midpoint of the curve.
1548 % \begin{macrocode}
1549 \xdef\tqft@clist{%
1550 \tqft@clist,-between incoming \kmo\space and \k/{(\cpos pt,-\tqft@val{cobordism height}/4)}%
1551 }%
1552 % \end{macrocode}
1553 % And add the centre to the list for available shifts.
1554 % \begin{macrocode}
1555 \xdef\tqft@alist{%
1556 \tqft@alist,-incoming boundary \k/{(\kmo * \tqft@val{boundary separation},0)}%
1557 }%
1558 }%
1559 \fi
1560 \fi
1561 % \end{macrocode}
1562 % We're at the edge of the last incoming boundary component.
1563 % What we do now depends on whether or not there are outgoing boundary components.
1564 % \begin{macrocode}
1565 \ifnum\tqft@val{outgoing boundary components}>0\relax
1566 % \end{macrocode}
1567 % There are.
1568 %
1569 % We start by adding a curve from the end of the last incoming to the last outgoing component to the full path,
1570 % \begin{macrocode}
1571 \pgfmathsetmacro\xppos{%
1572 (\tqft@outgoing@end * (\tqft@val{outgoing boundary components} -1)
1573 + \tqft@val{offset}) * \tqft@val{boundary separation}
1574 + \tqft@outgoing@dir * \tqft@val{circle x radius}}%
1575 \pgfmathsetmacro\tqft@ht{abs(\tqft@val{incoming boundary components} - \tqft@val{outgoing boundary components} - \tqft@val{offset})}%
1576 \pgfmathsetmacro\tqft@ht{1/3 + 2/3*\tqft@ht/(\tqft@ht + 1)}%
1577 \xdef\tqft@fullpath{%
1578 \tqft@fullpath
1579 .. controls +(0,-\tqft@ht*\tqft@val{cobordism height}) and +(0,\tqft@ht*\tqft@val{cobordism height}) .. (\xppos pt, -\tqft@val{cobordism height})
1580 }%
1581 % \end{macrocode}
1582 % and the edge path.
1583 % \begin{macrocode}
1584 \xdef\tqft@blist{%
1585 \tqft@blist,between last incoming and last outgoing/incoming and outgoing/{%
1586 (\tqft@val{incoming boundary components} * \tqft@val{boundary separation} + \tqft@val{circle x radius} - \tqft@val{boundary separation},0pt)
1587 .. controls +(0,-\tqft@ht*\tqft@val{cobordism height}) and +(0,\tqft@ht*\tqft@val{cobordism height}) .. (\xppos pt, -\tqft@val{cobordism height})}%
1588 }%
1589 % \end{macrocode}
1590 % In addition, we add a coordinate at the midpoint.
1591 % \begin{macrocode}
1592 \pgfmathsetmacro\xppos{(\xppos + (\tqft@val{incoming boundary components} -1) * \tqft@val{boundary separation} +\tqft@val{circle x radius})/2}%
1593 \xdef\tqft@clist{%
1594 \tqft@clist,-between last incoming and last outgoing/{(\xppos pt,-\tqft@val{cobordism height}/2)}%
1595 }%
1596 \else
1597 % \end{macrocode}
1598 % There aren't any outgoing boundary components so we loop back to the start.
1599 % We adjust the height of the control points to take into account the overall width.
1600 % \begin{macrocode}
1601 \pgfmathsetmacro\tqft@ht{1/3 + 2/3*(\tqft@val{incoming boundary components} - 1)/\tqft@val{incoming boundary components}}
1602 \xdef\tqft@fullpath{%
1603 \tqft@fullpath
1604 .. controls +(0,-\tqft@ht*\tqft@val{cobordism height}) and +(0,-\tqft@ht*\tqft@val{cobordism height}) .. (-\tqft@val{circle x radius},0)
1605 }%
1606 % \end{macrocode}
1607 % Same for the edge path.
1608 % \begin{macrocode}
1609 \xdef\tqft@blist{%
1610 \tqft@blist,between first and last incoming/incoming and outgoing/{%
1611 (\tqft@val{incoming boundary components} * \tqft@val{boundary separation} + \tqft@val{circle x radius} - \tqft@val{boundary separation},0pt)
1612 .. controls +(0,-\tqft@ht*\tqft@val{cobordism height}) and +(0,-\tqft@ht*\tqft@val{cobordism height}) .. (-\tqft@val{circle x radius},0)}
1613 }%
1614 % \end{macrocode}
1615 % Add a coordinate at the midpoint.
1616 % \begin{macrocode}
1617 \pgfmathsetmacro\xppos{(\tqft@val{incoming boundary components} -1) * \tqft@val{boundary separation}/2}%
1618 \xdef\tqft@clist{%
1619 \tqft@clist,-between first and last incoming/{(\xppos pt,-\tqft@ht*\tqft@val{cobordism height}*3/4)},-between first incoming and last incoming/{(\xppos pt,-\tqft@ht*\tqft@val{cobordism height}*3/4)}%
1620 }%
1621 \fi
1622 \else
1623 % \end{macrocode}
1624 % There weren't any incoming boundary components, so we test to see if there were any outgoing ones and move to the start of them.
1625 % \begin{macrocode}
1626 \ifnum\tqft@val{outgoing boundary components}>0\relax
1627 \pgfmathsetmacro\xppos{( \tqft@outgoing@end * (\tqft@val{outgoing boundary components} -1)+\tqft@val{offset}) * \tqft@val{boundary separation} + \tqft@outgoing@dir * \tqft@val{circle x radius}}
1628 % \end{macrocode}
1629 % Add a move to the full path,
1630 % \begin{macrocode}
1631 \xdef\tqft@fullpath{%
1632 \tqft@fullpath
1633 (\xppos pt, -\tqft@val{cobordism height})
1634 }%
1635 \fi
1636 \fi
1637 % \end{macrocode}
1638 % We're done with the incoming boundary components, now we're set up for the outgoing ones.
1639 % However we got there, if we have outgoing boundary components then we're now located at the start of them, although we might be counting backwards .
1640 % \begin{macrocode}
1641 \ifnum\tqft@val{outgoing boundary components}>0\relax
1642 \pgfmathsetmacro\xppos{%
1643 ( \tqft@outgoing@end * (\tqft@val{outgoing boundary components} -1)
1644 + \tqft@val{offset}) * \tqft@val{boundary separation}
1645 - \tqft@outgoing@dir * \tqft@val{circle x radius}}%
1646 % \end{macrocode}
1647 % Draw the arc for the first (well, last actually) boundary component.
1648 % \begin{macrocode}
1649 \xdef\tqft@fullpath{%
1650 \tqft@fullpath
1651 arc[end angle=\pgf@tqft@upper180, start angle=0, x radius=\tqft@outgoing@dir * \tqft@val{circle x radius}, y radius=\tqft@val{circle y radius}]
1652 }%
1653 % \end{macrocode}
1654 % And add the centre to the list for available shifts.
1655 % \begin{macrocode}
1656 \xdef\tqft@alist{%
1657 \tqft@alist,-outgoing boundary \tqft@val{outgoing boundary components}/{(\xppos pt + \tqft@outgoing@dir * \tqft@val{circle x radius},-\tqft@val{cobordism height})},-outgoing boundary/{(\tqft@val{offset}*\tqft@val{boundary separation},-\tqft@val{cobordism height})}%
1658 }%
1659 % \end{macrocode}
1660 % Do we have more than one boundary component?
1661 % \begin{macrocode}
1662 \ifnum\tqft@val{outgoing boundary components}>1\relax
1663 % \end{macrocode}
1664 % Yes, so add a curve and arc for each.
1665 % \begin{macrocode}
1666 \foreach \k [evaluate=\k as \ok using int(\tqft@outgoing@end * (\tqft@val{outgoing boundary components} + 1) - \tqft@outgoing@dir * \k)] in {2,...,\tqft@val{outgoing boundary components}} {
1667 \edef\tqft@temp{\noexpand\pgfutil@in@{,\ok,}{,\tqft@val{skip outgoing boundary components},}}
1668 \tqft@temp
1669 \ifpgfutil@in@
1670 \xdef\tqft@cobdrylist{\tqft@cobdrylist,\k}
1671 \else
1672 \xdef\tqft@obdrylist{\tqft@obdrylist,\k}
1673 \fi
1674 }
1675 \ifx\tqft@obdrylist\pgfutil@gobble
1676 \else
1677 \foreach \k [
1678 remember=\k as \kmo (initially 1),
1679 evaluate=\k as \xpos using ( \tqft@outgoing@end * (\tqft@val{outgoing boundary components} + 1) - 1 - \tqft@outgoing@dir * \k + \tqft@val{offset})*\tqft@val{boundary separation} + \tqft@outgoing@dir * \tqft@val{circle x radius},
1680 ] in \tqft@obdrylist {
1681 \pgfmathsetmacro\xppos{(\tqft@outgoing@end * (\tqft@val{outgoing boundary components} + 1) - 1 - \tqft@outgoing@dir * \kmo + \tqft@val{offset})*\tqft@val{boundary separation} - \tqft@outgoing@dir * \tqft@val{circle x radius}}
1682 \pgfmathsetmacro\cpos{(\xpos + \xppos)/2}
1683 \pgfmathsetmacro\nk{int(\tqft@val{outgoing boundary components} - \k + 1)}
1684 \pgfmathsetmacro\nkpo{int(\tqft@val{outgoing boundary components} - \kmo + 1)}
1685 % \end{macrocode}
1686 % Both are added to the full path.
1687 % \begin{macrocode}
1688 \xdef\tqft@fullpath{%
1689 \tqft@fullpath
1690 .. controls +(0,\tqft@val{cobordism height}/3) and +(0,\tqft@val{cobordism height}/3) .. (\xpos pt,-\tqft@val{cobordism height}) arc[end angle=\pgf@tqft@upper180, start angle=0, x radius=\tqft@outgoing@dir * \tqft@val{circle x radius}, y radius=\tqft@val{circle y radius}]
1691 }%
1692 % \end{macrocode}
1693 % Just the arc for the edge paths.
1694 % \begin{macrocode}
1695 \xdef\tqft@blist{%
1696 \tqft@blist,between outgoing \nk\space and \nkpo/outgoing/{%
1697 (\xppos pt,-\tqft@val{cobordism height})
1698 .. controls +(0,\tqft@val{cobordism height}/3) and +(0,\tqft@val{cobordism height}/3) .. (\xpos pt,-\tqft@val{cobordism height}) ++(-2*\tqft@val{circle x radius},0)}%
1699 }%
1700 % \end{macrocode}
1701 % And a coordinate at the midpoint.
1702 % \begin{macrocode}
1703 \xdef\tqft@clist{%
1704 \tqft@clist,-between outgoing \nk\space and \nkpo/{(\cpos pt,-3*\tqft@val{cobordism height}/4)}%
1705 }%
1706 % \end{macrocode}
1707 % And add the centre to the list for available shifts.
1708 % \begin{macrocode}
1709 \xdef\tqft@alist{%
1710 \tqft@alist,-outgoing boundary \nk/{(\xpos pt - \tqft@val{circle x radius},-\tqft@val{cobordism height})}%
1711 }%
1712 }%
1713 \fi
1714 \fi
1715 % \end{macrocode}
1716 % Now we're at the end of the outgoing boundary components (well, the start actually).
1717 % What we do now depends on whether or not there are any incoming boundary components.
1718 % \begin{macrocode}
1719 \ifnum\tqft@val{incoming boundary components}>0\relax
1720 % \end{macrocode}
1721 % There are, so we draw the path back up.
1722 % \begin{macrocode}
1723 \pgfmathsetmacro\tqft@ht{1/3 + 2/3*abs(\tqft@val{offset})/(abs(\tqft@val{offset}) + 1)}%
1724 \xdef\tqft@fullpath{%
1725 \tqft@fullpath
1726 .. controls +(0,\tqft@ht*\tqft@val{cobordism height}) and +(0,-\tqft@ht*\tqft@val{cobordism height}) .. (-\tqft@val{circle x radius},0)
1727 }%
1728 % \end{macrocode}
1729 % And the edge path does the same.
1730 % \begin{macrocode}
1731 \xdef\tqft@blist{%
1732 \tqft@blist,between first incoming and first outgoing/incoming and outgoing/{%
1733 ({ ( (1 - \tqft@outgoing@end) * (\tqft@val{outgoing boundary components} - 1) + \tqft@val{offset}) * \tqft@val{boundary separation} - \tqft@outgoing@dir * \tqft@val{circle x radius}},-\tqft@val{cobordism height})
1734 .. controls +(0,\tqft@ht*\tqft@val{cobordism height}) and +(0,-\tqft@ht*\tqft@val{cobordism height}) .. (-\tqft@val{circle x radius},0)}%
1735 }%
1736 % \end{macrocode}
1737 % Add a coordinate at the midpoint.
1738 % \begin{macrocode}
1739 \xdef\tqft@clist{%
1740 \tqft@clist,-between first incoming and first outgoing/{({ ( (1 - \tqft@outgoing@end) * (\tqft@val{outgoing boundary components} - 1) +\tqft@val{offset})*\tqft@val{boundary separation}/2- \tqft@outgoing@end * \tqft@val{circle x radius}},-\tqft@val{cobordism height}/2)}%
1741 }%
1742 \else
1743 % \end{macrocode}
1744 % No incoming boundary components so loop back to the other end of the outgoing boundary components.
1745 % \begin{macrocode}
1746 \pgfmathsetmacro\xppos{(\tqft@val{outgoing boundary components} -1+\tqft@val{offset}) * \tqft@val{boundary separation} +\tqft@val{circle x radius}}%
1747 \pgfmathsetmacro\tqft@ht{1/3 + 2/3*(\tqft@val{outgoing boundary components} - 1)/\tqft@val{outgoing boundary components}}
1748 % \end{macrocode}
1749 % Full path.
1750 % \begin{macrocode}
1751 \xdef\tqft@fullpath{%
1752 \tqft@fullpath
1753 .. controls +(0,\tqft@ht*\tqft@val{cobordism height}) and +(0,\tqft@ht*\tqft@val{cobordism height}) .. (\xppos pt,-\tqft@val{cobordism height})
1754 }%
1755 % \end{macrocode}
1756 % Edge path.
1757 % \begin{macrocode}
1758 \xdef\tqft@blist{%
1759 \tqft@blist,between first and last outgoing/incoming and outgoing/{%
1760 (\tqft@val{offset} * \tqft@val{boundary separation} - \tqft@val{circle x radius},-\tqft@val{cobordism height})
1761 .. controls +(0,\tqft@ht*\tqft@val{cobordism height}) and +(0,\tqft@ht*\tqft@val{cobordism height}) .. (\xppos pt,-\tqft@val{cobordism height})}%
1762 }%
1763 % \end{macrocode}
1764 % Add a coordinate at the midpoint.
1765 % \begin{macrocode}
1766 \pgfmathsetmacro\xppos{(\tqft@val{outgoing boundary components}/2 + \tqft@val{offset} -1/2) * \tqft@val{boundary separation}}%
1767 \pgfmathsetmacro\tqft@ht{1 -\tqft@ht*3/4}%
1768 \xdef\tqft@clist{%
1769 \tqft@clist,-between first and last outgoing/{(\xppos pt,-\tqft@ht*\tqft@val{cobordism height})},-between first outgoing and last outgoing/{(\xppos pt,-\tqft@ht*\tqft@val{cobordism height})}%
1770 }%
1771 \fi
1772 \fi
1773 % \end{macrocode}
1774 % Now we define the clip path for the genus holes.
1775 % We start with a big rectangle that \emph{ought} to be big enough to contain the whole shape.
1776 % We start with the top left corner.
1777 % \begin{macrocode}
1778 \pgfmathsetmacro\xpos{%
1779 (
1780 \tqft@val{outgoing boundary components} > 0 ?
1781 (
1782 \tqft@val{incoming boundary components} > 0 ?
1783 min(0,\tqft@val{offset}) : \tqft@val{offset}
1784 ) : 0
1785 )
1786 *\tqft@val{boundary separation} - 2*\tqft@val{circle x radius}}%
1787 \xdef\tqft@gclip{(\xpos pt,2*\tqft@val{circle y radius}) rectangle }%
1788 % \end{macrocode}
1789 % Now the bottom right.
1790 % \begin{macrocode}
1791 \pgfmathsetmacro\xpos{%
1792 ((
1793 \tqft@val{outgoing boundary components} > 0 ?
1794 (
1795 \tqft@val{incoming boundary components} > 0 ?
1796 max(\tqft@val{incoming boundary components},\tqft@val{outgoing boundary components} + \tqft@val{offset}) : \tqft@val{outgoing boundary components} + \tqft@val{offset}
1797 ) : \tqft@val{incoming boundary components}
1798 )-1)
1799 *\tqft@val{boundary separation} + 2*\tqft@val{circle x radius}}%
1800 % \end{macrocode}
1801 % Together, these make a rectangle.
1802 % \begin{macrocode}
1803 \xdef\tqft@gclip{\tqft@gclip (\xpos pt,-\tqft@val{cobordism height} - 2*\tqft@val{circle y radius})}%
1804 % \end{macrocode}
1805 % Are there any holes?
1806 % \begin{macrocode}
1807 \ifnum\tqft@val{genus}>0\relax
1808 % \end{macrocode}
1809 % Yes, so first we need to figure out where to place them.
1810 % We work out the left-hand edge of the cobordism.
1811 % \begin{macrocode}
1812 \pgfmathsetmacro\xpos{%
1813 (
1814 \tqft@val{outgoing boundary components} > 0 ?
1815 (
1816 \tqft@val{incoming boundary components} > 0 ?
1817 \tqft@val{offset}/2 : \tqft@val{offset}
1818 ) : 0
1819 )
1820 *\tqft@val{boundary separation} - \tqft@val{circle x radius}}%
1821 % \end{macrocode}
1822 % Work out the height that the holes should be punched at.
1823 % \begin{macrocode}
1824 \pgfmathsetmacro\ypos{%
1825 (
1826 \tqft@val{outgoing boundary components} > 0 ?
1827 (
1828 \tqft@val{incoming boundary components} > 0 ?
1829 -\tqft@val{cobordism height}/2 : -1 + \tqft@val{cobordism height}/3
1830 ) : - \tqft@val{cobordism height}/3
1831 )}%
1832 % \end{macrocode}
1833 % Start our clip path at this point
1834 % \begin{macrocode}
1835 \xdef\tqft@gclip{%
1836 \tqft@gclip
1837 (\xpos pt,\ypos pt)
1838 }%
1839 % \end{macrocode}
1840 % Now work out the width of the cobordism, in units of circle half-widths.
1841 % This may not be very accurate if there aren't any boundary components of a given type.
1842 % \begin{macrocode}
1843 \pgfmathsetmacro\gsize{%
1844 ((
1845 \tqft@val{outgoing boundary components} > 0 ?
1846 (
1847 \tqft@val{incoming boundary components} > 0 ?
1848 (\tqft@val{incoming boundary components} + \tqft@val{outgoing boundary components})/2 : \tqft@val{outgoing boundary components}
1849 ) : \tqft@val{incoming boundary components}
1850 )-1)
1851 *\tqft@val{boundary separation}/\tqft@val{circle x radius} + 2}%
1852 % \end{macrocode}
1853 % Each hole should take up three half-widths, but we want a little extra on the edges so the total number of half-widths we want is \(3g + 1\).
1854 % Do we need to scale down the holes (we never scale up)?
1855 % If so, \verb+\gscale+ holds the overall scale factor and \verb+\gxscale+ and \verb+gyscale+ are the resulting horizontal and vertical measurements.
1856 % The baseline is the size of the boundary circles.
1857 % \begin{macrocode}
1858 \pgfmathsetmacro\gscale{min(1,\gsize/(3*\tqft@val{genus}+1))}%
1859 \pgfmathsetmacro\gyscale{\tqft@val{circle y radius}*\gscale*.707}%
1860 \pgfmathsetmacro\gxscale{\tqft@val{circle x radius}*\gscale}%
1861 % \end{macrocode}
1862 % Each hole should take up 2 half widths, modulo scaling, so the total width used by the holes is \(2 g s\) leaving \(w - 2 g s\) left for the gaps which is divided in to \(g + 1\) lots.
1863 % \begin{macrocode}
1864 \pgfmathsetmacro\gsep{((\gsize - 2*\tqft@val{genus}*\gscale)/(\tqft@val{genus} + 1)*\tqft@val{circle x radius}}%
1865 % \end{macrocode}
1866 % We shift in by half of one unit of excess separation.
1867 % \begin{macrocode}
1868 \xdef\tqft@gclip{%
1869 \tqft@gclip
1870 ++(\gsep/2 pt,0)
1871 }%
1872 % \end{macrocode}
1873 % Some useful quantities.
1874 % \begin{macrocode}
1875 \pgfmathsetmacro\omrstwo{1 - 1/sqrt(2)}%
1876 \pgfmathsetmacro\sqrtwo{sqrt(2)}%
1877 % \end{macrocode}
1878 % Now we iterate over the holes.
1879 % \begin{macrocode}
1880 \foreach[
1881 evaluate=\k as \kmo using int(2 * \k-1)
1882 ] \k in {1,...,\tqft@val{genus}} {
1883 % \end{macrocode}
1884 % For the clipping path, we just want the bare hole.
1885 % \begin{macrocode}
1886 \xdef\tqft@gclip{%
1887 \tqft@gclip
1888 % \end{macrocode}
1889 % Move in by half an excess separation unit and move to the left-hand extent of the hole.
1890 % \begin{macrocode}
1891 ++(\gsep/2 pt + \omrstwo*\gxscale pt,0)
1892 % \end{macrocode}
1893 % Now curve up over the hole,
1894 % \begin{macrocode}
1895 .. controls +(\gxscale*\sqrtwo/3 pt,4/3*\gyscale pt) and +(-\gxscale*\sqrtwo/3 pt,4/3*\gyscale pt)
1896 .. ++(\sqrtwo*\gxscale pt,0)
1897 % \end{macrocode}
1898 % and return on the underside.
1899 % \begin{macrocode}
1900 .. controls +(-\gxscale*\sqrtwo/3 pt,-4/3*\gyscale pt) and +(\gxscale*\sqrtwo/3 pt,-4/3*\gyscale pt)
1901 .. ++(-\sqrtwo*\gxscale pt,0)
1902 % \end{macrocode}
1903 % Lastly, move to the right-hand edge of the space taken up by this hole.
1904 % \begin{macrocode}
1905 ++(2*\gxscale pt -\omrstwo*\gxscale pt + \gsep/2 pt,0)
1906 }
1907 % \end{macrocode}
1908 % For the genus \emph{path} we want to add the little ``tails'' which means that the two curves are different, and we need to take into acount the \verb+view from+ direction.
1909 % \begin{macrocode}
1910 \xdef\tqft@glist{%
1911 \tqft@glist,%
1912 hole \k/lower/{%
1913 % \end{macrocode}
1914 % Move to the starting point of the smaller curve and add that.
1915 % \begin{macrocode}
1916 (\xpos pt + \k * \gsep pt + \kmo * \gxscale pt + \gxscale pt -\omrstwo*\gxscale pt,\ypos pt)
1917 .. controls +(-\gxscale pt*\sqrtwo/3,\pgf@tqft@upper4/3*\gyscale pt) and +(\gxscale pt*\sqrtwo/3,\pgf@tqft@upper4/3*\gyscale pt)
1918 .. ++(-\sqrtwo*\gxscale pt,0)},%
1919 % Move to the left-hand corner of the path, with the upper or lower chosen by the \verb+view from+ direction.
1920 hole \k/upper/{(\xpos pt + \k * \gsep pt + \kmo * \gxscale pt - \gxscale pt,\ypos pt + \pgf@tqft@upper\gyscale pt)%
1921 % \end{macrocode}
1922 % Add the larger of the two curves.
1923 % \begin{macrocode}
1924 .. controls +(\gxscale pt*2/3,\pgf@tqft@lower8/3*\gyscale pt) and +(-\gxscale pt*2/3,\pgf@tqft@lower8/3*\gyscale pt)
1925 .. ++(2*\gxscale pt,0)}%
1926 }%
1927 % \end{macrocode}
1928 % Add a coordinate at the centre of the hole.
1929 % \begin{macrocode}
1930 \xdef\tqft@clist{%
1931 \tqft@clist,-hole \k/{(\xpos pt + \k * \gsep pt + \kmo * \gxscale pt,\ypos pt)}%
1932 }%
1933 }%
1934 \fi
1935 % \end{macrocode}
1936 % Now we start to lay out the cobordism
1937 % Were we given a shift? If so, shift.
1938 % \begin{macrocode}
1939 \gdef\tqft@shift{(0,0)}%
1940 \edef\tqft@anchor{\tqft@val{anchor}}%
1941 \expandafter\pgfutil@in@\expandafter{\expandafter,\expandafter}\expandafter{\tqft@anchor}%
1942 \ifpgfutil@in@
1943 \expandafter\tqft@split\tqft@anchor\relax
1944 \else
1945 \edef\tqft@anchor{-\tqft@val{anchor}}%
1946 \xdef\tqft@alist{\tqft@clist,\tqft@alist}%
1947 \foreach \anchor/\coord in \tqft@alist
1948 {
1949 \ifx\anchor\tqft@anchor\relax
1950 \global\let\tqft@shift\coord
1951 \fi
1952 }%
1953 \fi
1954 \tikz@scan@one@point\pgfutil@firstofone\tqft@shift\relax
1955 \begin{scope}[shift={(-\pgf@x,-\pgf@y)}]
1956 % \end{macrocode}
1957 % At each incoming boundary component we place an elliptical node of the right size.
1958 % \begin{macrocode}
1959 \ifnum\tqft@val{incoming boundary components}>0\relax
1960 \ifx\tqft@ibdrylist\pgfutil@gobble
1961 \xdef\tqft@ibdrylist{1}
1962 \else
1963 \xdef\tqft@ibdrylist{1,\tqft@ibdrylist}
1964 \fi
1965 \foreach[evaluate=\k as \xpos using (\k-1)*\tqft@val{boundary separation}] \k in \tqft@ibdrylist {
1966 \node[
1967 transform shape,
1968 node contents={},
1969 ellipse,
1970 inner sep=0pt,
1971 outer sep=0pt,
1972 minimum width=2*\tqft@val{circle x radius},
1973 minimum height=2*\tqft@val{circle y radius},
1974 at={(\xpos pt,0)},
1975 name=-incoming boundary \k,
1976 /tikz/tqft/every boundary component/.try,
1977 /tikz/tqft/every incoming boundary component/.try,
1978 /tikz/tqft/incoming boundary component \k/.try
1979 ];
1980 }%
1981 \ifx\tqft@cibdrylist\pgfutil@gobble
1982 \else
1983 \foreach[evaluate=\k as \xpos using (\k-1)*\tqft@val{boundary separation}] \k in \tqft@cibdrylist {
1984 \node[
1985 transform shape,
1986 node contents={},
1987 ellipse,
1988 inner sep=0pt,
1989 outer sep=0pt,
1990 minimum width=2*\tqft@val{circle x radius},
1991 minimum height=2*\tqft@val{circle y radius},
1992 at={(\xpos pt,0)},
1993 name=-incoming boundary \k,
1994 /tikz/tqft/every skipped boundary component/.try,
1995 /tikz/tqft/every skipped incoming boundary component/.try,
1996 /tikz/tqft/skipped incoming boundary component \k/.try,
1997 ];
1998 }%
1999 \fi
2000 % \end{macrocode}
2001 % Add an alias for the first.
2002 % \begin{macrocode}
2003 \path node also[pic alias=-incoming boundary] (-incoming boundary 1);
2004 \fi
2005 % \end{macrocode}
2006 % Same for the outgoing boundary components.
2007 % \begin{macrocode}
2008 \ifnum\tqft@val{outgoing boundary components}>0\relax
2009 \ifx\tqft@obdrylist\pgfutil@gobble
2010 \xdef\tqft@obdrylist{1}
2011 \else
2012 \xdef\tqft@obdrylist{1,\tqft@obdrylist}
2013 \fi
2014 \foreach \k [evaluate=\k as \ok using int(\tqft@outgoing@end * (\tqft@val{outgoing boundary components} + 1) - \tqft@outgoing@dir * \k)] in \tqft@obdrylist {
2015 \xdef\tqft@robdrylist{\tqft@robdrylist,\ok}
2016 }
2017 \foreach[
2018 evaluate=\k as \xpos using (\k-1+\tqft@val{offset})*\tqft@val{boundary separation}
2019 ] \k in \tqft@robdrylist {
2020 \node[
2021 transform shape,
2022 node contents={},
2023 ellipse,
2024 inner sep=0pt,
2025 outer sep=0pt,
2026 minimum width=2*\tqft@val{circle x radius},
2027 minimum height=2*\tqft@val{circle y radius},
2028 at={(\xpos pt,-\tqft@val{cobordism height})},
2029 name=-outgoing boundary \k,
2030 /tikz/tqft/every boundary component/.try,
2031 /tikz/tqft/every outgoing boundary component/.try,
2032 /tikz/tqft/outgoing boundary component \k/.try
2033 ];
2034 }%
2035 \ifx\tqft@cobdrylist\pgfutil@gobble
2036 \else
2037 \foreach \k[evaluate=\k as \ok using int(\tqft@outgoing@end * (\tqft@val{outgoing boundary components} + 1) - \tqft@outgoing@dir * \k)]
2038 in \tqft@cobdrylist {
2039 \xdef\tqft@rcobdrylist{\tqft@rcobdrylist,\ok}
2040 }
2041 \foreach[
2042 evaluate=\k as \xpos using (\k-1+\tqft@val{offset})*\tqft@val{boundary separation}
2043 ] \k in \tqft@rcobdrylist {
2044 \node[
2045 transform shape,
2046 node contents={},
2047 ellipse,
2048 inner sep=0pt,
2049 outer sep=0pt,
2050 minimum width=2*\tqft@val{circle x radius},
2051 minimum height=2*\tqft@val{circle y radius},
2052 at={(\xpos pt,-\tqft@val{cobordism height})},
2053 name=-outgoing boundary \k,
2054 /tikz/tqft/every skipped boundary component/.try,
2055 /tikz/tqft/every skipped outgoing boundary component/.try,
2056 /tikz/tqft/skipped outgoing boundary component \k/.try
2057 ];
2058 }%
2059 \fi
2060 % \end{macrocode}
2061 % Add an alias for the first.
2062 % \begin{macrocode}
2063 \path node also[pic alias=-outgoing boundary] (-outgoing boundary 1);
2064 \fi
2065 % \end{macrocode}
2066 % Now we draw the lower paths of the incoming boundary components.
2067 % \begin{macrocode}
2068 \ifnum\tqft@val{incoming boundary components}>0\relax
2069 \foreach[evaluate=\k as \xpos using (\k-1)*\tqft@val{boundary separation}] \k in \tqft@ibdrylist {
2070 \path[
2071 /tikz/tqft/every lower boundary component/.try,
2072 /tikz/tqft/every incoming lower boundary component/.try,
2073 /tikz/tqft/incoming lower boundary component \k/.try
2074 ] (\xpos pt - \tqft@val{circle x radius},0) arc[start angle=\pgf@tqft@lower180,end angle=0, x radius=\tqft@val{circle x radius}, y radius =\tqft@val{circle y radius}];
2075 }%
2076 \fi
2077 % \end{macrocode}
2078 % Same for the outgoing boundary components.
2079 % \begin{macrocode}
2080 \ifnum\tqft@val{outgoing boundary components}>0\relax
2081 \foreach[
2082 evaluate=\k as \xpos using (\k-1+\tqft@val{offset})*\tqft@val{boundary separation}
2083 ] \k in \tqft@robdrylist {
2084 \path[
2085 /tikz/tqft/every lower boundary component/.try,
2086 /tikz/tqft/every outgoing lower boundary component/.try,
2087 /tikz/tqft/outgoing lower boundary component \k/.try
2088 ] (\xpos pt - \tqft@val{circle x radius},-\tqft@val{cobordism height}) arc[start angle=\pgf@tqft@lower180,end angle=0, x radius=\tqft@val{circle x radius}, y radius =\tqft@val{circle y radius}];
2089 }%
2090 \fi
2091 % \end{macrocode}
2092 % Full outer path, clipped against the genus holes in case it is filled.
2093 % \begin{macrocode}
2094 \begin{scope}
2095 \path[overlay,clip] \tqft@gclip;
2096 \path[
2097 /tikz/tqft/cobordism/.try,
2098 pic actions,
2099 /tikz/tqft/cobordism outer path/.try,
2100 ] \tqft@fullpath;
2101 \end{scope}
2102 % \end{macrocode}
2103 % Now we draw the genus path, outside the clip.
2104 % We view this as part of the full cobordism path so try to apply the same style as for the full path, but if that is filled then we turn the fill off.
2105 % It can be turned back on again using the styles \verb+cobordism edge+ or \verb+genus style+.
2106 % We also apply the \verb+cobordism edge+ style as it could be thought of as part of the non-boundary edge.
2107 % Finally, it has its own style to enable overrides if the other two get confused.
2108 % \begin{macrocode}
2109 \ifx\tqft@glist\pgfutil@gobble
2110 \else
2111 \foreach \tqft@gstyle/\tqft@gside/\tqft@gpath in \tqft@glist {
2112 \path[
2113 /tikz/tqft/cobordism/.try,
2114 pic actions,
2115 fill=none,
2116 shade=none,
2117 /tikz/tqft/cobordism edge/.try,
2118 /tikz/tqft/genus style/.try,
2119 /tikz/tqft/genus \tqft@gside/.try,
2120 /tikz/tqft/\tqft@gstyle/.try,
2121 /tikz/tqft/\tqft@gstyle\space\tqft@gside/.try,
2122 ] \tqft@gpath;
2123 }
2124 \fi
2125 % \end{macrocode}
2126 % Now we redraw the non-boundary paths.
2127 % \begin{macrocode}
2128 \ifx\tqft@blist\pgfutil@gobble
2129 \else
2130 \foreach \tqft@bstyle/\tqft@btype/\tqft@bpath in \tqft@blist {
2131 \path[
2132 /tikz/tqft/cobordism edge/.try,
2133 /tikz/tqft/cobordism outer edge/.try,
2134 /tikz/tqft/between \tqft@btype/.try,
2135 /tikz/tqft/\tqft@bstyle/.try,
2136 ] \tqft@bpath;
2137 }
2138 \fi
2139 % \end{macrocode}
2140 % There were various coordinates that we wanted to define but couldn't.
2141 % Here, we put those in place.
2142 % \begin{macrocode}
2143 \ifx\tqft@clist\pgfutil@gobble
2144 \else
2145 \foreach \name/\coord in \tqft@clist {
2146 \path \coord node[coordinate,node contents={},name=\name];
2147 }
2148 \fi
2149 % \end{macrocode}
2150 % The last task is to draw the upper paths of the boundary components.
2151 % First, incoming.
2152 % \begin{macrocode}
2153 \ifnum\tqft@val{incoming boundary components}>0\relax
2154 \foreach[evaluate=\k as \xpos using (\k-1)*\tqft@val{boundary separation}] \k in \tqft@ibdrylist {
2155 \path[
2156 /tikz/tqft/every upper boundary component/.try,
2157 /tikz/tqft/every incoming upper boundary component/.try,
2158 /tikz/tqft/incoming upper boundary component \k/.try
2159 ] (\xpos pt - \tqft@val{circle x radius},0) arc[start angle=\pgf@tqft@upper180,end angle=0, x radius=\tqft@val{circle x radius}, y radius =\tqft@val{circle y radius}];
2160 }
2161 \fi
2162 % \end{macrocode}
2163 % Next, outgoing.
2164 % \begin{macrocode}
2165 \ifnum\tqft@val{outgoing boundary components}>0\relax
2166 \foreach[
2167 evaluate=\k as \xpos using (\k-1+\tqft@val{offset})*\tqft@val{boundary separation}
2168 ] \k in \tqft@robdrylist {
2169 \path[
2170 /tikz/tqft/every upper boundary component/.try,
2171 /tikz/tqft/every outgoing upper boundary component/.try,
2172 /tikz/tqft/outgoing upper boundary component \k/.try
2173 ] (\xpos pt - \tqft@val{circle x radius},-\tqft@val{cobordism height}) arc[start angle=\pgf@tqft@upper180,end angle=0, x radius=\tqft@val{circle x radius}, y radius =\tqft@val{circle y radius}];
2174 }
2175 \fi
2176 \end{scope}
2177 % \end{macrocode}
2178 % We're done!
2179 % Phew.
2180 % \begin{macrocode}
2181 }
2182 }
2183 % \end{macrocode}
2184 %
2185 % \iffalse
2186 %</library>
2187 % \fi
2188 % \Finale
2189
2190 \endinput