2

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

3

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

2

Since you have placed the nodes between the coordinates, they are automatically placed in the middle of the path and therefore it is useless to ask for it expressly with midway.

For placement, there is the auto=left option that automatically places to the left of the path and by adding swap, it places it to the right.

screenshot

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

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

\end{document}
  • Thanks for the midway tip, didn't know it worked like this. Anyway, this solution looks like mine except that it doesn't follow the edge (the text is horizontal) and if I had "sloped" to it the result looks bad. But I'm glad I learned about this "auto" option which will help me when placing multiple nodes and not having to think about above, below, left or right. – Lhooq Jul 2 at 13:48
  • With your new edit, my result is very strange. I'll add it to my question to show you what I have. – Lhooq Jul 2 at 13:49
  • Have you recompiled? I've updated again. – AndréC Jul 2 at 13:59
  • Yes, I edited my question with the result it gives me. – Lhooq Jul 2 at 14:01
  • Your edition is not visible, there is a bug. – AndréC Jul 2 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.