# string-diagrams > string-diagrams is a LaTeX package that draws string diagrams with TikZ. It draws boxes with numbered ports, dots that split and join wires, and the wires between them, with labels. Wires can also go to a boundary around the diagram. This summary is for version 0.5.0. Load the package with `\usepackage{string-diagrams}`. The package loads TikZ and the `fit` and `quotes` libraries of TikZ. It needs LaTeX 2023-11-01 or later, as in TeX Live 2023. A diagram is a usual `tikzpicture` environment. Boxes and dots are TikZ nodes, and you can put them in any position. The `\wires` command draws the wires between them, and the `wire` key draws wires with the TikZ commands. Version 0.5.0 added boundaries that you can make, draw and give ports. In version 0.5.0, `@` is the boundary of a `\wires` command, and outside the command `@` is not a boundary. In version 0.5.0, each `\wires` command makes its own boundary. In earlier versions, all the `\wires` commands of a picture used one boundary. Version 0.4.0 added wires written as one item, wires without anchors, keys in brackets, and the `wire`, `wire flow`, `beyond` and `beside` keys. Versions before 0.3.0 do not have the boundary node `@`. With an older version, some examples below fail. To update, use `tlmgr update string-diagrams`, or put https://paolobrasolin.github.io/string-diagrams/string-diagrams.sty in the folder of the document. Online editors such as Overleaf can have an older version than CTAN. ## Reference ### Boxes - `\node[box] (f) {f};` draws a box. The label of the box is in math mode: write `{f}`, not `{$f$}`, because `{$f$}` breaks the node. For a label in text, use `box math=false` on the box, on a scope or on the picture. - `every box` is a style that `box` applies after its own keys. It is empty at first. Set it to style all the boxes of a picture, a scope or the document, as in `every box/.style={rounded corners, box math=false}`. It does not apply to a boundary. - `box ports north=`, `box ports east=`, `box ports south=` and `box ports west=` open `` ports on a side. A box has no ports until you open them. - `box ports=///` sets the four sides at the same time, and `box=///` does the same. Without counts, `box` keeps the counts that are set. Thus you can also set the keys on a picture or a scope, for all its boxes. - Ports are anchors: `A.east.1` is the first port on the east side of `A`. On the north and south sides, the ports go from left to right. On the east and west sides, they go from top to bottom. ### Dots - `\node[dot] (x) {};` draws a dot, which splits and joins wires. Wires attach to the sides and the corners of a dot. From a corner, a wire goes out diagonally. ### Wires - `\wires[]{}` draws wires. The wires are a list. Each item is one wire, from an anchor to an anchor: `. = .`. - Wires start and end at sides (`north`, `east`, `south`, `west`), at corners (`north east` and the other three) and at ports (`east.1`). From a side or a port, a wire goes straight out. From a corner, it goes out diagonally. You cannot use anchors without a direction, such as `center`. - Each `\wires` command has a boundary around its diagram. In the command, the name of the boundary is `@`. A wire to a side of `@`, such as `f.east = @.east`, is a loose end of the diagram. It goes straight out to that side, or it bends to it. - Wires that start in the same way can share the start. Write the start one time, then `=` and a group in braces with the remaining part of each wire. A start stops after the name of a node, or at the `=` of a wire. Thus a group can contain the anchors of the start node, as in `f = { north.1 = g.south.1, east = h.west }`, the targets of an anchor, as in `f.north.1 = { g.south.1, h.south.1 }`, or the anchors of a target node, as in `f.north.1 = { g = { south.1, south.2 } }`. A group can contain groups. Wires cannot share their end. In a group after a start node, an item without `=` is a target, as in `f = { g, h.south }`. Thus a name in that position is always a node. - You can write a node in more than one item, and an anchor can have more than one wire. - The keys of a `\wires` command apply to all its wires. Keys in brackets can also start an item, a group or a target, as in `f.north.1 = [dashed] g.south.1`. These keys apply to all the wires in it. The keys nearest to a wire apply last. The keys in brackets stop at the first `]` that is not in braces: thus put a key with brackets in braces, as in `[-{Stealth[length=6pt]}]`. - The keys apply after the angles that `\wires` calculates. Thus `out`, `in`, `bend left` and `bend right` change these angles. Each wire is a TikZ edge, so the keys also change its line. - The `every wire` style applies to all wires. The `every to` and `every edge` styles of the picture do not apply to the wires of `\wires`. - A `\wires` command without boundary keys makes a boundary of its own. This boundary is around the nodes of its wires and around their labels and pins, with a margin. Thus, two commands with different nodes have different boundaries, for example the two sides of an equation. If a command has no nodes, its boundary is around the picture as it is at that point. - To give one boundary to many `\wires` commands, use `same boundary` in all the commands after the first. If the boundary of the first command does not hold all the nodes, draw all the nodes first. Then make the boundary with `\node[boundary] (B) {};`, and use it in each command with `use boundary=B`. Then the loose ends of all the commands go to the same boundary. - A wire that crosses the boundary goes through its middle. To put such a wire at a different position, for example as an identity next to a box, connect a coordinate to the boundary in the two directions. Put the coordinate with `\path (x,y) coordinate (i);`, because `\coordinate` does not change the size of the picture. - Wires get their directions from the names of the anchors, in the frame of the picture. You can shift and scale a picture, but you cannot rotate or mirror it. The package does not support rotated nodes. - Up to version 0.2.1, `\wires` had a third argument with the loose ends. The package does not read this argument now: write wires to `@` instead. ### Wires without anchors - You can write a wire end without an anchor, as in `f = g` or `f.north.1 = g`. Then the end is the side of the node that the wire goes through. If this side has one port, the end is the port. If it has more ports, write the port. - The direction of the wire comes from the end that has an anchor. From a corner, the direction turns to the axis of the flow, or else to the axis with the larger distance between the two ends. - If the two ends have no anchor, the direction comes from the flow. Without a flow, it comes from the axis with the larger distance between the centers of the two nodes. Two nodes with the same center need a flow. Near a diagonal, a small movement of a node can change the axis: set a flow to keep it. - `wire flow=` sets the flow: `north`, `east`, `south` or `west`. `up`, `right`, `down` and `left` are the same directions, as for the `grow` key of TikZ. `none` removes the flow. You can set it on a picture, a scope, a `\wires` command, a group, a wire or a path. - The flow applies also when the target is behind the start: then the wire goes out in the direction of the flow, and turns back. A wire goes from its start to its target. - With a flow, loose ends do not need anchors: `@ = f` starts behind `f`, and `f = @` ends after `f`. ### Boundaries - `\node[boundary] (B) {};` makes a boundary. The boundary goes around the picture as it is at that point, unless you set `fit`. You can draw it, fill it and label it with the usual TikZ keys. Write these keys after `boundary`: the `boundary` key removes the stroke, the fill and the size that come before it. Thus, styles for all nodes, for example `every node`, do not change a boundary. It must have a name. - The margins of a boundary are its `inner xsep` and `inner ysep`. If you do not set them, they are 2.3333em and 1.3333em. The `scale` key does not change them. - `every boundary` is a style that `boundary` applies after its own keys. It is empty at first. Set it to style all the boundaries of a picture, a scope or the document, also the boundaries of the `\wires` commands. For example, `every boundary/.style={draw, dashed}` draws all the boundaries, and `every boundary/.style={inner ysep=2em}` makes the loose ends at the top and at the bottom longer. - On a boundary, `box=///` gives ports. It does not draw the boundary. Write `box` after `boundary`: the `boundary` key removes the style of a box that comes before it. A loose end goes to a port of a boundary as a wire goes to a port of a box: write the port, or write no anchor if the side has one port. A side without ports takes loose ends straight. - `\wires[boundary={}]{}` makes a boundary for this command, around the nodes of its wires and around their labels and pins, with the keys. In the command, `@` is this boundary. To use the boundary in other commands, give it a name with `name=`. Without a name, only this command uses it. A `fit` in the keys replaces the nodes of the wires: then the boundary goes around the nodes that `fit` names, and not around their labels. - `\wires[use boundary=]{}` uses a boundary that you made before. In the command, `@` is this boundary. - `\wires[same boundary]{}` uses the boundary of the `\wires` command before it in the same picture. Use it when many commands draw the wires of one diagram, for example with different styles. Then their loose ends go to the same boundary. This boundary does not grow: it must hold the nodes of all these commands. A command can have only one of `boundary`, `use boundary` and `same boundary`. - `\wires[boundary=none]{}` uses no boundary. Then the picture has no margins for loose ends, and a loose end is an error. - Outside `\wires`, `@` is not a boundary. To draw a wire to a boundary with the `wire` key, give the boundary a name, as in `\draw (f.north.1) to[wire] (B.north);`. - A boundary with ports is a box for the wires outside it. Thus you can put a diagram in a boundary, and connect the boundary as a box. - A fill of a boundary covers its nodes. Fill it on the background layer of the `backgrounds` library of TikZ. - Each boundary fits its nodes. To give the same height to two boundaries, set `minimum height`. ### Nodes in nodes - An end faces inwards if its node contains the other node: its box contains the box of the other node. Borders that touch count as inside. The wire then goes the other way at that end, and it goes straight across to a side without ports. - A boundary contains the nodes of its `\wires` command, thus its loose ends face inwards. A frame that you draw around a box does the same. - The box of a node is the rectangle between its `south west` and `north east` anchors. On a circle, this rectangle is smaller than the node. Thus use a rectangular node to contain other nodes. - If two nodes do not overlap, their ends face out, and the wire can turn back. If two nodes overlap and one does not contain the other, their ends also face out, but each end must have an anchor. - A boundary does not grow. A node that you draw after a boundary can be partly outside it. If a `\wires` command uses that boundary and connects the node to `@`, you get a warning, and the wires to `@` face out. - If a `\wires` command makes or uses a boundary, and a wire connects the boundary to a node that is not in it, you get a warning. - With a flow, a wire from a node to itself without anchors goes round the node: with `wire flow=east`, `f = f` is `f.east = f.west`. `@ = @` is `@.west = @.east`, across the boundary. Without a flow, such a wire is an error. - A wire between two ends at the same height of a node, such as `f.east.1 = f.west.1`, goes straight through the node. To go round it, give angles, as in `f = [out=60, in=120, looseness=2.5] f`. ### The wire key - `\draw (f.north.1) to[wire] (g.south.1);` and `\path (f) edge[wire] (g);` draw a wire with the TikZ commands, as `\wires` does. Ends without anchors are possible. - The `every wire` style applies after the `wire` key. On an edge it changes the line, as `every edge` does. On a `to` path it does not change the line, as `every to` does not. - Keys after `wire` change its angles. Keys before it do not. - An end of a `wire` path is a node or an anchor of a node. An end that you shift or calculate, such as `([yshift=1pt]f.north.1)`, is a point: the key gives an error and does not draw the wire. Put a coordinate at that position, and connect the coordinate. - To draw a wire to a boundary with the `wire` key, give the boundary a name. Outside `\wires`, `@` is not a boundary. ### Labels - The type of a wire goes along it. Use the quotes of TikZ: `f.north.1 = ["$A$"] g.south.1`. The label goes to the left of the wire, and `"$A$"'` puts it to the right. - The interface of a diagram goes at its loose ends. Use `beyond end`, or `beyond start` for a wire from the boundary: the label goes after the end of the wire, as in `f.north.1 = ["$A$" beyond end] @`. On a boundary with ports, you can put labels at the ports, as in `label=north.1:$A$` in the keys of the boundary. - The type of a port goes next to the port. Use `beside start` or `beside end` on the wire, or `beside` in the label of the box, as in `\node[box=1/0/0/0, label={[beside]north.1:$A$}] (f) {f};`. The label goes next to the port, out of the box, to the left of a wire that goes out of it. `swap` puts it to the right. A label of the box shows one time, for all the wires of the port. - `beyond` and `beside` only set the anchor of the label, from the direction of the wire at the label. `at end`, `at start` and `pos` set its position: `beyond end` is the same as `at end, beyond`. In the label of a node, the keys use the name of the anchor. - The options of a quote stop at the first `]` that is not in braces, thus `"x"[blue]` fails. Write `"x" blue`, or `"x" {pos=1, below}`. With a language of babel that makes `"` a shorthand, load the `babel` library of TikZ. - Each key of the package also has a long name, with `string diagrams/` before the short name. For example, `string diagrams/box`, `string diagrams/dot`, `string diagrams/wire`, `string diagrams/boundary` and `string diagrams/box ports north`. The package itself uses only the long names. Thus, if a different package or one of your styles changes a short name, for example `dot`, `\wires` still draws correctly, and `string diagrams/dot` still gives the dot of this package. - The styles `every box`, `every wire` and `every boundary` are different: set them with their short names. The package applies the long names, and the long names apply the short names. - The shape of a box has only a long name: `string diagrams box`. The long names of the `box ports` keys keep the number of ports, as in `\pgfkeysvalueof{/tikz/string diagrams/box ports north}`. ### Errors - "Box ports take four counts, for the north, east, south and west sides, as in 1/2/1/2, not `'": `box ports` and `box` need exactly four counts. - "Box `' has no port ., since its side has .": open more ports on that side, or use a smaller index. - "box math is true or false, not `', so it is left as it was.": write `box math=true` or `box math=false`. `box math` without a value is `box math=true`. - "Node `' has no anchor `', so the wire is not drawn.": the node does not have this anchor. A boundary has ports only with `box`. - "Wire end `.' is not on a side, so the wire is not drawn: wires end at north, east, south or west, or at ports.": use a side, a corner or a port. - "Wire end `()' is not an anchor of a node, so the wire is not drawn: wires go between anchors, as in (f.north) to[wire] (g.south).": a `wire` path goes from a node to a node, not to a coordinate or a calculated point. - "Wire end `' is on a node that doesn't exist, so the wire is not drawn.": write the name of a node that exists. Outside `\wires`, `@` is not a node. - "Wire end `' has no anchor, and its side has ports, so the wire is not drawn: name one, as in ..1.": write the port. - "Wire from `' to `' has no anchors, and their centers coincide, so it is not drawn: give an end an anchor, or set a wire flow.": write an anchor, or set `wire flow`. - "Wire from `' to itself has no anchors, so it is not drawn: give an end an anchor, or set a wire flow.": write an anchor, or set `wire flow`. - "Wire from `' to `' has an end without anchor, and the two nodes overlap, so it is not drawn: give both ends anchors.": write the two anchors. - "Wire flow `' is not a direction: use north, east, south, west, up, right, down, left or none.": use one of these values. - "Node `' has no wires: list them as in = { north = b.south }.": after a node and `=`, write a group of wires in braces. - "Wire end `.' has no target: give it one as in = b.south.": each wire needs two ends. - "`' has a parenthesis, so it is ignored: wires go between nodes, not coordinates such as (1,2). Name the place, as in \path (1,2) coordinate (c); and wire c.": a wire end in `\wires` is a node, not a coordinate. Put a coordinate with a name at that position, and write the name. - "`' is a group without a beginning, so it is ignored: a group follows a node or an anchor and =, as in b = { south.1, south.2 }.": write the shared start before the group. - "Wire end `' can't begin a group: targets share their node, as in b = { east.1, east.2 }, not part of an anchor.": a group cannot start in an anchor. - "Wire end `.' has a value: list the anchors of `' alone, as in = { east.1, east.2 }.": the anchors of a target node do not have a value. - "Keys in brackets after the beginning of `' are ignored: keys begin an item or a value, as in a.north = [red] b.south.": put the keys at the start of the item. - "Keys `[]' begin no wire, so they are ignored.": put a wire after the keys. - "This boundary has no name, so nothing can use it: name it, as in \node[boundary] (B) {}.": give the boundary a name. - "A call takes one of boundary, use boundary and same boundary, so its loose ends are not drawn.": use only one of the three keys. - "This call has same boundary, but no call comes before it in this picture, so its loose ends are not drawn.": remove `same boundary` from the first `\wires` command of the picture. - "The call before this one has no boundary, so the loose ends of this one, which has same boundary, are not drawn.": correct the boundary of the `\wires` command before it, or remove `same boundary`. - "This picture has a node `' already, so the loose ends of this call are not drawn: give the boundary another name, or write use boundary=.": give a different name to the boundary, or use the node that has the name. - "This picture has no node `' to use as boundary, so the loose ends of this call are not drawn.": make the boundary before the `\wires` command, or write its correct name. - "Wire end `' is a loose end, but the call has boundary=none, so the wire is not drawn.": remove `boundary=none`, or remove the loose end. - The warning "Node `' is not inside the boundary of this call, so its wires to @ face out.": put the node in the boundary, or make the boundary larger with `fit`. - "A `[' in the wires has no matching `]', so the rest of the list is ignored." and "A `]' in the wires has no matching `[', so the rest of the list is ignored.": close each bracket. - "\wires draws in a picture of TikZ only, so it does nothing here: use it in a tikzpicture.": put the `\wires` command in a `tikzpicture` environment. ## Examples Each example is the body of a document that loads the package and `\usetikzlibrary{calc}`. ### Bialgebra law If you copy the XOR of two bits, you get the XOR of the copies. ```latex \tikzset{ equation/.style={baseline={([yshift=-0.5ex]current bounding box.center)}}, spider/.style={dot, draw, fill=#1, minimum width=0.8em}, } \begin{tikzpicture}[equation, wire flow=north] \node[spider=red!45] (m) at (0,0) {}; \node[spider=green!45] (d) at (0,1) {}; \wires{ @ = { m.south west, m.south east }, m = d, d = { north west = @, north east = @ }, } \end{tikzpicture} $=$ \begin{tikzpicture}[equation, wire flow=north] \node[spider=green!45] (a) at (0,0) {}; \node[spider=green!45] (b) at (1,0) {}; \node[spider=red!45] (A) at (0,1) {}; \node[spider=red!45] (B) at (1,1) {}; \wires{ @ = { a, b }, a = { north west = A.south west, north east = B.south west }, b = { north west = A.south east, north east = B.south east }, A = @, B = @, } \end{tikzpicture} ``` ### Copy law If you copy the result of f, you get the result of f on each copy. ```latex \tikzset{ equation/.style={baseline={([yshift=-0.5ex]current bounding box.center)}}, } \begin{tikzpicture}[equation, wire flow=north] \node[box=1/0/1/0] (f) at (0,0) {f}; \node[dot] (c) at (0,1) {}; \wires{ @ = f, f = c, c = { north west = @, north east = @ } } \end{tikzpicture} $=$ \begin{tikzpicture}[equation, wire flow=north] \node[dot] (c) at (0,0) {}; \node[box=1/0/1/0] (f) at (-0.6,1) {f}; \node[box=1/0/1/0] (g) at (+0.6,1) {f}; \wires{ @ = c, c = { north west = f, north east = g }, f = @, g = @ } \end{tikzpicture} ``` ### Chocolate mousse Each box is a step, and each wire is an ingredient. ```latex \begin{tikzpicture}[ wire flow=east, box math=false, every node/.append style={minimum height=2.6em}, every edge quotes/.append style={font=\small\itshape, minimum height=0pt, inner sep=2pt}, ] \node[box=0/2/0/1] (s) at (0,0.6) {separate}; \node[box=0/1/0/1] (m) at (0,-1) {melt}; \node[box=0/1/0/1] (w) at (2.8,1.4) {whisk}; \node[box=0/1/0/2] (t) at (2.8,-0.5) {stir}; \node[box=0/1/0/2] (f) at (4.9,0.45) {fold}; \wires{ @ = { ["egg" beyond start] s, ["chocolate" beyond start] m }, s = { east.1 = ["white"] w, east.2 = ["yolk"'] t.west.1 }, m = t.west.2, w = f.west.1, t = f.west.2, f = ["mousse" beyond end] @, } \end{tikzpicture} ``` The examples of the manual start with a full diagram: ### Intro ```latex \begin{tikzpicture}[wire flow=north] \node[box=2/0/1/0] (f) at (0,0) {f}; \node[box=1/0/1/0] (g) at (-.6,1.5) {g}; \node[box=1/0/1/0] (h) at (.6,1.5) {h}; \wires{ @ = f, f = { north.1 = g, north.2 = h }, g = @, h = @, } \end{tikzpicture} ``` ### Box ```latex \begin{tikzpicture} \node[box] {A}; \end{tikzpicture} ``` ### Box math ```latex \begin{tikzpicture}[box math=false] \node[box] (A) at (0,0) {whisk}; \node[box, box math] (B) at (2,0) {f}; \end{tikzpicture} ``` ### Every box ```latex \begin{tikzpicture}[every box/.style={ rounded corners, box math=false}] \node[box] (A) at (0,0) {whisk}; \node[box] (B) at (2,0) {fold}; \end{tikzpicture} ``` ### Boxes ```latex \begin{tikzpicture} \node[box] (A) at (0,0) {A}; \node[box, right of=A] (B) {B}; \node[box] (C) at ($(B)+(2cm,1em)$) {C}; \end{tikzpicture} ``` ### Box ports ```latex \begin{tikzpicture}[ marker/.style={circle, fill, inner sep=1pt, text=white}, ] \node[ box, box ports north=3, box ports east=3, box ports south=3, box ports west=3, minimum width=6em, minimum height=6em, ] (A) {A}; \foreach \side in {north,east,south,west} \foreach \index in {1,...,3} \node[marker] at (A.\side.\index) {\index}; \end{tikzpicture} ``` ### Box ports shortcut ```latex \begin{tikzpicture}[ marker/.style={circle, fill, inner sep=1pt}, ] \node[box, box ports=1/2/3/4] (A) {A}; \foreach \side/\n in {north/1,east/2,south/3,west/4} \foreach \index in {1,...,\n} \node[marker] at (A.\side.\index) {}; \end{tikzpicture} ``` ### Wire ```latex \begin{tikzpicture} \node[box=1/0/0/0] (f) at (0,0) {f}; \node[box=0/0/1/0] (g) at (1,1.5) {g}; \wires{ f.north.1 = g.south.1 } \end{tikzpicture} ``` ### Wires ```latex \begin{tikzpicture}[scale=0.6] \node[box=0/2/0/1] (A) at (-2, 0) {A}; \node[box=0/1/0/2] (B) at (+2, 0) {B}; \node[box=0/1/0/1] (C) at ( 0,+1) {C}; \node[box=0/1/0/1] (D) at ( 0,-1) {D}; \wires{ A = { east.1 = C.west , east.2 = D.west , west = @.west }, B.east = @.east, C.east = B.west.1, D.east = B.west.2, } \end{tikzpicture} ``` ### Equation ```latex \begin{tikzpicture} \node[box=1/0/1/0] (f) at (0,0) {f}; \node[box=1/0/1/0] (g) at (0,1.4) {g}; \wires{ f = { south.1 = @.south, north.1 = g.south.1 }, g.north.1 = @.north } \node at (1,.7) {$=$}; \node[box=1/0/1/0] (h) at (2,.7) {h}; \wires{ h = { south.1 = @.south, north.1 = @.north } } \end{tikzpicture} ``` ### Identity ```latex \begin{tikzpicture} \node[box=1/0/1/0] (f) at (0,0) {f}; \path (0.8,0) coordinate (i); \wires{ f = { north.1 = @.north , south.1 = @.south }, i = { north = @.north , south = @.south }, } \end{tikzpicture} ``` ### Styling ```latex \begin{tikzpicture} \node[box=1/0/1/0] (f) at (0,0) {f}; \node[box=1/0/1/0] (g) at (0,1.5) {g}; \wires{ f.south.1 = @.south, f.north.1 = [dashed] g.south.1, g.north.1 = @.north, } \end{tikzpicture} ``` ### Flow ```latex \begin{tikzpicture}[wire flow=north] \node[box=1/0/1/0] (f) at (0,0) {f}; \node[box=1/0/1/0] (g) at (.4,1.4) {g}; \wires{ @ = f, f = g, g = @ } \end{tikzpicture} ``` ### Bare ```latex \begin{tikzpicture} \node[box=2/0/0/0] (f) at (0,0) {f}; \node[box=0/0/1/0] (g) at (-.5,1.5) {g}; \node[dot] (d) at (.8,1.5) {}; \wires{ f = { north.1 = g, north.2 = d } } \end{tikzpicture} ``` ### Flow east ```latex \begin{tikzpicture}[wire flow=east] \node[box=0/1/0/1] (a) at (0,0) {a}; \node[box=0/1/0/1] (b) at (1.2,.3) {b}; \node[box=0/1/0/1] (c) at (2.4,0) {c}; \wires{ @ = a, a = b, b = c, c = @ } \end{tikzpicture} ``` ### Boundary ```latex \begin{tikzpicture} \node[box=1/0/1/0] (f) at (0,0) {f}; \node[boundary, draw, dashed] (B) {}; \draw (f.north.1) to[wire] (B.north); \draw (f.south.1) to[wire] (B.south); \end{tikzpicture} ``` ### Every boundary ```latex \begin{tikzpicture}[every boundary/.style={draw, dashed}] \node[box=1/0/1/0] (f) at (0,0) {f}; \wires{ f = { south.1 = @.south, north.1 = @.north } } \node at (1.4,0) {$=$}; \node[box=1/0/1/0] (g) at (2.8,0) {g}; \wires{ g = { south.1 = @.south, north.1 = @.north } } \end{tikzpicture} ``` ### Boundary call ```latex \begin{tikzpicture} \node[box=1/0/1/0] (a) at (0,0) {a}; \node[box=1/0/1/0] (b) at (.3,1) {b}; \wires[boundary={name=F, box=1/0/1/0, draw, dashed}]{ a = { south.1 = @, north.1 = b.south.1 }, b.north.1 = @ } \node[box=1/0/0/0] (h) at (0,-1.5) {h}; \node[box=0/0/1/0] (g) at (0,2.5) {g}; \wires{ h.north.1 = F.south.1, F.north.1 = g.south.1 } \end{tikzpicture} ``` ### Same boundary ```latex \begin{tikzpicture} \node[box=1/0/1/0] (f) at (0,0) {f}; \node[box=1/0/1/0] (g) at (1,0) {g}; \wires{ f = { south.1 = @.south, north.1 = @.north }, g.south.1 = @.south } \wires[same boundary, dashed]{ g.north.1 = @.north } \end{tikzpicture} ``` ### Frame ```latex \begin{tikzpicture} \node[box=0/1/0/1] (a) at (0,.25) {a}; \node[draw, dashed, minimum width=2.6cm, minimum height=1.4cm] (F) at (0,0) {}; \wires{ F.west = a, a = F.east } \end{tikzpicture} ``` ### Wire key ```latex \begin{tikzpicture} \node[box=1/0/0/0] (f) at (0,0) {f}; \node[box=0/0/1/0] (g) at (1.2,1.5) {g}; \node[box=1/0/0/0] (h) at (2.4,0) {h}; \draw[->] (f.north.1) to[wire] (g.south.1); \path (h) edge[wire, dashed, "$A$"'] (g); \end{tikzpicture} ``` ### Labels ```latex \begin{tikzpicture} \node[box=2/0/1/0] (f) {f}; \wires{ f = { north.1 = ["$A$" beyond end] @, north.2 = ["$B$" beyond end] @, south.1 = ["$C$" beyond end] @ } } \end{tikzpicture} ``` ### Labels all ```latex \begin{tikzpicture}[wire flow=north] \node[box=1/0/2/0, label={[swap, beside]south.1:$A$}, label={[beside]south.2:$B$}] (f) at (0,0) {f}; \node[box=1/0/2/0] (g) at (.4,1.6) {g}; \wires{ @ = { f.south.1, f.south.2, ["$D$" beyond start] g.south.2 }, f.north.1 = ["$C$"] g.south.1, g.north.1 = ["$E$" beyond end] @, } \end{tikzpicture} ``` ### Dots ```latex \begin{tikzpicture} \node[box=0/1/0/2] (A) at ( 0,+1) {A}; \node[box=0/2/0/1] (B) at ( 0,-1) {B}; \node[dot] (x) at (+1, 0) {}; \node[dot] (y) at (-1, 0) {}; \wires[looseness=1.5,-stealth]{ A = { east.1 = x.north , west.1 = @.west }, B = { east.1 = x.south , east.2 = @.east }, y = { north = A.west.2 , south = B.west , west = @.west }, x = { east = @.east }, } \end{tikzpicture} ``` ### Bubble ```latex \begin{tikzpicture} \node[dot] (c) at (0,0) {}; \node[dot] (m) at (0,1.2) {}; \wires{ c = { south = @.south , north west = m.south west , north east = m.south east }, m = { north = @.north }, } \end{tikzpicture} ``` ### Long names ```latex \begin{tikzpicture}[dot/.style={circle, draw, red}] \node[string diagrams/box=1/0/0/0] (f) at (0,0) {f}; \node[string diagrams/dot] (d) at (0,1) {}; \node[dot] (e) at (1,1) {}; \wires{ f = d } \end{tikzpicture} ``` ## Links - [Manual](https://paolobrasolin.github.io/string-diagrams/string-diagrams.pdf): the full documentation, with the implementation (PDF) - [Package](https://paolobrasolin.github.io/string-diagrams/string-diagrams.sty): the current string-diagrams.sty - [CTAN](https://ctan.org/pkg/string-diagrams) - [Source and issue tracker](https://github.com/paolobrasolin/string-diagrams)