Live demo
You can change the code of each example. Click Compile to draw the picture again. TeX operates in your browser.
\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}
\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}
\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}
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
- The manual (PDF) starts with one box and gives an example for each key and command. It also contains the implementation, with comments.
- llms.txt gives a summary of the package and all its examples in plain text, for AI assistants.
- The changelog shows the changes in each version.
- Send questions, ideas and bugs to the issue tracker.