[Tex/LaTex] Placing node right after end of path in Tikz

positioningtikz-nodetikz-pgf

I'd like to place a label (most of the times a single letter) right after the end of a line. That is, the node should be moved into the direction of the line from its endpoint, just as far that is doesn't overlap with the line.

I know that I could do

\tikz (a)--node[pos=1.1]{b}(b);

but I don't want the distance of the node from the line end to depend on the length of the line. Also I'd like this to work for arbitrary curved paths, not just straight lines.

What is the easiest way to do this in Tikz?

Best Answer

(this is the first time I add another answer --- if it's not the correct thing to do, please tell me; but this is so different from my first one that... should I mark it community wiki?)

You can use a transform on a decoration, thanks to the fact that the coordinate system in a decoration is along the path; that will shift the node after the end of the path by a distance specified as the second argument:

\documentclass[border = 2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[
    endnode/.style n args={2}{
        decoration={
        markings,
        % transform={xshift=0.1*\pgfdecoratedpathlength}, % relative to lenght
        transform={xshift=#2},                            % absolute 
        % maybe add a endnodestyle like in Torbjørn T.'s answer
        mark=at position 1 with {\node[draw=red] {#1};}   % just to debug 
        },
     postaction={decorate},
  },
]
\foreach \Ang/\Len in {0/1,45/1.2,90/1.4,135/1.6,180/1.8,225/2,270/2.2,315/2.4}
{
    \draw [endnode={A}{2mm}] (0,0) -- (\Ang:\Len);
    \draw [endnode={B}{4mm}] (6,0) to[bend right]  ++(\Ang:\Len);
}
\end{tikzpicture}
\end{document}

enter image description here

This makes the node centered at a fixed distance from the end of the path, along the path. It doesn't change the node anchors, unfortunately... and auto is not working here.

Thanks to: Length of curve in TikZ, Torbjørn T.'s answer, \tikzset key with multiple arguments.