# String diagrams in monoidal categories

I'm writing a paper which contains a chapter on a certain kind of monoidal categories (spherical, that is) and I would like to include the usual graphical calculus for morphisms. In particular, I'm looking for some way to draw diagrams like:

(this is taken from this paper which features many nice examples). Now, I've been searching all day and just couldn't find a way to do it. The only thing I found was the braid package but I don't think it lets you do the caps and cups on the left and right side of the X arrow. I'm starting to get really desperate, if anyone has an idea of how to draw them, I'd be glad about any suggestion!

• It can be done with pst-nodes (one of the pstricks modules). – Bernard Sep 3 '15 at 16:38
• Another option is TikZ. – Heiko Oberdiek Sep 3 '15 at 17:00
• I'm reading into pst-nodes right now, thank you. I've wondered about whether it'd be possible to do with TikZ but I didn't manage to find a way. – Nephry Sep 3 '15 at 17:11
• @nicolas I did actually use the code provided by Gonzalo in the answer below. It turned out looking quite nice in the end! – Nephry Jun 17 '17 at 10:40
• thanks. I wonder where this gap between fancy theories and abysmal imperative untyped code really happens. if one day we find out and lift it, how nicer will it be ! – nicolas Jun 17 '17 at 10:45

Here's one option using TikZ:

The code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,arrows,arrows.meta}

\tikzset{
myblock/.style={
draw,text width=0.75cm,minimum height=2cm,align=center,
append after command={node[fill,anchor=north west,minimum size=0.35cm] at (\tikzlastnode.north west) {}}
},
twoarrows/.style n args={4}{
decoration={
markings,
mark=at position #1 with {\arrow{>}\node[above] {#3};},
mark=at position #2 with {\arrow{>}\node[above] {#4};}
},
postaction=decorate
},
onearrow/.style 2 args={
decoration={
markings,
mark=at position #1 with {\arrow{>}\node[above] {#2};},
},
postaction=decorate
},
}

\begin{document}

\begin{tikzpicture}
\node[myblock]
(G)
{$g$};
\draw[rounded corners,twoarrows={0.07}{0.95}{$X$}{$X$}]
([shift={(0pt,10pt)}]G.south east) -|
([shift={(25pt,-20pt)}]G.south east) --
([shift={(-25pt,-20pt)}]G.south west) |-
([shift={(0pt,10pt)}]G.south west);
\draw[onearrow={0.5}{$A$}]
([shift={(-40pt,-15pt)}]G.north west) -- ([shift={(0pt,-15pt)}]G.north west);
\draw[onearrow={0.5}{$B$}]
([shift={(0pt,-15pt)}]G.north east) -- ([shift={(40pt,-15pt)}]G.north east);
\end{tikzpicture}

\end{document}


And another example, reproducing the table at the top of page 6 in the linked document:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,positioning}
\usepackage{array}

\DeclareMathOperator{\iden}{id}

\tikzset{
myblock/.style={
draw,text width=0.75cm,minimum height=2cm,align=center,
append after command={node[fill,anchor=north west,minimum size=0.35cm] at (\tikzlastnode.north west) {}}
},
mysquare/.style={
draw,minimum size=1cm,align=center,
append after command={node[fill,anchor=north west,minimum size=0.35cm] at (\tikzlastnode.north west) {}}
},
twoarrows/.style n args={4}{
decoration={
markings,
mark=at position #1 with {\arrow{>}\node[above] {#3};},
mark=at position #2 with {\arrow{>}\node[above] {#4};}
},
postaction=decorate
},
onearrow/.style 2 args={
decoration={
markings,
mark=at position #1 with {\arrow{>}\node[above] {#2};},
},
postaction=decorate
},
}

\begin{document}

\begin{table}

\renewcommand\arraystretch{3}
\begin{tabular}{l>{$}l<{$}c}
Object & A &
\begin{tikzpicture}[baseline]
\draw[onearrow={0.5}{$A$}] (0,0) -- ++(40pt,0pt);
\end{tikzpicture}
\\
Morphism & f\colon A\to B &
\begin{tikzpicture}[baseline]
\node[mysquare] (F) {$f$};
\draw[onearrow={0.5}{$A$}]
([xshift=-40pt]F.west) -- (F.west);
\draw[onearrow={0.5}{$A$}]
(F.east) -- ([xshift=40pt]F.east);
\end{tikzpicture}
\\
Identity &  \iden_{A}\colon A\to A &
\begin{tikzpicture}[baseline]
\draw[onearrow={0.5}{$A$}] (0,0) -- ++(40pt,0pt);
\end{tikzpicture}
\\
Composition & t\circ s &
\begin{tikzpicture}[baseline]
\node[mysquare] (T) {$t$};
\node[mysquare,right=40 pt of T] (S) {$s$};
\draw[onearrow={0.5}{$A$}]
([xshift=-40pt]T.west) -- (T.west);
\draw[onearrow={0.5}{$B$}]
(T.east) -- (S.west);
\draw[onearrow={0.5}{$C$}]
(S.east) -- ([xshift=40pt]S.east);
\end{tikzpicture}
\end{tabular}
\end{table}

\end{document}


• This is wonderful! Does precisely what I need. Thank you very much! – Nephry Sep 3 '15 at 17:40
• @Nephry You're welcome! I updated my answer adding another example which could be of interest for you. – Gonzalo Medina Sep 3 '15 at 17:46
• That is indeed very helpful, thanks you so very much! – Nephry Sep 3 '15 at 17:58