How to compare integers in TeX?How to compare two counters using TeX conditionals in ConTeXt?Numerical conditional within tikz keys?Set PGFplots coordinate label using ifnumA more elegant version of the ifnot macroWhy do we need ldots?Commands in TeX which are different in LaTeXHow to compare a string to a cs from a read properly?Seemingly unused edef prior to an ifx mysteriously affects the outcome of the ifx. Why?What tokens are in the end of line?How do I define a new TeX register with arguments in LuaTeX?
GPLv3 forces us to make code available, but to who?
How to bring home documents from work?
Delete n lines skip 1 line script
Diminished data rate with logic output optoisolator
Impossible violin chord, how to fix this?
A word that refers to saying something in an attempt to anger or embarrass someone into doing something that they don’t want to do?
Phonetic distortion when words are borrowed among languages
Beyond Futuristic Technology for an Alien Warship?
How deep is the liquid in a half-full hemisphere?
How big would the ice ball have to be to deliver all the water at once?
Calculate the Ultraradical
Convert a string of digits from words to an integer
Implementation of a Thread Pool in C++
What action is recommended if your accommodation refuses to let you leave without paying additional fees?
Knights and Knaves: What does C say?
Is the "spacetime" the same thing as the mathematical 4th dimension?
What "level" is a monster, for the Tough feat?
Detail vs. filler
Shell Sort, Insertion Sort, Bubble Sort, Selection Sort Algorithms (Python)
Isn't the detector always measuring, and thus always collapsing the state?
What could cause lower torque than normal during cruise in a King Air 300?
How do I automatically add linebreaks "\" to the end of each row of a tabular environment?
Garage door sticks on a bolt
Realistically, how much do you need to start investing?
How to compare integers in TeX?
How to compare two counters using TeX conditionals in ConTeXt?Numerical conditional within tikz keys?Set PGFplots coordinate label using ifnumA more elegant version of the ifnot macroWhy do we need ldots?Commands in TeX which are different in LaTeXHow to compare a string to a cs from a read properly?Seemingly unused edef prior to an ifx mysteriously affects the outcome of the ifx. Why?What tokens are in the end of line?How do I define a new TeX register with arguments in LuaTeX?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I do not understand how I can compare integers in TeX.
documentclass[]article
begindocument
if 1<>0
1 is not equal 0.
else
1 equals 0.
fi
enddocument
According to this code snippet 1 equals 0. Why? I have read that integer comparisons are done with ifnum
, but this command throws errors.
tex-core conditionals
add a comment
|
I do not understand how I can compare integers in TeX.
documentclass[]article
begindocument
if 1<>0
1 is not equal 0.
else
1 equals 0.
fi
enddocument
According to this code snippet 1 equals 0. Why? I have read that integer comparisons are done with ifnum
, but this command throws errors.
tex-core conditionals
3
ifnum...else...fi
is the integer comparator in TeX. However,<>
is not a valid comparison.ifnum0=1relax 0 equals 1else 0 is not equal 1fi
. What you wrote in your question compared the tokens1
and<
, which were not found identical, so that theelse
clause was invoked.
– Steven B. Segletes
18 hours ago
add a comment
|
I do not understand how I can compare integers in TeX.
documentclass[]article
begindocument
if 1<>0
1 is not equal 0.
else
1 equals 0.
fi
enddocument
According to this code snippet 1 equals 0. Why? I have read that integer comparisons are done with ifnum
, but this command throws errors.
tex-core conditionals
I do not understand how I can compare integers in TeX.
documentclass[]article
begindocument
if 1<>0
1 is not equal 0.
else
1 equals 0.
fi
enddocument
According to this code snippet 1 equals 0. Why? I have read that integer comparisons are done with ifnum
, but this command throws errors.
tex-core conditionals
tex-core conditionals
edited 5 hours ago


Phelype Oleinik
34.5k7 gold badges59 silver badges115 bronze badges
34.5k7 gold badges59 silver badges115 bronze badges
asked 18 hours ago
nullnull
2111 silver badge6 bronze badges
2111 silver badge6 bronze badges
3
ifnum...else...fi
is the integer comparator in TeX. However,<>
is not a valid comparison.ifnum0=1relax 0 equals 1else 0 is not equal 1fi
. What you wrote in your question compared the tokens1
and<
, which were not found identical, so that theelse
clause was invoked.
– Steven B. Segletes
18 hours ago
add a comment
|
3
ifnum...else...fi
is the integer comparator in TeX. However,<>
is not a valid comparison.ifnum0=1relax 0 equals 1else 0 is not equal 1fi
. What you wrote in your question compared the tokens1
and<
, which were not found identical, so that theelse
clause was invoked.
– Steven B. Segletes
18 hours ago
3
3
ifnum...else...fi
is the integer comparator in TeX. However, <>
is not a valid comparison. ifnum0=1relax 0 equals 1else 0 is not equal 1fi
. 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
18 hours ago
ifnum...else...fi
is the integer comparator in TeX. However, <>
is not a valid comparison. ifnum0=1relax 0 equals 1else 0 is not equal 1fi
. 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
18 hours ago
add a comment
|
2 Answers
2
active
oldest
votes
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 >
.
add a comment
|
Just for completeness: (La)TeX does have something that is equivalent to <>
: unlessifnum#1=#2
.
documentclass[]article
begindocument
unlessifnum1=0
1 is not equal 0.
else
1 equals 0.
fi
enddocument
In this case it does not make things shorter or simpler, but sometimes this helps making the code easier to understand.
This requires LaTeX. The question is tagged "tex-core".
– barbara beeton
15 hours ago
@barbarabeeton Thanks! Corrected. The MWE is clearly LaTeX.
– Schrödinger's cat
15 hours ago
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
14 hours ago
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, andunless
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
14 hours ago
@barbarabeeton You could have used itunless
it woult not yet have been implemented? ;-)
– Schrödinger's cat
9 hours ago
|
show 2 more comments
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "85"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f509581%2fhow-to-compare-integers-in-tex%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 >
.
add a comment
|
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 >
.
add a comment
|
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 >
.
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 >
.
edited 18 hours ago


egreg
771k91 gold badges2013 silver badges3369 bronze badges
771k91 gold badges2013 silver badges3369 bronze badges
answered 18 hours ago


Phelype OleinikPhelype Oleinik
34.5k7 gold badges59 silver badges115 bronze badges
34.5k7 gold badges59 silver badges115 bronze badges
add a comment
|
add a comment
|
Just for completeness: (La)TeX does have something that is equivalent to <>
: unlessifnum#1=#2
.
documentclass[]article
begindocument
unlessifnum1=0
1 is not equal 0.
else
1 equals 0.
fi
enddocument
In this case it does not make things shorter or simpler, but sometimes this helps making the code easier to understand.
This requires LaTeX. The question is tagged "tex-core".
– barbara beeton
15 hours ago
@barbarabeeton Thanks! Corrected. The MWE is clearly LaTeX.
– Schrödinger's cat
15 hours ago
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
14 hours ago
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, andunless
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
14 hours ago
@barbarabeeton You could have used itunless
it woult not yet have been implemented? ;-)
– Schrödinger's cat
9 hours ago
|
show 2 more comments
Just for completeness: (La)TeX does have something that is equivalent to <>
: unlessifnum#1=#2
.
documentclass[]article
begindocument
unlessifnum1=0
1 is not equal 0.
else
1 equals 0.
fi
enddocument
In this case it does not make things shorter or simpler, but sometimes this helps making the code easier to understand.
This requires LaTeX. The question is tagged "tex-core".
– barbara beeton
15 hours ago
@barbarabeeton Thanks! Corrected. The MWE is clearly LaTeX.
– Schrödinger's cat
15 hours ago
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
14 hours ago
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, andunless
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
14 hours ago
@barbarabeeton You could have used itunless
it woult not yet have been implemented? ;-)
– Schrödinger's cat
9 hours ago
|
show 2 more comments
Just for completeness: (La)TeX does have something that is equivalent to <>
: unlessifnum#1=#2
.
documentclass[]article
begindocument
unlessifnum1=0
1 is not equal 0.
else
1 equals 0.
fi
enddocument
In this case it does not make things shorter or simpler, but sometimes this helps making the code easier to understand.
Just for completeness: (La)TeX does have something that is equivalent to <>
: unlessifnum#1=#2
.
documentclass[]article
begindocument
unlessifnum1=0
1 is not equal 0.
else
1 equals 0.
fi
enddocument
In this case it does not make things shorter or simpler, but sometimes this helps making the code easier to understand.
edited 15 hours ago
answered 16 hours ago


Schrödinger's catSchrödinger's cat
9,57013 silver badges28 bronze badges
9,57013 silver badges28 bronze badges
This requires LaTeX. The question is tagged "tex-core".
– barbara beeton
15 hours ago
@barbarabeeton Thanks! Corrected. The MWE is clearly LaTeX.
– Schrödinger's cat
15 hours ago
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
14 hours ago
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, andunless
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
14 hours ago
@barbarabeeton You could have used itunless
it woult not yet have been implemented? ;-)
– Schrödinger's cat
9 hours ago
|
show 2 more comments
This requires LaTeX. The question is tagged "tex-core".
– barbara beeton
15 hours ago
@barbarabeeton Thanks! Corrected. The MWE is clearly LaTeX.
– Schrödinger's cat
15 hours ago
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
14 hours ago
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, andunless
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
14 hours ago
@barbarabeeton You could have used itunless
it woult not yet have been implemented? ;-)
– Schrödinger's cat
9 hours ago
This requires LaTeX. The question is tagged "tex-core".
– barbara beeton
15 hours ago
This requires LaTeX. The question is tagged "tex-core".
– barbara beeton
15 hours ago
@barbarabeeton Thanks! Corrected. The MWE is clearly LaTeX.
– Schrödinger's cat
15 hours ago
@barbarabeeton Thanks! Corrected. The MWE is clearly LaTeX.
– Schrödinger's cat
15 hours ago
2
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
14 hours ago
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
14 hours ago
1
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
14 hours ago
@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
14 hours ago
@barbarabeeton You could have used it
unless
it woult not yet have been implemented? ;-)– Schrödinger's cat
9 hours ago
@barbarabeeton You could have used it
unless
it woult not yet have been implemented? ;-)– Schrödinger's cat
9 hours ago
|
show 2 more comments
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f509581%2fhow-to-compare-integers-in-tex%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
ifnum...else...fi
is the integer comparator in TeX. However,<>
is not a valid comparison.ifnum0=1relax 0 equals 1else 0 is not equal 1fi
. What you wrote in your question compared the tokens1
and<
, which were not found identical, so that theelse
clause was invoked.– Steven B. Segletes
18 hours ago