15

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

2
  • 2
    Welcome to TeX.SX! arctan2 is still arctan, in my point of view, however, with a specific purpose defined by programming languages rather
    – user31729
    Commented Jun 9, 2016 at 11:10
  • 3
    @user31729 It's not just a programming convenience. They are different mathematical functions with different mathematical meanings and different input/output graphs. For example arctan( 1 / -1 )=-pi/4, whereas atan2(1,-1)=pi*3/4.
    – Kevin Holt
    Commented Oct 29, 2020 at 19:13

3 Answers 3

26

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}

enter image description here

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.

15
  • Just a quibble: isn't it traditionally defined as (y,x) not (x,y)? Commented Jun 9, 2016 at 11:14
  • 2
    Of course atan2(y,x) is not arctan(y/x); it would be silly to define it this way, wouldn't it?
    – egreg
    Commented Jun 9, 2016 at 11:25
  • 2
    @ChristianHupfer According to mathematics, it isn't. ;-)
    – egreg
    Commented Jun 9, 2016 at 11:33
  • 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}
    – egreg
    Commented Jun 9, 2016 at 11:57
  • 1
    " According to the C language man page, atan2(y,x) is the principal value of arctan y/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 used atan2 in quite a few language and it has always been the quadrant disambiguated version that returns values in a full circle. Commented Apr 11, 2019 at 15:53
5

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

Another option is to use \mathrm. So:

$\mathrm{atan2}$
$\mathrm{arctan2}$

Which gives

enter image description here

enter image description here

1
  • This is not an operator and the spacing will most likely be incorrect. Commented Aug 29, 2023 at 12:03

You must log in to answer this question.

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