Is there an arctan2 / atan2 function in LaTeX, which can print an aesthetic result like the other trigonometric functions \sin
\cos
... ?
3 Answers
amsmath
provides \DeclareMathOperator{\foo}{foo}
to define new operator names that are typeset similar to \sin
and \cos
, \ln
, i.e. upright font and correct spacings.
This can be used with \atantwo
as well (the 2
isn't allowed as part of the macro name, however, therefor \atantwo
.
\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator{\atantwo}{atan2}
\DeclareMathOperator{\arctantwo}{arctan2}
\begin{document}
\[
\atantwo ( y, x)
\]
\[
\arctantwo ( y, x)
\]
\[
\sin (x y)
\]
\end{document}
Note: The atan2(y,x)
function determines the angle in polar coordinates and is related to but different than the function atan(y/x)
. The two are not interchangeable. The atan
function has output range (-pi/2,pi/2)
, whereas atan2
has an output range of (-pi,pi]
. If x>0
, then atan2(y,x)=atan(y/x)
. If x<0
, then atan2(y,x)=atan(y/x) +/- pi
. And if x=0
then atan(y/x)
is undefined while atan2(y,x)=pi/2
for y>0
or -pi/2
for y<0
. This definition of atan2
(or equivalently arctan2
) is the same both for mathematics and for common programming languages including C, FORTRAN, and matlab.
-
Just a quibble: isn't it traditionally defined as
(y,x)
not(x,y)
? Jun 9, 2016 at 11:14 -
2Of course atan2(y,x) is not arctan(y/x); it would be silly to define it this way, wouldn't it?– egregJun 9, 2016 at 11:25
-
2
-
3@ChristianHupfer The description at en.wikipedia.org/wiki/Atan2 seems quite accurate. Talking about TeX, I wouldn't be displeased by
\DeclareMathOperator{\atantwo}{atan_2}
– egregJun 9, 2016 at 11:57 -
1" According to the C language man page,
atan2(y,x)
is the principal value of arctany/x
" The same page goes on to say that the returned value is in the range[-pi, pi]
which tells you that the author of the page was confused about the meaning of "principle value" in this context. I've usedatan2
in quite a few language and it has always been the quadrant disambiguated version that returns values in a full circle. Apr 11, 2019 at 15:53
If you want you can let them be a little bit more intelligent to enable \atan2
and \arctan2
\long\def\gobbleone#1{}
\protected\def\atan{\futurelet\tmptoken\doatan}
\protected\def\doatan{\operatorname{atan\ifx\tmptoken22\fi}%
\ifx\tmptoken2\expandafter\gobbleone\fi}
\protected\def\arctan{\futurelet\tmptoken\doarctan}
\protected\def\doarctan{\operatorname{arc\,tan\ifx\tmptoken22\fi}%
\ifx\tmptoken2\expandafter\gobbleone\fi}
$\atan2(y, x) \arctan2(y, x) \sin(x, y)$
-
This is not an operator and the spacing will most likely be incorrect. Aug 29 at 12:03
arctan2
is stillarctan
, in my point of view, however, with a specific purpose defined by programming languages rather