Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
Where exactly is the end of the arrow? Are you interested in TikZ: How to draw an arrow in the middle of the line? or TikZ-pgf directed graph: change arrow color and location? –  Qrrbrbirlbel Oct 5 '12 at 7:50
    
@Qrrbrbirlbel: since the arrow points from A to B, its direction is determined. The length of the vector (arrow) is other specified. Then the end of the arrow can be computed. I will have a look at the links you gave. Thanks. –  Shiyu Oct 5 '12 at 7:58
add comment

3 Answers

up vote 6 down vote accepted

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

share|improve this answer
    
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
    
It does work! Thanks a lot! –  Shiyu Oct 5 '12 at 8:21
add comment

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

share|improve this answer
    
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). –  Andrew Stacey Oct 5 '12 at 11:08
add comment

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}
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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