3

My question is this. Is there a way to measure the distance of two points? (The points are drawn with TikZ.)

Thank you for your answer.

egreg
  • 1,121,712
encuka
  • 103

1 Answers1

2

You can use veclen function.

\documentclass[varwidth,border=50]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
  \draw (30:1) coordinate(a) -- (75:3) coordinate(b); 
  \path let \p1=(a), \p2=(b), \n1={veclen(\x2-\x1,\y2-\y1)} in node{\n1};
\end{tikzpicture}
\end{document}

And if you want the length in cm or inches, you can see this answer.

enter image description here

Kpym
  • 23,002
  • How do I store \n1 for later use like in \node [minimum width=\n1]{};? I would rather not use \newlength because I'd like to keep the code localized and in pgf. Therefore, I tried inserting \pgfsetmacrolength into your last node: node{\n1\pgfsetmacrolength{\var}{\n1}}. – Jonathan Komar Feb 24 '16 at 09:51
  • 1
    @macmadness86 It is hard to know what is the best thing to do without example. One solution is to define a global macro \var using ... in \pgfextra{\xdef\var{\n1}}. You don't need a node just for saving the macro. – Kpym Feb 24 '16 at 14:24