14

I am looking for a conditional command with the next structure

ifnum x is even DO THIS \else DO THIS \fi

usually ifnum command have the options = < >, but I would like to have the option even and odd.

7
  • 7
    TeX has \ifodd<number><true>\else<false>\fi
    – egreg
    Commented Feb 5, 2015 at 20:39
  • Related/duplicate: If Then Else for odd page/even page
    – Werner
    Commented Feb 5, 2015 at 20:39
  • @Werner Surely not a duplicate. That one is very specialized for page numbers.
    – egreg
    Commented Feb 5, 2015 at 20:40
  • Related/duplicate: LaTeX conditional expression
    – Werner
    Commented Feb 5, 2015 at 20:42
  • @Werner Can you find \ifodd or similar in the answers to the linked question? I can't.
    – egreg
    Commented Feb 5, 2015 at 20:44

1 Answer 1

17

Among its primitive conditionals, TeX has \ifodd:

\ifodd<number><true text>\else<false text>\fi

Be careful though that testing

\ifodd\value{page}

(in LaTeX, it would be \ifodd\pageno in Plain TeX) is not reliable due to the asynchronous page breaking mechanism. Refer to If Then Else for odd page/even page for this specific problem.

On the other hand if you want to test whether another LaTeX counter is even or odd, you can safely test

\ifodd\value{<counter>}

(in Plain TeX, \ifodd\counter; <counter> or \counter stand for the specific name).

Anything that satisfies the syntax for a <number> can follow \ifodd; the usual caveat about expansion in this context apply; a space token following an explicit constant is looked for (with expansion) and ignored.

3
  • no wonder \ifeven wasn't working...
    – PatrickT
    Commented Sep 17, 2018 at 17:40
  • 1
    @PatrickT You could do \unless\ifodd; but you can't do \def\ifeven{\unless\ifodd} if you plan to nest it in other conditionals. You could do \def\even{TT\fi\unless\ifodd} and use \if\even, though. Using \ifodd seems simpler. ;-)
    – egreg
    Commented Sep 17, 2018 at 17:54
  • Oh... great! \unless\ifodd could come in handy! One for the toolbox. Thanks egreg.
    – PatrickT
    Commented Sep 17, 2018 at 18:03

You must log in to answer this question.

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