10

I have two points:

\coordinate (A) at (1,2);
\coordinate (B) at (13.2,-23.4);

I would like to draw a vector pointing from point A to point B. The vector starts from point A, its length is other specified, and its end is not at point B. See the figure below. Is there an automatic way to draw the arrow such that I need not to calculate the coordinate of the end of the arrow every time?

enter image description here

9

Additional to shorten, you could be interested in the calc library.

Code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,arrows}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (-1,5);
\coordinate (B) at (1,2);
\tikzset{-|}
\draw [|-|,help lines] (A) -- (B);
\draw[black,very thick] (A) -- ($(B)!1cm!(A)$); % 1 cm before B
\draw[blue, thick] (A) -- ($(A)!.65!(B)$);      % 65 % on the path from A to B
\draw[green] (A) -- ($(B)!.45!(A)$);            % 45 % on the path from B to A (55% from A to B)
\end{tikzpicture}
\end{document}

Output

Output

  • Thanks. But it has a problem that it can only specify the distance between the end of the arrow and point B. In order to satisfy the specified length of the vector, the distance between A and B need to be computed, right? – Shiyu Oct 5 '12 at 8:11
  • @Shiyu No, ($(A)!<length of vector>!(B)$) should work, too. (Should have made a better example …) – Qrrbrbirlbel Oct 5 '12 at 8:12
5

Here is a way:

\documentclass[11pt,a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{decorations.markings,arrows}
\begin{document}
\begin{tikzpicture}
\node[circle,draw,black,scale=0.2] (A) at (1,2) {};
\node[circle,draw,black,scale=0.2] (B) at (3,4) {};

\draw[postaction={decorate,decoration={markings,mark=at position 0.25 with {\arrow[black,line width=1.5pt]{>}}}}](A)node[below]{A}--(B)node[above]{B};
\end{tikzpicture}
\end{document}

Result:

enter image description here

  • One important feature of this solution is that it doesn't double-draw the line from A to the arrow. Namely, if the line from A to B is already drawn, this does not add an extra line on top. If the line from A to B is not to be drawn, this probably isn't the desired solution (but the question is not clear on this regard). – Loop Space Oct 5 '12 at 11:08
4

You can use shorten >= <length>:

enter image description here

Code

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\coordinate (A) at (1,2);
\coordinate (B) at (2,4);

\draw [fill=blue] (A) circle (2pt) node [left] {A};
\draw [fill=blue] (B) circle (2pt) node [left] {B};

\draw [-latex, red, thick, shorten >= 1.00cm] (A) -- (B);
\end{tikzpicture}
\end{document}
  • It's interesting. But we need to calculate the distance between A and B. Denote the distance between A and B as d. The specified length of the vector is d_1. Then the shorten length is d-d_1, right? how to compute the distance d? – Shiyu Oct 5 '12 at 8:06
  • @Shiyu: Not sure I understand. If you already know d_1 then you can use that without knowing the distance d. d can certainly be computed, but just not sure what you want to do with it. Try changing the shorten amount to 0.00 and see what happens, and replace that with d_1 length. – Peter Grill Oct 5 '12 at 8:12
  • I have tried the code. And it seems the shorten amount only specify the distance between the end of the arrow and point B, but not the distance between the end of the arrow and point A. So if the latter distance need to be a specified value, we need compute d and then compute the shorten amount as d-d_1. Hope I am clear:) – Shiyu Oct 5 '12 at 8:21
  • Qrrbrbirlbel's method solves my problem. Thanks a lot anyway. – Shiyu Oct 5 '12 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.