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 want to retrieve the length of a segment in cm for display in a node

As in the example below, I would like to display the length of three vectors

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at  (0,0);
\coordinate (B) at (4,2);
\coordinate (C) at (1,3);
\coordinate (D) at (5,3);

\draw[-latex, green, thick] (A) node[left,black]{A} -- (B)node[above left,black]{B} ;
\draw (A) -- (C)node[left]{C} ;
\draw (B) -- (D) node[left]{D};

\draw[thick,-latex,red] (B) -- (intersection of A--C and B--D) coordinate(I) node[left,black]{I} ;
\draw[thick, -latex]  (I) -- (A) node[left,midway]{lenght cm};

\end{tikzpicture}
\end{document}

enter image description here

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

TikZ provides the calculation via veclen. To calculate the length in cm I used 1cm=28.4pt.

The example below is taken from the manual where you can find more examples.

Here an example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at  (0,0);
\coordinate (B) at (4,2);
\coordinate (C) at (1,3);
\coordinate (D) at (5,3);

\draw[-latex, green, thick] (A) node[left,black]{A} -- (B)node[above left,black]{B} ;
\draw (A) -- (C)node[left]{C} ;
\draw (B) -- (D) node[left]{D};

\draw[thick,-latex,red] (B) -- (intersection of A--C and B--D) coordinate(I) node[left,black]{I} ;
\draw[thick, -latex] let \p1 = ($ (I) - (A) $) in (I) -- (A) node[left,midway]{ \pgfmathparse{veclen(\x1,\y1)/28.4}\pgfmathresult cm};

\end{tikzpicture}
\end{document}

share|improve this answer
    
merci, je vais essayer –  rpapa Sep 30 '12 at 10:55
2  
\veclen(\x1,\y1)/1cm works and is more readable... –  Paul Gaborit Sep 30 '12 at 16:41
    
Indeed. Thanks. –  Marco Daniel Sep 30 '12 at 16:41
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.