6

I'd like to place a node at some position along a path, e.g.

\tikz (a)node[pos=0.5,left]{x}{b};

But instead of shifting it 'left', 'right', 'above' or 'below' in absolute coordinates, I would like to have it left or right of the path relative to the direction of the path. E.g. if the path goes horizontally from the right to the left side of the canvas, 'right' should place it above the line. The node should not be rotated, and it should work not only for straight lines but for curved paths also.

What is the easiest solution for this in Tikz?

  • 1
    Sorry about my previous comment, seems I can't read. You're after the auto option I below. – Torbjørn T. May 15 '18 at 10:03
  • Ah, this was supposed to be a different question, as the node now is not at the end of the line shifted in the line direction but along the line shifted perpendicular to the line. Didn't realize that both could be formulated as "node along path shifted by coordinates relative to the path direction"... – Andi Bauer May 15 '18 at 10:06
9

This is what the auto option does. By default it places the nodes on the left side of the line, looking along the path. You can move it to the other side by adding the swap option.

enter image description here

\documentclass[border = 2mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
  every node/.style={inner sep=0pt} % to make it clearer which line the nodes belong to
]
\foreach \Ang in {0,45,...,350}
\foreach \Ang in {0,45,...,350}
{
  \draw (0,0) --node[auto] {a} (\Ang:2cm);
  \draw (5,0) to[bend left] node[auto,swap] {a} ++(\Ang:2cm);
}
\end{tikzpicture}
\end{document}

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.