One can get somewhat close using the decorations.markings
library. The code below makes use of the fact that a markings
decoration has an internal coordinate system where the x-axis is parallel to the path, so I made a short path along the y-axis, and placed an auto
node along it.
It's not perfect, and I can't guarantee that it will always work. For the bent to
paths in the example below, I had to rotate the nodes by -90 degrees, and I'm not exactly sure why. You can also see that the position of the nodes is perhaps not perfect.
\documentclass[border = 2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[
endnode style/.style={},
endnodestyle/.style={endnode style/.append style={#1}},
endnode/.style={
decoration={
markings,
mark=at position 1 with {
\path (0,-1pt) -- node[auto,swap,endnode style] {#1} (0,1pt);
}
},
postaction={decorate}
}
]
\foreach \Ang in {0,45,...,350}
{
% works fine for straight lines
\draw [endnode=a] (0,0) -- (\Ang:2cm);
% needs a rotation for these curved paths
\draw [endnodestyle={rotate=-90},endnode=foo] (6,0) to[bend right] ++(\Ang:2cm);
}
\end{tikzpicture}
\end{document}
Better output, less convenient to use
You could save two coordinates on the path, one very close to the end (I randomly used pos=0.97
here, but I suspect the ideal value will depend on the path), one at the end (pos=1
). Then in a separate path, calculate the angle between those two coordinates with the let
operation (manual section 14.15), and use that to set the anchor
of a node
. This does not have the problem the decorations.markings
version above suffered, of failing at larger bend angles
, but is as mentioned less convenient to use.
\documentclass[border = 2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings, calc}
\newcommand{\addnode}[2][tmp]{%
\path let
\p1=($(#1-A)-(#1-B)$),\n1={atan2(\y1,\x1)}
in
node[anchor=\n1] at (#1-B) {#2};
}
\begin{document}
\begin{tikzpicture}
\foreach \Ang in {0,45,...,350}
{
% works fine for straight lines
\draw (0,0) --
coordinate[pos=0.97] (tmp-A) % near end
coordinate[pos=1] (tmp-B) % at end
(\Ang:2cm); % close the path
\addnode{a} % add node in separate step
\draw (6,0) to[bend right=80]
coordinate[pos=0.97] (tmp-A)
coordinate[pos=1] (tmp-B)
++(\Ang:2cm);
\addnode{foo}
}
\end{tikzpicture}
\end{document}