13

I do not understand how I can compare integers in TeX.

\documentclass[]{article}    
\begin{document}

\if 1<>0
1 is not equal 0.
\else
1 equals 0.
\fi

\end{document}

According to this code snippet 1 equals 0. Why? I have read that integer comparisons are done with \ifnum, but this command throws errors.

  • 3
    \ifnum...\else...\fi is the integer comparator in TeX. However, <> is not a valid comparison. \ifnum0=1\relax 0 equals 1\else 0 is not equal 1\fi. What you wrote in your question compared the tokens 1 and <, which were not found identical, so that the \else clause was invoked. – Steven B. Segletes Sep 24 '19 at 12:07
21

\if compares two tokens, independently of what they mean. The test \if 1<>0 compares 1 and < and yields false, thus you see 1 equals 0. For the sake of the example, if you had, \if 11<>0 then the test would be true because TeX would compare 1 and the next 1 and would return true. Then the test:

\if 11<>0
11 is not equal 0.
\else
11 equals 0.
\fi

would print:

<>0 11 is not equal 0.

because the tokens <>0 would not be used by \if, so TeX would simply write them on the output.

To do an integer comparison you need \ifnum:

\ifnum 1=0
1 equals 0.
\else
1 is not equal 0.
\fi

Also, TeX does not have a not equal to comparison. You can only compare with <, =, or >.

| improve this answer | |
11

Just for completeness: (La)TeX does have something that is equivalent to <>: \unless\ifnum#1=#2.

\documentclass[]{article}    
\begin{document}

\unless\ifnum1=0
1 is not equal 0.
\else
1 equals 0.
\fi

\end{document}

In this case it does not make things shorter or simpler, but sometimes this helps making the code easier to understand.

| improve this answer | |
  • 1
    This requires LaTeX. The question is tagged "tex-core". – barbara beeton Sep 24 '19 at 15:00
  • @barbarabeeton Thanks! Corrected. The MWE is clearly LaTeX. – user194703 Sep 24 '19 at 15:02
  • 2
    This does not require LaTeX: it requires e-TeX extensions, which, these days, are normally enabled by default. This has probably been a “slip of the tongue” of @barbarabeeton; it is a fact, however, that, since 2015, if I recollect correctly, the LaTeX kernel requires e-TeX extensions. – GuM Sep 24 '19 at 16:25
  • 1
    @GuM -- I accept the fact that this requires e-TeX extensions. However, I still consider "tex-core" to pertain to the original Knuthian TeX, and \unless isn't there. (I know. I could have used it more than once.) This limitation is present in the "tex-core" explication: "regardless of extensions (eTeX, etc.)". – barbara beeton Sep 24 '19 at 16:53
  • 1
    @GuM -- The way the question is now phrased, that conclusion is solid. However, the original phrasing (I checked the edit trail) said "... compare integers in LaTeX." My objection (which still holds, but has never been phrased that way) should be that the "tex-core" tag isn't appropriate. (Apologies for spreading confusion.) – barbara beeton Sep 25 '19 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.