# How to have the arctan2 / atan2 function in LaTeX?

Is there an arctan2 / atan2 function in LaTeX, which can print an aesthetic result like the other trigonometric functions \sin \cos... ?

• Welcome to TeX.SX! arctan2 is still arctan, in my point of view, however, with a specific purpose defined by programming languages rather – user31729 Jun 9 '16 at 11:10

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.

The atan2(y,x) function has different meanings: In mathematics, it determines the angle in polar coordinates, in computing languages it is the principle value of atan(y/x), at least for the C programming language and FORTRAN.

\documentclass{article}

\usepackage{amsmath}

\DeclareMathOperator{\atantwo}{atan2}

\DeclareMathOperator{\arctantwo}{arctan2}

\begin{document}

$\atantwo ( y, x)$

$\arctantwo ( y, x)$

$\sin (x y)$

\end{document}

• Just a quibble: isn't it traditionally defined as (y,x) not (x,y)? – Steven B. Segletes Jun 9 '16 at 11:14
• @StevenB.Segletes: Depends on the meaning of x and y, perhaps? ;-) At least for C it's the way you said. I'll change – user31729 Jun 9 '16 at 11:14
• Of course atan2(y,x) is not arctan(y/x); it would be silly to define it this way, wouldn't it? – egreg Jun 9 '16 at 11:25
• @ChristianHupfer According to mathematics, it isn't. ;-) – egreg Jun 9 '16 at 11:33
• @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} – egreg Jun 9 '16 at 11:57

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)$