[Tex/LaTex] Passing argument to \ifnum


\ifnum works fine it it's got two straight numbers. However, when calculation is involved, I get bunch of ! Missing = inserted for \ifnum. errors. Is there any way to solve that problem?

\ifnum\ifnum#1>180 1\else\ifnum#1>-180 0\else1\fi\fi =1

\axes{200}; % fine
\axes{180+\anga}; % not fine

Best Answer

You can't use arithmetic expression in TeX primitives. However, since modern TeX all support eTeX extensions, you can use \numexpr to do the calculation:

\ifnum\ifnum\numexpr#1\relax>180 1\else\ifnum\numexpr#1\relax>-180 0\else1\fi\fi =1

\axes{200}; % fine
\axes{180+\anga}; % fine

If you use pgf or tikz to draw axis (why not?), you can use pgfmath for length arithmetic.

\usepackage{tikz} % or pgf


    ifthenelse(abs(#1) > 180, "out", "in")}%

\axes{200.3}; % out

\axes{180+\anga}; % in

\axes{-5.5}; % out

Related Question