[Tex/LaTex] Rotating label of arrow along the arrow

tikz-cd

Suppose I want to make a diagram using tikz, where an arrow is diagonal:

\begin{tikzcd}
A\ar[rd] & B \\
& C
\end{tikzcd}

and I have a label to put on this arrow. I want this label to have the same "inclination" as the arrow itself: how can one achieve this? I know of the option using rotate, but one doesn't necessarily know the angle over which the label should then be rotated. Thanks in advance!

Edit.

An example is the following diagram

enter image description here

I would like to align the labels with the arrows.

Best Answer

It seems that TikZ is not able to compute correctly the width of the sloped label; with \mathclap we make it zero width. It's your responsibility to look at the diagram and increase row or column separation so they fit, but this is true for every diagram with long labels, so not a real problem.

I also add the version with the unsloped labels, which I prefer as it doesn't force readers to tilt their head in order to look at the labels.

\documentclass{article}
\usepackage{amsmath,mathtools}
\usepackage{tikz-cd}

\newcommand{\id}{\mathrm{id}}

\begin{document}

\begin{tikzcd}[column sep=huge,row sep=large]
k\otimes A
  \arrow[r,"\eta\otimes\id_A"]
  \arrow[dr,"\mathclap{\mu\circ(\eta\otimes\id_A)}"',sloped]
&
A\otimes A
  \arrow[d,"\mu"]
&
A\otimes k
  \arrow[l,"\id_A\otimes\eta"']
  \arrow[dl,"\mathclap{\mu\circ(\id_A\otimes\eta)}",sloped]
\\
& A
\end{tikzcd}

\begin{tikzcd}[column sep=huge,row sep=large]
k\otimes A
  \arrow[r,"\eta\otimes\id_A"]
  \arrow[dr,"\mu\circ(\eta\otimes\id_A)"']
&
A\otimes A
  \arrow[d,"\mu"]
&
A\otimes k
  \arrow[l,"\id_A\otimes\eta"']
  \arrow[dl,"\mu\circ(\id_A\otimes\eta)"]
\\
& A
\end{tikzcd}

\end{document}

enter image description here

Related Question