You could remove fill=white
from the nodes and add near end
(pos=.75
) to the 4
node as such:
\draw[->|, rotate around={30:(0,0)}] (0,0) -- (2,0) node [midway] {$2$};
\draw[->|, rotate around={60:(0,0)}] (0,0) -- (4,0) node [near end] {$4$};
but this will give you
which doesn't look nice, does it?
Unfortunately, the mark connection node
decoration doesn't like arrow tips but for straight paths, you just place the node first and then connect it with lines:
\path (0,0) -- node[near end] (4) {$4$} (60:4);
\draw[->|] (0,0) -- (4) -- (60:4);
Since this involves using each coordinate/name at least twice, I'll suggest putting this into a to path
.
This needs some extra work because it uses an edge
(which is also a to
):
line to
to get the original --(\tikztotarget)\tikztonodes
to path (and not the one we are just defining),
path only
to force no drawing, no filling, …,
- all nodes along the path are aliased
@nodeonline@
so we can use them later (the coordinate
is a fallback in case no node has been used with the to
path) and
- sneaking in the
\tikztonodes
from the parent to
but the internal version.
The latter uses \unexpanded
in case there are any fragile macros part of the nodes (not your case).
A previous version of this answer provided a different solution that was more complicated than it needed to be (but didn't use any TikZ internals as this one does).
I prefer this one because it is as close to normal TikZ usage and can be used repeatedly on a path without the path being interrupted:
\tikz[inner sep=.15em, circle, nodes=draw, sloped]
\draw[ultra thick, ->, node on line] (0,0) to["0"] (1,1)
to["1"] (2,0)
to["2"] (4,1);
If this is needed for more than one node, a bit more work is necessary.
Code
\documentclass[border=5mm, tikz]{standalone}
\usetikzlibrary{arrows.meta, quotes}% quotes for "syntax" on edges/tos
\makeatletter
\tikzset{
node on line/.style={
to path={
\pgfextra{%
\edef\tikz@temp{% rescuing nodes and target for edge
edge[
line to, path only, % line to = --, path only = no draw, no fill, …
every edge quotes/.append style={auto=false},% node *on* the line
nodes={alias=@nodeonline@}]
coordinate(@nodeonline@)% fallback coordinate
\unexpanded\expandafter{\tikz@tonodes}(\tikztotarget)
}\expandafter
}\tikz@temp
-- (@nodeonline@) -- (\tikztotarget)}}}
\makeatother
\begin{document}
\begin{tikzpicture}
\draw[help lines, color=gray!30, dashed] (-4.9, -4.9) grid (4.9, 4.9);
\draw[->,ultra thick] (-5, 0) -- (5, 0) node[right]{$x$};
\draw[->,ultra thick] ( 0,-5) -- (0, 5) node[above]{$y$};
\draw [thick] circle [radius=2]
circle [radius=4];
\draw[double = gray!40, double distance=2cm, opacity=0.2] (0,0) circle [radius=3];
% Short: polar coordinate and "syntax"
\draw[->|] (0,0) to[node on line, "$2$"] (30:2);
% Long: rotate around and literal node after to
\draw[->|, rotate around={60:(0,0)}] % long form (short: "$4$" near end)
(0,0) to[node on line] node[near end]{$4$} (4,0);
\draw[fill=black](0,0) circle [radius=2pt]
node [anchor=south east] {$O$};
\end{tikzpicture}
\end{document}
Output
fill=white
in the node option; addingpos=.75
in the node option