# Tikz: label of node, midway perpendicular to the node

I'd like to have to automatically give a position to a node label without needing to manually choose the angle using the label options. What I want is just to say "please go in the middle of this path, and perpendicularly to the path, mode n centimeters and place my label here". I don't want to specify manually the angle (too annoying to compute when you have not trivial paths), and the options right and left are not good because they don't go perpendicularly to the path, but rather horizontally or vertically.

Any idea?

Thanks.

MWE:

\documentclass{article}
\usepackage{tikz}
\begin{document}
A is too below, B is too right\\
\begin{tikzpicture}
\draw (0,0) -- node[midway,below]{A} node[midway,right]{B}
node[circle,fill=red,inner sep=.5pt]{} (3,1.5);
\end{tikzpicture}
\end{document}

• did you try the auto key with possibly swap? – percusse May 17 '18 at 23:28
• @percusse Indeed it seems to work pretty well, thank you! – tobiasBora May 17 '18 at 23:42
• Incidentally, someone asked more or less the same question quite recently: tex.stackexchange.com/questions/431654 – Torbjørn T. May 18 '18 at 7:04

Here are three different approaches.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- node[midway,below,sloped]{A}
node[circle,fill=red,inner sep=.5pt]{} (3,1.5);

\draw (0,-1) -- node[midway,below,sloped] (A) {\phantom{A}}
node[circle,fill=red,inner sep=.5pt]{} (3,0.5);
\node at (A) {A};

\draw (0,-2) -- node[circle,fill=red,inner sep=.5pt] (B) {} (3,-0.5);
\path ($(B)!\ht\strutbox!-90:(3,-0.5)$) node{B};
\end{tikzpicture}
\end{document}


• Thank you for the good ideas! Just, do you know what is the differences between this and auto ? (I prefer your rendering, but I'm curious to know why) – tobiasBora May 18 '18 at 0:20
• and also, would it be possible to have some explanation on the last ($(B)!\ht\strutbox!-90:(3,-0.5)$), I'm not sure to get it – tobiasBora May 18 '18 at 0:20
• I wasn't that familiar with [auto], but I know [sloped] would work, and started playing with it. Looking up [auto] in the manual, it is probably easier to use. The calc solution is found on page 144. – John Kormylo May 18 '18 at 0:27