[Tex/LaTex] Label an edge above and below

labelsoverleaftikz-pgf

I want to be able to label an edge with a part of the label above it and the other part below it like in the following example :

enter image description here

For the moment I have this solution:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[text=black]
  \node [draw, ] (a) at (0,0) {a};
  \node [draw, below left=4 of a] (b) {b};
  \draw[->] (a) to[bend left] node[midway, sloped, above]{to} node[midway, sloped, below]{b} (b.east) ;
\end{tikzpicture}

\end{document}

Where I create tWo midway nodes, one above the edge, the other one below it but I wanted to know if there is a simpler solution.


[EDIT]
@AndréC solutions gives the following result on Overleaf:

enter image description here

Best Answer

You do not need two nodes for this, one with align=center is sufficient.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[text=black]
  \path (0,0) node [draw] (a) {a}
  node[draw, below left=4 of a] (b) {b};
  \draw[->] (a) to[bend left] node[sloped, align=center]{to\\
  b} (b.east) ;
\end{tikzpicture}

\end{document}

enter image description here