Earlier I asked a question about labelling the edges of graphs with arrows. Zarko presented a solution that, stripped down to its minimum, looks like this:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{mathtools}
\begin{document}
\begin{tikzpicture}
\node (A) at (0,0) {A};
\node (B) at (1,1.5) {B};
\draw (B) -- node[sloped,above] {$\xleftarrow{2}$} (A);
\end{tikzpicture}
\end{document}
Here is the result:
It looks great in the context of what I'm doing, but I don't like the way the number is slanted – I'd like it to be positioned where it is, but to be upright, like this:
I thought I could do this by defining a second node with a greater outer sep
and without the sloped
modifier, like this:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (A) at (0,0) {A};
\node (B) at (1,1.5) {B};
\draw (B) -- node[sloped,above] {$\leftarrow$} node[above,outer sep=6pt] {\scriptsize{2}} (A);
\end{tikzpicture}
\end{document}
However, as you can see this doesn't put the number where I want it, since it's literally above the midpoint of the line, rather than "above" relative to its slope:
Is there a way that I can position the node in the same way as if it was sloped (i.e. by moving a set distance along a line perpendicular to the edge), without the node's contents being sloped? Or is there some other way to achieve the effect I'm after?
Best Answer
You could use an empty sloped node to get the correct position and then use that node's center anchor to place a the desired un-sloped node.
However, the placement doesn't look quite right, so you probably need to manually shift the node position a bit.