1

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:

enter image description here

\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}
3
3

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}

demo

3
  • 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

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.