string-diagrams

String diagrams in LaTeX, drawn with TikZ.

You put boxes and dots in a TikZ picture. Then you write a list of wires. The \wires command draws the wires and their loose ends at the edge of the picture.

Live demo

If you copy the XOR of two bits, you get the XOR of the copies.
\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}
Two string diagrams of red and green dots, equal. Left: two wires merge at a red dot, and a green dot splits the result in two. Right: two green dots split the two wires, the inner copies cross, and two red dots merge them pairwise.
If you copy the result of f, you get the result of f on each copy.
\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}
Two string diagrams, equal. Left: a wire goes through a box f and then splits at a dot. Right: the wire splits at a dot first, and each branch goes through its own box f.
Each box is a step, and each wire is an ingredient.
\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}
A recipe as a string diagram: an egg is separated into white and yolk; the white is whisked, the chocolate melted and stirred with the yolk, and the two are folded into a mousse.

Getting started

The package is on CTAN and in TeX Live. Put this line in the preamble of your document. The package loads TikZ.

\usepackage{string-diagrams}

The examples need version 0.5 or later. To update an older version, use tlmgr update string-diagrams. You can also put string-diagrams.sty in the folder of your document. The Open in Overleaf buttons do this.

A diagram is a usual tikzpicture. Boxes and dots are TikZ nodes, and you can put them in any position. The \wires command connects them. Each wire goes out of a side, a corner or a port in the correct direction. A wire can also go to the edge of the diagram. In the command, this edge is the boundary @. If you do not write an anchor, the wire uses the side of the node in its direction. The wire key also draws wires with the TikZ commands, for example \draw (f) to[wire] (g);.

Documentation