How to have the arctan2 / atan2 function in LaTeX?
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.
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)$