[Tex/LaTex] String diagrams in monoidal categories

diagramsstrings

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:
enter image description here

(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!

Best Answer

Here's one option using TikZ:

enter image description here

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}

enter image description here