Placing node right after end of path in Tikz
You can put the node at pos=1
and then left (or right, depending on where) align it:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[every node/.style={draw=red},
]
\draw (0,0) -- (1,0) node[right]{a} ;
\draw (1,0.5) --node[pos=1, left]{b} (0,0.5) ;
\draw (0,-1) -- node[pos=1, right]{c} (1, -2) ;
\draw (0,-1.3) -- node[pos=1, sloped, right]{d} (1, -2.3) ;
\draw (0,-2) .. controls (0,-1) and (0.5,-1.5) .. node[pos=1, right]{e} (1, -1) ;
\end{tikzpicture}
\end{document}
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}
(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}
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.