[Tex/LaTex] LaTeX: Use command argument in conditional

comparisonconditionalsmacros

I'm in the process of writing a command that outputs a certain text only if its argument is a nonzero number. I'd like to be able to write something like:

\newcommand{mycommand}[1]{
\if{#1 != 0}
Some text, because it's nonzero
\else
Some other text, because it's zero
\fi
}

\mycommand{0}
\mycommand{1}

And get the output:

Some other text, because it's zero
Some text, because it's nonzero

I'm running into trouble getting the conditional right, though – I'm not sure that LaTeX is treating #1 like an integer to be checked (or even if that's the right syntax for not-equals – I wasn't able to find a ton of information on integer conditionals). How might I go about writing this conditional?

Best Answer

You can do it like this:

\newcommand{\mycommand}[1]{%
\ifnum0=#1\relax
  Some other text, because it's zero
\else
  Some text, because it's nonzero
\fi
}

Or with the ifthen package:

\ifthenelse{0=#1}{%
    Some other text, because it's zero
}{%
    Some text, because it's nonzero
}
Related Question