Given two points (defined, for instance using nodes), I want to compute the distance between them.

  1. Is there some build in functionality in tikz to do this?
  2. If not, how can it be done using the mathematical engine?

The application I have in mind is to draw an circular arc centered at (a) and passing through some second point (b), where only (a) and (b) are known.

  • Have a look at the through library for this purpose. – Roelof Spijker Dec 16 '11 at 11:29
  • 3
    What about veclen(a,b)? – Ignasi Dec 16 '11 at 11:46

As wh1t3 commented, there is a through library which even has the command circle through. Here is the example in the manual: After adding the line \usetikzlibrary{through} in the preamble,

\draw[help lines] (0,0) grid (3,2);
\node (a) at (2,1.5) {$a$};
\node [draw] at (1,1) [circle through={(a)}] {$c$};

You can do this using the calc library with almost the same convenience (on which Ignasi commented while I was typing the answer). You can further use this for other purposes: Modfying the example slightly and using \usetikzlibrary{calc} in the preamble, you can get the vector length by using the veclen command as

\coordinate [label=left:$A$] (A) at (0,0);
\coordinate [label=right:$B$] (B) at (2,2);

\draw[red,line width=1mm] let \p1 = ($(B)-(A)$) in (A) -- ++(45:({veclen(\x1,\y1)}););
\draw (A) -- (B);
\draw[blue] (A) let \p1 = ($(B)-(A)$) in -- ++({veclen(\x1,\y1)},0) arc (0:45:({veclen(\x1,\y1)}););

which would give

enter image description here

  • The minimal example you provided is almost what I was after. I didn't manage to get rid of the horizontal blue edge. I only want the arc... Thanks in advance! – Dror Dec 16 '11 at 12:18
  • 1
    @Dror Just remove -- in the last line ;) – percusse Dec 16 '11 at 12:24
  • 1
    Thanks! After correcting some unbalanced parentheses your code was what I was looking for! Thanks! – Dror Dec 16 '11 at 19:22
  • @Dror Oops, fixed! – percusse Dec 17 '11 at 1:50

With TikZ, you have the answer with percusse, with pgfmath is the same method but you need to determine the coordinates vx, vy of the vector formed by the two points and then \pgfmathparse{veclen(vx,vy)} the result is in \pgfmathresult.

Personally, I take a few fantasies with the TikZ's syntax. I do not find very satisfactory the syntax let \p1 \n1 and I prefer to calculate the length before drawing the objects. In addition, in some cases the result is not very fine also I use fp to calculate the length. Lua is also a possibilty. It's also possible to use the library fpu with TikZ.


\global\expandafter\edef\csname #3\endcsname{\FPMathLen}


  5cm = 5*28.45274 pt =142.2637pt

\coordinate (A) at (1,2);
\coordinate (B) at (4,6);
% \draw (A) circle (\mylen pt); % pt is important here
With calclength the length of AB is : \mylen

\coordinate (A) at (1,2);
\coordinate (B) at (4,6);
\path (A) let   \p1 = ($ (B) - (A) $),  \n1 = {veclen(\x1,\y1)} 
    in -- (B) node[draw]  {With veclen the length is :\n1};


enter image description here

to get the arc the code is

\draw[red,line width=1mm]  (A) -- ++(45:\mylen pt);  
\draw[blue] (A) -- ++(\mylen pt,0) arc (0:45:\mylen pt); 

For me it is more readable but it is a matter of taste

  • 1
    Completely agree. With calcLength the code is much more readable and avoiding the let syntax simplifies typing. 1 up! – loved.by.Jesus Oct 5 '15 at 11:57

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.