# How to set the distance between the halfway label and arrow in tikz?

I know that I can use

\begin{tikzpicture}
\draw (0,0) to node[midway, above=..., left=..., rotate=45] {A} (1,1) ;
\end{tikzpicture}


to set the position of the label, but it is very tedious to calculate the correct position, and when the rotate angle changed, I may need to recalculate the position. Is there an easier way to set the distance between label and arrow?

• You can use [sloped] May 1 '19 at 14:15

Like this?

BTW, I apologize for some gratuitous changes, but I find it easier to draw the line first, then add the labels.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (1,1) node[midway, above, sloped] {A};
\end{tikzpicture}
\end{document}

• IMHO your suggestion to put the label after the path is not necessarily a good one. True, in this case it works, but in general it won't. Compare \draw (0,-2) to[bend left] (2,-2) node[midway, above, sloped] {A} ; to \draw (0,-2) to[bend left] node[midway, above, sloped] {A} (2,-2);. What you are doing here is certainly not wrong but I personally would refrain from calling your prescription "easier" because it cannot be used in path constructions that are not just --.
– user121799
May 1 '19 at 14:39
• @marmot - More precisely, I find it easier to read if I don't have to wade thorough all the node(s) before locating the endpoint. (BTW, is sloped even defined for curved lines?) May 2 '19 at 3:18
• Yes, sloped is defined, try e.g. \draw (0,0) to[out=90,in=180] node[midway, above, sloped] {A} ++ (2,2); and compare this to \draw (0,0) to[out=90,in=180] ++ (2,2) node[midway, above, sloped] {A} ;.
– user121799
May 2 '19 at 3:24
• @marmot - Ah, the problem isn't with sloped or bend, it is with to and midway (or any pos). May 2 '19 at 3:53

With the library positioning, you can choose any distance you like after above left=, please note the and between the two distances.

Of course, you could use also above or left alone, and also below, right or a combination of them.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}
\draw (0,0) to node[midway, above left=3ex and 1em, rotate=45] {A} (1,1) ; \end{tikzpicture}
\end{document}