Debian Codegraph @

texmf-dist/source/latex/string-diagrams/string-diagrams.dtx

raw
1 % \iffalse meta-comment
2 %% =============================================================================
3 %%
4 %% string-diagrams 0.2.1 (2023/06/13)
5 %%
6 %% Copyright (C) 2023 by Paolo Brasolin <[email protected]>
7 %% SPDX-License-Identifier: LPPL-1.3c
8 %%
9 %% =============================================================================
10 %%
11 %% This work may be distributed and/or modified under the
12 %% conditions of the LaTeX Project Public License, either version 1.3c
13 %% of this license or (at your option) any later version.
14 %% The latest version of this license is in
15 %% https://www.latex-project.org/lppl.txt
16 %% and version 1.3c or later is part of all distributions of LaTeX
17 %% version 2008 or later.
18 %%
19 %% This work has the LPPL maintenance status `author-maintained'.
20 %%
21 %% The Current Maintainer of this work is Paolo Brasolin.
22 %%
23 %% This work consists of the files README.md,
24 %% string-diagrams.dtx,
25 %% and the derived files string-diagrams.ins,
26 %% string-diagrams.sty, and
27 %% string-diagrams.pdf.
28 %%
29 %% =============================================================================
30 %%
31 %
32 % %%%=[ INSTALL ]===============================================================
33 %
34 %<*internal>
35 \def\nameofplainTeX{plain}
36 \ifx\fmtname\nameofplainTeX\else
37 \expandafter\begingroup
38 \fi
39 %</internal>
40 %
41 %<*install>
42 \input l3docstrip.tex
43 \keepsilent
44 \askforoverwritefalse
45
46 % NOTE: to avoid redundancy we use `%%'-lines instead of pre/postambles
47 \preamble
48 \endpreamble
49 \nopostamble
50
51 \usedir{tex/latex/string-diagrams}
52 \generate{\file{\jobname.sty}{\from{\jobname.dtx}{package}}}
53
54 %</install>
55 %<*internal>
56
57 \usedir{source/latex/string-diagrams}
58 \generate{\file{\jobname.ins}{\from{\jobname.dtx}{install}}}
59
60 %</internal>
61 %
62 %<*internal>
63 \ifx\fmtname\nameofplainTeX
64 \expandafter\endbatchfile
65 \else
66 \expandafter\endgroup
67 \fi
68 %</internal>
69 %
70 %<install>\endbatchfile
71 %
72 % %%%=[ DRIVER ]================================================================
73 %
74 %<*driver>
75 \documentclass[a4paper,full]{l3doc}
76 \usepackage{parskip}
77
78 \NewDocumentCommand{\TikZ}{}{Ti\emph{k}Z}
79
80 \EnableCrossrefs
81 \CodelineIndex
82 \RecordChanges
83 \usepackage{string-diagrams}
84 \usetikzlibrary{calc}
85 \usepackage{snapshot}
86
87 \usepackage{tcolorbox}
88 \tcbuselibrary{listings, skins}
89
90 \lstset{
91 language=[LaTeX]TeX,
92 basicstyle=\MacroFont,
93 columns=flexible,
94 ^^A keywordstyle=\color{red},
95 ^^A morekeywords={},
96 texcsstyle=*\color{violet},
97 moretexcs={node,wires},
98 breaklines=true,
99 }
100
101 \tcbset{example/.style={
102 listing engine=listings,
103 verbatim ignore percent=true,
104 listing side text,
105 size=minimal,
106 skin=bicolor,
107 colback=black!5!white,
108 colbacklower=white,
109 sidebyside,
110 lefthand ratio=0.62,
111 listing options={
112 xleftmargin=-1.6em, % poor man's gobble=4
113 },
114 }}
115
116 \begin{document}
117 \DocInput{\jobname.dtx}
118 \PrintChanges
119 \PrintIndex
120 \end{document}
121 %</driver>
122 %
123 % \fi
124 %
125 % \changes{0.1.0}{2023/05/31}{initial version}
126 % \changes{0.2.0}{2023/06/12}{make box ports configurable}
127 %
128 % \GetFileInfo{\jobname.sty}
129 %
130 % \title{^^A
131 % The \pkg{\jobname} package^^A
132 % \thanks{Thanks!}\\^^A
133 % \fileinfo^^A
134 % }
135 %
136 % \author{^^A
137 % Paolo Brasolin\\^^A
138 % \texttt{\href{mailto:[email protected]}{[email protected]}}^^A
139 % }
140 %
141 % \date{\fileversion~(\filedate)}
142 %
143 % \maketitle
144 %
145 % ^^A=[ DOCUMENTATION ]=========================================================
146 %
147 % \begin{documentation}
148 %
149 % \begin{tcolorbox}[
150 % colback=red!5!white,
151 % colframe=red,
152 % sharp corners,
153 % boxrule=1pt,
154 % ]
155 % Please note this is the \href{https://semver.org/#spec-item-4}{major version zero}, meant for initial development: \emph{anything MAY change at any time}.
156 % The upside is that this is the best time to \href{https://github.com/paolobrasolin/string-diagrams#contributing}{contribute}!
157 % Of course you can also just keep the \texttt{sty} along with your code and not care at all.
158 % \end{tcolorbox}
159 %
160 %
161 % \section{Documentation}
162 %
163 % \begin{function}[added=2023-05-31,updated=2023-06-12]{/pgf/box}
164 %
165 % To draw boxes, you use this style on a node.
166 %
167 % \begin{tcblisting}{example}
168 % \begin{tikzpicture}
169 % \node[box] {A};
170 % \end{tikzpicture}
171 % \end{tcblisting}
172 %
173 % You can draw multiple boxes using any of your standard \TikZ\ positioning techniques.
174 % Don't forget to label the nodes so you can easily reference them.
175 %
176 % \begin{tcblisting}{example}
177 % \begin{tikzpicture}
178 % \node[box] (A) at (0,0) {A};
179 % \node[box, right of=A] (B) {B};
180 % \node[box] (C) at ($(B)+(2cm,1em)$) {C};
181 % \end{tikzpicture}
182 % \end{tcblisting}
183 % \end{function}
184 %
185 % \begin{function}[added=2023-06-12]{
186 % /pgf/box ports north,
187 % /pgf/box ports east,
188 % /pgf/box ports south,
189 % /pgf/box ports west,
190 % }
191 % \begin{syntax}
192 % /pgf/box ports north=\meta{integer}
193 % /pgf/box ports east=\meta{integer}
194 % /pgf/box ports south=\meta{integer}
195 % /pgf/box ports west=\meta{integer}
196 % \end{syntax}
197 %
198 % You can open up any number of ports on any side of a box using the appropriate key.
199 % Then, you can refer to the opened ports by their index.
200 %
201 % \begin{tcblisting}{example,lefthand ratio=0.8}
202 % \begin{tikzpicture}[
203 % marker/.style={circle, fill, inner sep=1pt, text=white},
204 % ]
205 %
206 % \node[
207 % box,
208 % box ports north=3,
209 % box ports east=3,
210 % box ports south=3,
211 % box ports west=3,
212 % minimum width=6em,
213 % minimum height=6em,
214 % ] (A) {A};
215 %
216 % \foreach \side in {north,east,south,west}
217 % \foreach \index in {1,...,3}
218 % \node[marker] at (A.\side.\index) {\index};
219 %
220 % \end{tikzpicture}
221 % \end{tcblisting}
222 %
223 % \end{function}
224 %
225 % \begin{function}[added=2023-06-12]{
226 % /pgf/box ports,
227 % }
228 % \begin{syntax}
229 % /pgf/box ports=\meta{integer}/\meta{integer}/\meta{integer}/\meta{integer}
230 % \end{syntax}
231 %
232 % The \texttt{box ports} key is a shortcut to set the number of ports on all sides at once.
233 %
234 % \begin{tcblisting}{example,lefthand ratio=0.8}
235 % \begin{tikzpicture}[
236 % marker/.style={circle, fill, inner sep=1pt},
237 % ]
238 %
239 % \node[box, box ports=1/2/3/4] (A) {A};
240 %
241 % \foreach \side/\n in {north/1,east/2,south/3,west/4}
242 % \foreach \index in {1,...,\n}
243 % \node[marker] at (A.\side.\index) {};
244 %
245 % \end{tikzpicture}
246 % \end{tcblisting}
247 %
248 % The same value can also be passed to the \texttt{box} key itself.
249 %
250 % \end{function}
251 %
252 % \begin{function}[added=2023-05-31,updated=2023-06-13]{\wires}
253 % \begin{syntax}
254 % \cs{wires}\oarg{\TikZ\ keys}\marg{connectivity}\marg{loose ends}
255 % \end{syntax}
256 %
257 % To connect boxes, you can use the \cmd\wires\ macro.
258 % The first argument is \TikZ\ styling for the wires; the second argument is a nested dicionary specifying the connectivity; the third argument is a list of the loose ends to draw.
259 % \texttt{box}es have the following anchors: \texttt{west}, \texttt{west.0}, \texttt{west.1}, \texttt{east}, \texttt{east.0}, and \texttt{east.1}.
260 %
261 % \begin{tcblisting}{example}
262 % \begin{tikzpicture}[scale=0.6]
263 % \node[box=0/2/0/1] (A) at (-2, 0) {A};
264 % \node[box=0/1/0/2] (B) at (+2, 0) {B};
265 % \node[box=0/1/0/1] (C) at ( 0,+1) {C};
266 % \node[box=0/1/0/1] (D) at ( 0,-1) {D};
267 % \wires{
268 % A = { east.1 = C.west, east.2 = D.west },
269 % C = { east = B.west.1 },
270 % D = { east = B.west.2 },
271 % }{ A.west, B.east }
272 % \end{tikzpicture}
273 % \end{tcblisting}
274 %
275 % \end{function}
276 %
277 % \begin{function}[added=2023-05-31]{/pgf/dot}
278 %
279 % To split and join wires, you can use \texttt{dot}s and their anchors \texttt{north}, \texttt{east}, \texttt{south}, and \texttt{west}.
280 % Remember to have fun with styling wires.
281 %
282 % \begin{tcblisting}{example}
283 % \begin{tikzpicture}
284 % \node[box=0/1/0/2] (A) at ( 0,+1) {A};
285 % \node[box=0/2/0/1] (B) at ( 0,-1) {B};
286 % \node[dot] (x) at (+1, 0) {};
287 % \node[dot] (y) at (-1, 0) {};
288 % \wires[looseness=1.5, dashed]{
289 % A = { east = x.north },
290 % B = { east.1 = x.south },
291 % y = { north = A.west.2, south = B.west },
292 % }{
293 % A.west.1, B.east.2, x.east, y.west
294 % }
295 % \end{tikzpicture}
296 % \end{tcblisting}
297 %
298 % \end{function}
299 %
300 % That's it. This is the package, for now.
301 %
302 % \end{documentation}
303 %
304 % ^^A=[ PACKAGE ]===============================================================
305 %
306 % \begin{implementation}
307 %
308 % \section{Implementation}
309 %
310 % Open the \pkg{DocStrip} guards and set the internal namespace prefix (as per \LaTeX3 \pkg{DocStrip} convention).
311 % \begin{macrocode}
312 %<*package>
313 %<@@=stridi>
314 % \end{macrocode}
315 %
316 % Load the essential support (\pkg{expl3}) \enquote{up-front}.
317 %
318 % \begin{macrocode}
319 \RequirePackage{expl3}[2023/05/11]
320 \RequirePackage{tikz}[2023/01/15]
321 % \end{macrocode}
322 %
323 % Identify the package and give the over all version information.
324 % \begin{macrocode}
325 \ProvidesExplPackage
326 {string-diagrams}
327 {2023/06/13}
328 {0.2.1}
329 {Draw string diagrams using TikZ}
330 % \end{macrocode}
331 %
332 % \begin{macro}{
333 % /pgf/box ports north,
334 % /pgf/box ports east,
335 % /pgf/box ports south,
336 % /pgf/box ports west,
337 % /pgf/box ports,
338 % }
339 %
340 % Define high level keys to configure the number of ports on each side.
341 % \begin{macrocode}
342 \pgfkeys{
343 /pgf/box~ports~north/.initial=1,
344 /pgf/box~ports~east/.initial=1,
345 /pgf/box~ports~south/.initial=1,
346 /pgf/box~ports~west/.initial=1,
347 /pgf/box~ports/.style~args={#1/#2/#3/#4}{
348 /pgf/box~ports~north=#1,
349 /pgf/box~ports~east=#2,
350 /pgf/box~ports~south=#3,
351 /pgf/box~ports~west=#4,
352 },
353 }
354 % \end{macrocode}
355 % \end{macro}
356 %
357 % \begin{macro}{\@@_intersect_hv_lines_through:NN}
358 % Calculates the intersection of two lines parallel to axes passing through given points on the plane.
359 % \begin{arguments}
360 % \item Point through which the vertical line passes
361 % \item Point through which the horizontal line passes
362 % \end{arguments}
363 % \begin{macrocode}
364 \cs_new:Nn \@@_intersect_hv_lines_through:NN {
365 \pgfextractx { \pgf@xa } { #1 }
366 \pgfextracty { \pgf@ya } { #2 }
367 \pgfpoint { \pgf@xa } { \pgf@ya }
368 }
369 % \end{macrocode}
370 % \end{macro}
371 %
372 % \begin{macro}{\@@_subdivide_segment:nNNNNN}
373 % Defines macros numbering equally spaced points on a segment.
374 % \begin{arguments}
375 % \item Base namespace
376 % \item Points count
377 % \item Point containing the x coordinate of the starting point
378 % \item Point containing the y coordinate of the starting point
379 % \item Point containing the x coordinate of the ending point
380 % \item Point containing the y coordinate of the ending point
381 % \end{arguments}
382 % \begin{macrocode}
383 \cs_new:Nn \@@_subdivide_segment:nNNNNN {
384 \int_step_inline:nnnn { #2 } { -1 } { 1 } {
385 \cs_if_exist:cTF
386 { #1.##1 }
387 { \prg_break: }
388 { \prg_do_nothing: }
389 \cs_new_nopar:cpn
390 { #1.##1 }
391 {
392 \pgfmathdivide
393 { 2 * ##1 - 1 }
394 { 2 * #2 }
395 \pgfpointlineattime
396 { \pgfmathresult }
397 { \@@_intersect_hv_lines_through:NN { #3 } { #4 } }
398 { \@@_intersect_hv_lines_through:NN { #5 } { #6 } }
399 }
400 }
401 }
402 % \end{macrocode}
403 % \end{macro}
404 %
405 % \begin{macro}{box}
406 % \changes{0.2.0}{2023/06/12}{make ports configurable through \TikZ\ keys}
407 %
408 % Define a rectangular shape with configurable ports.
409 % \begin{macrocode}
410 \pgfdeclareshape{box}{
411
412 % Inherit all the structure of rectangle
413 \inheritsavedanchors[from=rectangle]
414 \clist_map_inline:nn
415 {
416 north~west, north, north~east,
417 west, center, east,
418 mid~west, mid, mid~east,
419 base~west, base, base~east,
420 south~west, south, south~east,
421 }
422 { \inheritanchor[from=rectangle]{#1} }
423 \inheritanchorborder[from=rectangle]
424 \inheritbackgroundpath[from=rectangle]
425
426 % Dump port counts into saved macros
427 \savedmacro\portsnorth
428 {\pgfmathtruncatemacro\portsnorth{\pgfkeysvalueof{/pgf/box~ports~north}}}
429 \savedmacro\portseast
430 {\pgfmathtruncatemacro\portseast{\pgfkeysvalueof{/pgf/box~ports~east}}}
431 \savedmacro\portssouth
432 {\pgfmathtruncatemacro\portssouth{\pgfkeysvalueof{/pgf/box~ports~south}}}
433 \savedmacro\portswest
434 {\pgfmathtruncatemacro\portswest{\pgfkeysvalueof{/pgf/box~ports~west}}}
435
436 % Add ports definitions to shape definition
437 \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@box\endcsname{
438 \@@_subdivide_segment:nNNNNN { pgf@anchor@box@north } { \portsnorth }
439 { \southwest } { \northeast } { \northeast } { \northeast }
440 \@@_subdivide_segment:nNNNNN { pgf@anchor@box@east } { \portseast }
441 { \northeast } { \northeast } { \northeast } { \southwest }
442 \@@_subdivide_segment:nNNNNN { pgf@anchor@box@south } { \portssouth }
443 { \southwest } { \southwest } { \northeast } { \southwest }
444 \@@_subdivide_segment:nNNNNN { pgf@anchor@box@west } { \portswest }
445 { \southwest } { \northeast } { \southwest } { \southwest }
446 }
447
448 }
449 % \end{macrocode}
450 % \end{macro}
451 %
452 % \begin{macro}{/pgf/box}
453 % \changes{0.2.0}{2023/06/12}{acts as a shortcut for setting port counts}
454 %
455 % Define style to draw boxes.
456 % \begin{macrocode}
457 \ExplSyntaxOff
458 \tikzset{
459 box/.default={0/0/0/0},
460 box/.style args={#1}{
461 shape=box,
462 draw,
463 inner sep=.5em,
464 minimum width=2em,
465 minimum height=2em,
466 execute at begin node=$,
467 execute at end node=$,
468 /pgf/box ports=#1,
469 },
470 }
471 \ExplSyntaxOn
472 % \end{macrocode}
473 % \end{macro}
474 %
475 % \begin{macro}{/pgf/dot}
476 %
477 % Define style to draw dots.
478 % \begin{macrocode}
479 \ExplSyntaxOff
480 \tikzset{
481 dot/.style={
482 shape=circle,
483 fill,
484 inner sep=0,
485 minimum width=0.4em,
486 },
487 }
488 \ExplSyntaxOn
489 % \end{macrocode}
490 % \end{macro}
491 %
492 % \begin{macro}{\@@_draw_bound_wires:nn}
493 % Draws bound wires.
494 % \begin{arguments}
495 % \item \TikZ\ keys
496 % \item dictionary of port labels
497 % \end{arguments}
498 % \begin{macrocode}
499 \cs_new:Nn \@@_draw_bound_wires:nn {
500 \prop_set_from_keyval:Nn \l_tmpa_prop { #2 }
501 \prop_map_inline:Nn \l_tmpa_prop
502 {
503 \prop_set_from_keyval:Nn \l_tmpb_prop { ##2 }
504 \prop_map_inline:Nn \l_tmpb_prop
505 {
506 \regex_match_case:nn
507 {
508 { \. north } { \tl_gset:Nn \g_tmpa_tl { 90 } }
509 { \. south } { \tl_gset:Nn \g_tmpa_tl { -90 } }
510 { \. west } { \tl_gset:Nn \g_tmpa_tl { 180 } }
511 { \. east } { \tl_gset:Nn \g_tmpa_tl { 0 } }
512 } { ####2 }
513 \regex_match_case:nn
514 {
515 { north } { \tl_gset:Nn \g_tmpb_tl { 90 } }
516 { south } { \tl_gset:Nn \g_tmpb_tl { -90 } }
517 { west } { \tl_gset:Nn \g_tmpb_tl { 180 } }
518 { east } { \tl_gset:Nn \g_tmpb_tl { 0 } }
519 } { ####1 }
520 \draw [
521 out={\tl_use:N \g_tmpb_tl},
522 in={\tl_use:N \g_tmpa_tl},
523 #1,
524 ] (##1.####1) to (####2);
525 }
526 }
527 }
528 % \end{macrocode}
529 % \end{macro}
530 %
531 % \begin{macro}{\@@_draw_loose_wires:nn}
532 % Draws loose wires.
533 % \begin{arguments}
534 % \item \TikZ\ keys
535 % \item list of port labels
536 % \end{arguments}
537 % \begin{macrocode}
538 \cs_new:Nn \@@_draw_loose_wires:nn {
539 \clist_set:Nn \l_tmpa_clist { #2 }
540 \clist_map_inline:Nn \l_tmpa_clist {
541 \regex_match_case:nn
542 {
543 { \. north } { \draw[#1] (##1) -- +( 0,+1); } % TODO: cleaner solution?
544 { \. south }
545 {
546 \draw[out=-90, in=0,#1] (##1)
547 to ($(\pgf@picminx, \pgf@y)$);
548 } % TODO: not sure why this works
549 { \. west } { \draw[#1] (##1) -- +(-1, 0); }
550 { \. east } { \draw[#1] (##1) -- +(+1, 0); }
551 } { ##1 }
552 }
553 }
554 % \end{macrocode}
555 % \end{macro}
556 %
557 % \begin{macro}{\wires}
558 % \changes{0.2.1}{2023/06/13}{now correctly handles optional style parameter}
559 % Define our main actor.
560 % \begin{macrocode}
561 \NewDocumentCommand{\wires}{ O{} m m }
562 {
563 \@@_draw_bound_wires:nn { #1 } { #2 }
564 \@@_draw_loose_wires:nn { #1 } { #3 }
565 }
566 % \end{macrocode}
567 % \end{macro}
568 %
569 % Close the \pkg{DocStrip} guards and call it a day.
570 % \begin{macrocode}
571 %</package>
572 % \end{macrocode}
573 %
574 % \end{implementation}