1

I've just started to use tikz and I want to use \pgfmathanglebetweenlines, but I'm obviously not understanding it. I was expecting the following code to report the angle in the \try parameters, but it comes out as 0o each time (or sometimes 45o each time or 90oeach time). What have I done wrong?

%
% preamble
%
\documentclass[10pt]{article}
\usepackage{amssymb,amsmath}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
%
% drawing.
%
\begin{document}
\def\try#1{
\begin{tikzpicture}[scale=4]
\coordinate[label=below:$O$](O)at(0,0);
\coordinate[label=$X$](X)at(1,0);
\coordinate[label=$Y$](Y)at({cos(#1)},{sin(#1)});
\draw(Y)--(O)--(X);
\pgfmathanglebetweenlines{(O)}{(Y)}{(O)}{(X)}                                                                
\coordinate[label=right:{$\angle YOX=\pgfmathresult^\circ$}](label)at(X);             
\end{tikzpicture}
\\} 
\try{20}\try{45}\try{60}\try{180}\try{405}\try{-45}
\end{document}   

1 Answer 1

2

There's a couple of problems here. Firstly, \pgfmathanglebetweenlines requires basic layer coordinate specifications (e.g., in this case \pgfpointanchor{Y}{center}). Secondly \pgfmathresult is redefined every time the mathematical engine is used so by the time it is typeset the angle measurement is lost.

A solution is shown below. Note I have also added in a correction for acute angles (I think the documentation for \pgfmathanglebetweenlines isn't quite right) and to get the angle rounded to the nearest integer.

\documentclass[varwidth,border=5]{standalone}
\usepackage{tikz}
\def\try#1{%
\begin{tikzpicture}[scale=4]
\coordinate[label=below:$O$] (O) at (0,0);
\coordinate[label=$X$]       (X) at (1,0);
\coordinate[label=$Y$]       (Y) at (cos #1, sin #1);
\draw (Y) -- (O) -- (X);
\pgfmathanglebetweenlines%
  {\pgfpointanchor{O}{center}}{\pgfpointanchor{Y}{center}}
  {\pgfpointanchor{O}{center}}{\pgfpointanchor{X}{center}}
\pgfmathparse{int(round(min(\pgfmathresult, 360-\pgfmathresult))}% Correction
\let\angleyox=\pgfmathresult                                                              
\coordinate [label=right:{$\angle YOX=\angleyox^\circ$}] (label) at (X);             
\end{tikzpicture}
\\}
\begin{document}
\foreach \a in {20, 45, 60, 180, 405,-45}{\try{\a}}
\end{document}  

But! The basic layer stuff isn't necessary. The calc library provides all the stuff you need:

\documentclass[varwidth,border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\try#1{%
\begin{tikzpicture}[scale=4]
\coordinate[label=below:$O$] (O) at (0,0);
\coordinate[label=$X$]       (X) at (1,0);
\coordinate[label=$Y$]       (Y) at (cos #1, sin #1);
\draw (Y) -- (O) -- (X);
\path let \p1=(O),\p2=(Y),\p3=(X),
   \n1={atan2(\y2-\y1,\x2-\x1)},
   \n2={atan2(\y3-\y1,\x3-\x1)} in
   node at (X) [right] {\pgfmathparse{int(abs(\n2-\n1))}% Correction
     $\angle YOX=\pgfmathresult^\circ$};
\end{tikzpicture}
\\}
\begin{document}
\foreach \a in {20, 45, 60, 180, 405,-45}{\try{\a}}
\end{document} 

enter image description here

2
  • Perfect answer. The documentation of \pgfmathanglebetweenlines errs as you note. It should read "anticlockwise" rather than "clockwise", so X and Y should have been interchanged in the macro call. I have added \def\pb#1{\pgfpointanchor{#1}{center}} to my standard preamble so, hopefully, I'll now also be able to successfully use the shift parameter in \pgftransformcm. I notice that without the correction applied the printed results can differ by a value < 0.06. Is this just a matter of the limits of accuracy in arithmetical calculations? Commented Jan 27, 2017 at 14:18
  • By the way how did you get the result to display on the page? Commented Jan 27, 2017 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .