[Tex/LaTex] Tikz coordinates, arrows, and stopping short

tikz-pgf

So, I'm drawing velocity arrows with labels over them.

The minimal example is that I have a node (a) and I want an arrow pointing towards the origin, but only part way. The distance could be long or short, maybe based on the magnitude of velocity. I've found a solution, but I want to know what others are out there, because it's not that elegant and LaTeX-y.

What I just got working is this line:

\draw[->, shorten >=1cm] (a) -- (0,0)
node [near start, sloped, above] {12 m/s};

The parts I dislike about this solution are that I'm specifying how much to reduce the length rather that its actual length, and that as the length varies I have to change the label position, when I really just want it centered on the arrow length.

I'm imagining there might be some way to specify a relative polar coordinate as "towards that point, this long" instead of explicit angle. Any ideas?

Best Answer

You can use the <coordinate>!<number>!<second coordinate> syntax; for example (3,3)!.25!(0,0) means "the coordinate that is one quarter on the way from (3,3) to (0,0)". To place the label in the middle of the arrow, use the midway option. An example:

\documentclass[12pt]{article} 
\usepackage{tikz} 
\usetikzlibrary{calc} 

\begin{document}

\begin{tikzpicture}
\coordinate (a) at (3,3);
\coordinate (b) at (3,0);
\coordinate (c) at (-3,3);
\node (O) at (0,0) {origin};
\draw[->,red] (3,3) -- ( $ (a)!.25!(0,0) $ ) node [midway, sloped, above] {12 m/s};
\draw[->,blue] (b) -- ( $ (b)!.8!(0,0) $ ) node [midway, sloped, above] {12 m/s};
\draw[->,magenta] (c) -- ( $ (c)!.5!(0,0) $ ) node [midway, sloped, above] {12 m/s};
\end{tikzpicture}

\end{document}

enter image description here

Even closer to waht you need, is the <coordinate>!<dimension>!<second coordinate> syntax, which means "use the point that is distanced <dimension> from <coordinate> on the straight line from <coordinate> to <second coordinate>". A little example: :

\documentclass[12pt]{article} 
\usepackage{tikz} 
\usetikzlibrary{calc} 

\begin{document}

\begin{tikzpicture}
\coordinate (b) at (3,0);
\node (O) at (0,0) {origin};
\draw[->,magenta] (b) -- ( $ (b)!3cm!(0,0) $ ) node [midway, sloped, above] {12 m/s};
\end{tikzpicture}

\end{document}

enter image description here

And, of course, you can wrap this in a macro, as the following example suggests. The \arrowfromto[<attributes>]{<initial coordinate>}{<to coordinate>}{<distance>}{<label>} macro will apply \draw with the options given in the first optional argument, from <initial coordinate> to a point that is <distance> far away from it and on the straight line from <initial coordinate> to the <to coordinate>, using a label given by`:

\documentclass[12pt]{article} 
\usepackage{tikz} 
\usetikzlibrary{calc} 

\begin{document}

\begin{tikzpicture}
% syntax
% \arrowfromto[<attributes>]{<initial coordinate>}{<to coordinate>}{<distance>}{<label>}
\newcommand\arrowfromto[5][blue]{%
  \draw[#1] #2 -- ( $ #2!#4!#3 $ ) node [midway, sloped, above] {#5}}
\coordinate (a) at (3,3);
\node (O) at (0,0) {$O$};
\node (a) at (0,3) {$a$};
\node (b) at (3,3) {$b$};
\arrowfromto{(O)}{(a)}{5cm}{5cm};
\arrowfromto[-latex,red]{(a)}{(b)}{3cm}{3cm};
\arrowfromto[<->,green!80!black]{(b)}{(O)}{10mm}{10mm};
\end{tikzpicture}

\end{document}

enter image description here