Check if number is in list of numbersChecking if an integer is a member of an integer listIssue with Expansions of Nested MacrosCheck if LuaTeX is runningCheck if next char is {Check for empty macro argumentNumerical conditional within tikz keys?Check if a number is in a comma separated list (print or display selected sections from a specific list)Check if a string is just numbers or includes lettersHow do I check if a command/variable contains par?Check number of arguments in a commandHow to check if comma list is empty?
Where to start with a child learning chess?
What are examples of (collections of) papers which "close" a field?
Most general definition of differentiation
Using brushes as low-volume drumsticks
When to bemoan and when to moan
What do these chord annotations mean?
Did Roger Rabbit exist prior to the film "Who Framed Roger Rabbit?"
How far apart are stars in a binary system?
What attributes of a Pokemon can be improved?
If I drop a bag of of say 15 alchemist fire on giant and hits it will it take 15d4 of fire damage and so on?
Looking for a restaurant in Vienna with a name similar to "An die 2 Linsen" that served huge Schnitzel
How can there be a current without a voltage?
Restore a database with a different name on the same server
Tension in a massless string being pulled at its ends with unequal forces
Prospective employer asking for my current pay slip during interview
Should I still follow "programming to an interface not implementation" even if I think using concrete class members is the simpler solution?
Are optimal hyperparameters still optimal for a deeper neural net architecture?
How to protect assets from being passed to a beneficiary in a will when they are likely to die soon also
Why do cargo airlines frequently choose passenger aircraft rather than aircraft designed specifically for cargo?
Is it Academically Dishonest to submit the same project to two different classes in the same semester?
Are soldered electrical connections code-compliant?
How do electric hot water heaters explode and what can be done to prevent that from happening?
What does 36.000€ mean?
Can I use the original Lego Boost to create the R2D2 droid set?
Check if number is in list of numbers
Checking if an integer is a member of an integer listIssue with Expansions of Nested MacrosCheck if LuaTeX is runningCheck if next char is {Check for empty macro argumentNumerical conditional within tikz keys?Check if a number is in a comma separated list (print or display selected sections from a specific list)Check if a string is just numbers or includes lettersHow do I check if a command/variable contains par?Check number of arguments in a commandHow to check if comma list is empty?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
My end goal is to highlight certain nodes depending on their value, and there will be quite a few of them so I need there to be a conditional in a for loop. I've tried the following MWE, but it doesn't do what I expect.
Essentially, I need to check if an integer is in a list of integers, and draw a node based on that.
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach x in 10, 9,..., 0
ifcase x10
or5
node[] x;
fi
endtikzpicture
enddocument
Edit: Question reworded for clarity after accepting answer.
conditionals
|
show 4 more comments
My end goal is to highlight certain nodes depending on their value, and there will be quite a few of them so I need there to be a conditional in a for loop. I've tried the following MWE, but it doesn't do what I expect.
Essentially, I need to check if an integer is in a list of integers, and draw a node based on that.
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach x in 10, 9,..., 0
ifcase x10
or5
node[] x;
fi
endtikzpicture
enddocument
Edit: Question reworded for clarity after accepting answer.
conditionals
Useifnumx=10instead ofif x10.
– Schrödinger's cat
Sep 26 at 18:19
@Schrödinger'scat I will later be using an ifcase statement, here I only used if for simplicity. The ifnum would need to be nested a bunch of times in my end goal, so I would like to avoid that.
– gilbereth
Sep 26 at 18:20
Anifcasedoesn't do a comparison, it just takes a number.ifcasexrelax <case 0>or <case 1>or <case 2>or... else <other cases>fi.
– Phelype Oleinik
Sep 26 at 18:22
You can useifcaseof course. What does this have to do withifnumvs.if? See @PhelypeOleinik comment.ifcasegoes through the integers one by one, starting from 0.
– Schrödinger's cat
Sep 26 at 18:23
@PhelypeOleinik, I've edited the question to make it clearer, I don't believe it's a duplicate because I need to check a bunch of cases.
– gilbereth
Sep 26 at 18:23
|
show 4 more comments
My end goal is to highlight certain nodes depending on their value, and there will be quite a few of them so I need there to be a conditional in a for loop. I've tried the following MWE, but it doesn't do what I expect.
Essentially, I need to check if an integer is in a list of integers, and draw a node based on that.
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach x in 10, 9,..., 0
ifcase x10
or5
node[] x;
fi
endtikzpicture
enddocument
Edit: Question reworded for clarity after accepting answer.
conditionals
My end goal is to highlight certain nodes depending on their value, and there will be quite a few of them so I need there to be a conditional in a for loop. I've tried the following MWE, but it doesn't do what I expect.
Essentially, I need to check if an integer is in a list of integers, and draw a node based on that.
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach x in 10, 9,..., 0
ifcase x10
or5
node[] x;
fi
endtikzpicture
enddocument
Edit: Question reworded for clarity after accepting answer.
conditionals
conditionals
edited Sep 26 at 18:50
gilbereth
asked Sep 26 at 18:17
gilberethgilbereth
675 bronze badges
675 bronze badges
Useifnumx=10instead ofif x10.
– Schrödinger's cat
Sep 26 at 18:19
@Schrödinger'scat I will later be using an ifcase statement, here I only used if for simplicity. The ifnum would need to be nested a bunch of times in my end goal, so I would like to avoid that.
– gilbereth
Sep 26 at 18:20
Anifcasedoesn't do a comparison, it just takes a number.ifcasexrelax <case 0>or <case 1>or <case 2>or... else <other cases>fi.
– Phelype Oleinik
Sep 26 at 18:22
You can useifcaseof course. What does this have to do withifnumvs.if? See @PhelypeOleinik comment.ifcasegoes through the integers one by one, starting from 0.
– Schrödinger's cat
Sep 26 at 18:23
@PhelypeOleinik, I've edited the question to make it clearer, I don't believe it's a duplicate because I need to check a bunch of cases.
– gilbereth
Sep 26 at 18:23
|
show 4 more comments
Useifnumx=10instead ofif x10.
– Schrödinger's cat
Sep 26 at 18:19
@Schrödinger'scat I will later be using an ifcase statement, here I only used if for simplicity. The ifnum would need to be nested a bunch of times in my end goal, so I would like to avoid that.
– gilbereth
Sep 26 at 18:20
Anifcasedoesn't do a comparison, it just takes a number.ifcasexrelax <case 0>or <case 1>or <case 2>or... else <other cases>fi.
– Phelype Oleinik
Sep 26 at 18:22
You can useifcaseof course. What does this have to do withifnumvs.if? See @PhelypeOleinik comment.ifcasegoes through the integers one by one, starting from 0.
– Schrödinger's cat
Sep 26 at 18:23
@PhelypeOleinik, I've edited the question to make it clearer, I don't believe it's a duplicate because I need to check a bunch of cases.
– gilbereth
Sep 26 at 18:23
Use
ifnumx=10 instead of if x10.– Schrödinger's cat
Sep 26 at 18:19
Use
ifnumx=10 instead of if x10.– Schrödinger's cat
Sep 26 at 18:19
@Schrödinger'scat I will later be using an ifcase statement, here I only used if for simplicity. The ifnum would need to be nested a bunch of times in my end goal, so I would like to avoid that.
– gilbereth
Sep 26 at 18:20
@Schrödinger'scat I will later be using an ifcase statement, here I only used if for simplicity. The ifnum would need to be nested a bunch of times in my end goal, so I would like to avoid that.
– gilbereth
Sep 26 at 18:20
An
ifcase doesn't do a comparison, it just takes a number. ifcasexrelax <case 0>or <case 1>or <case 2>or... else <other cases>fi.– Phelype Oleinik
Sep 26 at 18:22
An
ifcase doesn't do a comparison, it just takes a number. ifcasexrelax <case 0>or <case 1>or <case 2>or... else <other cases>fi.– Phelype Oleinik
Sep 26 at 18:22
You can use
ifcase of course. What does this have to do with ifnum vs. if? See @PhelypeOleinik comment. ifcase goes through the integers one by one, starting from 0.– Schrödinger's cat
Sep 26 at 18:23
You can use
ifcase of course. What does this have to do with ifnum vs. if? See @PhelypeOleinik comment. ifcase goes through the integers one by one, starting from 0.– Schrödinger's cat
Sep 26 at 18:23
@PhelypeOleinik, I've edited the question to make it clearer, I don't believe it's a duplicate because I need to check a bunch of cases.
– gilbereth
Sep 26 at 18:23
@PhelypeOleinik, I've edited the question to make it clearer, I don't believe it's a duplicate because I need to check a bunch of cases.
– gilbereth
Sep 26 at 18:23
|
show 4 more comments
2 Answers
2
active
oldest
votes
if I understand the question correctly, you want to check whether or not a number is equal to A, B, C etc. This can be done as follows.
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach X in 10, 9,..., 0
pgfmathtruncatemacroitest
ifnumitest=1
node at (X,0) X;
fi
endtikzpicture
enddocument
For more extensive applications I recommend the memberQ function, which may or may not become one day part of the pgf world. It tests if an integer is in a list of integers.
documentclassarticle
usepackagetikz
makeatletter
pgfmathdeclarefunctionmemberQ2%
begingroup%
edefpgfutil@tmpb0%
edefpgfutil@tmpa#2%
expandafterpgfmath@member@i#1pgfmath@token@stop
edefpgfmathresultpgfutil@tmpb%
pgfmath@smuggleonepgfmathresult%
endgroup
defpgfmath@member@i#1%
ifxpgfmath@token@stop#1%
else
ifnum#1=pgfutil@tmparelax%
gdefpgfutil@tmpb1%
%typeout#1=pgfutil@tmpa
fi%
expandafterpgfmath@member@i
fi
makeatother
begindocument
begintikzpicture
foreach X in 10, 9,..., 0
pgfmathtruncatemacroitestmemberQ(1,4,8,X)
ifnumitest=1
node at (X,0) X;
fi
endtikzpicture
enddocument
Follow up for thememberQfunction - it doesn't seem to work for multi digit integers. For example, if I have "10" in the list,itestwill evaluate to 1 whenXis 0 or 1 - but not whenXis 10. Is there any way to enable multi digit numbers to work?
– gilbereth
Sep 27 at 13:41
never mind, I got rid of "pgfutil@firstofone" in the function, and that fixed it. It seems that if the first number in your list is multiple digits, it will read it as those digits instead of as one integer.
– gilbereth
Sep 27 at 14:13
add a comment
|
ifcase doesn't work like the switch statement in other languages, in which you choose what values have a branch of their own. The syntax of ifcase is:
ifcase<number>
<case 0>
or <case 1>
or <case 2>
or <as many as you want>
else <other cases>
fi
you can't skip a value. In your code you'd need:
foreach x in 10, 9,..., 0
ifcasex relax
% 0
or % 1
or % 2
or % 3
or % 4
or % 5
node[] x;
or % 6
or % 7
or % 8
or % 9
or % 10
else % other cases
fi
which is a handful. For a small number of exceptions you could use ifnum:
foreach x in 10, 9,..., 0
ifnumx=5 relax
node[] x;
elseifnumx=10 relax
% do things with x=10
else
% possibly more cases
fifi
which can become a mess, once you have more than a couple of cases.
My suggestion: int_case:nnF. You can specify each case individually and a false branch in case no other is taken:
documentclassarticle
usepackagetikz
usepackageexpl3
ExplSyntaxOn
cs_new_eq:NN IntCasennF int_case:nnF
ExplSyntaxOff
begindocument
begintikzpicture
foreach x in 10, 9,..., 0
IntCasennF x
5node[] x;
10<Code for 10>
<Other cases>
endtikzpicture
enddocument
Thanks, your answer was really informative, although I accepted Schrödinger's cat's as it was a bit cleaner for my use case.
– gilbereth
Sep 26 at 18:47
@gilbereth No problem :-) Since you rephrased your question to "Check if number is in list of numbers", this might also be of interest to you.
– Phelype Oleinik
Sep 26 at 18:53
add a comment
|
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%2f509912%2fcheck-if-number-is-in-list-of-numbers%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 I understand the question correctly, you want to check whether or not a number is equal to A, B, C etc. This can be done as follows.
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach X in 10, 9,..., 0
pgfmathtruncatemacroitest
ifnumitest=1
node at (X,0) X;
fi
endtikzpicture
enddocument
For more extensive applications I recommend the memberQ function, which may or may not become one day part of the pgf world. It tests if an integer is in a list of integers.
documentclassarticle
usepackagetikz
makeatletter
pgfmathdeclarefunctionmemberQ2%
begingroup%
edefpgfutil@tmpb0%
edefpgfutil@tmpa#2%
expandafterpgfmath@member@i#1pgfmath@token@stop
edefpgfmathresultpgfutil@tmpb%
pgfmath@smuggleonepgfmathresult%
endgroup
defpgfmath@member@i#1%
ifxpgfmath@token@stop#1%
else
ifnum#1=pgfutil@tmparelax%
gdefpgfutil@tmpb1%
%typeout#1=pgfutil@tmpa
fi%
expandafterpgfmath@member@i
fi
makeatother
begindocument
begintikzpicture
foreach X in 10, 9,..., 0
pgfmathtruncatemacroitestmemberQ(1,4,8,X)
ifnumitest=1
node at (X,0) X;
fi
endtikzpicture
enddocument
Follow up for thememberQfunction - it doesn't seem to work for multi digit integers. For example, if I have "10" in the list,itestwill evaluate to 1 whenXis 0 or 1 - but not whenXis 10. Is there any way to enable multi digit numbers to work?
– gilbereth
Sep 27 at 13:41
never mind, I got rid of "pgfutil@firstofone" in the function, and that fixed it. It seems that if the first number in your list is multiple digits, it will read it as those digits instead of as one integer.
– gilbereth
Sep 27 at 14:13
add a comment
|
if I understand the question correctly, you want to check whether or not a number is equal to A, B, C etc. This can be done as follows.
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach X in 10, 9,..., 0
pgfmathtruncatemacroitest
ifnumitest=1
node at (X,0) X;
fi
endtikzpicture
enddocument
For more extensive applications I recommend the memberQ function, which may or may not become one day part of the pgf world. It tests if an integer is in a list of integers.
documentclassarticle
usepackagetikz
makeatletter
pgfmathdeclarefunctionmemberQ2%
begingroup%
edefpgfutil@tmpb0%
edefpgfutil@tmpa#2%
expandafterpgfmath@member@i#1pgfmath@token@stop
edefpgfmathresultpgfutil@tmpb%
pgfmath@smuggleonepgfmathresult%
endgroup
defpgfmath@member@i#1%
ifxpgfmath@token@stop#1%
else
ifnum#1=pgfutil@tmparelax%
gdefpgfutil@tmpb1%
%typeout#1=pgfutil@tmpa
fi%
expandafterpgfmath@member@i
fi
makeatother
begindocument
begintikzpicture
foreach X in 10, 9,..., 0
pgfmathtruncatemacroitestmemberQ(1,4,8,X)
ifnumitest=1
node at (X,0) X;
fi
endtikzpicture
enddocument
Follow up for thememberQfunction - it doesn't seem to work for multi digit integers. For example, if I have "10" in the list,itestwill evaluate to 1 whenXis 0 or 1 - but not whenXis 10. Is there any way to enable multi digit numbers to work?
– gilbereth
Sep 27 at 13:41
never mind, I got rid of "pgfutil@firstofone" in the function, and that fixed it. It seems that if the first number in your list is multiple digits, it will read it as those digits instead of as one integer.
– gilbereth
Sep 27 at 14:13
add a comment
|
if I understand the question correctly, you want to check whether or not a number is equal to A, B, C etc. This can be done as follows.
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach X in 10, 9,..., 0
pgfmathtruncatemacroitest
ifnumitest=1
node at (X,0) X;
fi
endtikzpicture
enddocument
For more extensive applications I recommend the memberQ function, which may or may not become one day part of the pgf world. It tests if an integer is in a list of integers.
documentclassarticle
usepackagetikz
makeatletter
pgfmathdeclarefunctionmemberQ2%
begingroup%
edefpgfutil@tmpb0%
edefpgfutil@tmpa#2%
expandafterpgfmath@member@i#1pgfmath@token@stop
edefpgfmathresultpgfutil@tmpb%
pgfmath@smuggleonepgfmathresult%
endgroup
defpgfmath@member@i#1%
ifxpgfmath@token@stop#1%
else
ifnum#1=pgfutil@tmparelax%
gdefpgfutil@tmpb1%
%typeout#1=pgfutil@tmpa
fi%
expandafterpgfmath@member@i
fi
makeatother
begindocument
begintikzpicture
foreach X in 10, 9,..., 0
pgfmathtruncatemacroitestmemberQ(1,4,8,X)
ifnumitest=1
node at (X,0) X;
fi
endtikzpicture
enddocument
if I understand the question correctly, you want to check whether or not a number is equal to A, B, C etc. This can be done as follows.
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach X in 10, 9,..., 0
pgfmathtruncatemacroitest
ifnumitest=1
node at (X,0) X;
fi
endtikzpicture
enddocument
For more extensive applications I recommend the memberQ function, which may or may not become one day part of the pgf world. It tests if an integer is in a list of integers.
documentclassarticle
usepackagetikz
makeatletter
pgfmathdeclarefunctionmemberQ2%
begingroup%
edefpgfutil@tmpb0%
edefpgfutil@tmpa#2%
expandafterpgfmath@member@i#1pgfmath@token@stop
edefpgfmathresultpgfutil@tmpb%
pgfmath@smuggleonepgfmathresult%
endgroup
defpgfmath@member@i#1%
ifxpgfmath@token@stop#1%
else
ifnum#1=pgfutil@tmparelax%
gdefpgfutil@tmpb1%
%typeout#1=pgfutil@tmpa
fi%
expandafterpgfmath@member@i
fi
makeatother
begindocument
begintikzpicture
foreach X in 10, 9,..., 0
pgfmathtruncatemacroitestmemberQ(1,4,8,X)
ifnumitest=1
node at (X,0) X;
fi
endtikzpicture
enddocument
edited Sep 27 at 15:32
gilbereth
675 bronze badges
675 bronze badges
answered Sep 26 at 18:33
Schrödinger's catSchrödinger's cat
28.9k2 gold badges43 silver badges68 bronze badges
28.9k2 gold badges43 silver badges68 bronze badges
Follow up for thememberQfunction - it doesn't seem to work for multi digit integers. For example, if I have "10" in the list,itestwill evaluate to 1 whenXis 0 or 1 - but not whenXis 10. Is there any way to enable multi digit numbers to work?
– gilbereth
Sep 27 at 13:41
never mind, I got rid of "pgfutil@firstofone" in the function, and that fixed it. It seems that if the first number in your list is multiple digits, it will read it as those digits instead of as one integer.
– gilbereth
Sep 27 at 14:13
add a comment
|
Follow up for thememberQfunction - it doesn't seem to work for multi digit integers. For example, if I have "10" in the list,itestwill evaluate to 1 whenXis 0 or 1 - but not whenXis 10. Is there any way to enable multi digit numbers to work?
– gilbereth
Sep 27 at 13:41
never mind, I got rid of "pgfutil@firstofone" in the function, and that fixed it. It seems that if the first number in your list is multiple digits, it will read it as those digits instead of as one integer.
– gilbereth
Sep 27 at 14:13
Follow up for the
memberQ function - it doesn't seem to work for multi digit integers. For example, if I have "10" in the list, itest will evaluate to 1 when X is 0 or 1 - but not when X is 10. Is there any way to enable multi digit numbers to work?– gilbereth
Sep 27 at 13:41
Follow up for the
memberQ function - it doesn't seem to work for multi digit integers. For example, if I have "10" in the list, itest will evaluate to 1 when X is 0 or 1 - but not when X is 10. Is there any way to enable multi digit numbers to work?– gilbereth
Sep 27 at 13:41
never mind, I got rid of "pgfutil@firstofone" in the function, and that fixed it. It seems that if the first number in your list is multiple digits, it will read it as those digits instead of as one integer.
– gilbereth
Sep 27 at 14:13
never mind, I got rid of "pgfutil@firstofone" in the function, and that fixed it. It seems that if the first number in your list is multiple digits, it will read it as those digits instead of as one integer.
– gilbereth
Sep 27 at 14:13
add a comment
|
ifcase doesn't work like the switch statement in other languages, in which you choose what values have a branch of their own. The syntax of ifcase is:
ifcase<number>
<case 0>
or <case 1>
or <case 2>
or <as many as you want>
else <other cases>
fi
you can't skip a value. In your code you'd need:
foreach x in 10, 9,..., 0
ifcasex relax
% 0
or % 1
or % 2
or % 3
or % 4
or % 5
node[] x;
or % 6
or % 7
or % 8
or % 9
or % 10
else % other cases
fi
which is a handful. For a small number of exceptions you could use ifnum:
foreach x in 10, 9,..., 0
ifnumx=5 relax
node[] x;
elseifnumx=10 relax
% do things with x=10
else
% possibly more cases
fifi
which can become a mess, once you have more than a couple of cases.
My suggestion: int_case:nnF. You can specify each case individually and a false branch in case no other is taken:
documentclassarticle
usepackagetikz
usepackageexpl3
ExplSyntaxOn
cs_new_eq:NN IntCasennF int_case:nnF
ExplSyntaxOff
begindocument
begintikzpicture
foreach x in 10, 9,..., 0
IntCasennF x
5node[] x;
10<Code for 10>
<Other cases>
endtikzpicture
enddocument
Thanks, your answer was really informative, although I accepted Schrödinger's cat's as it was a bit cleaner for my use case.
– gilbereth
Sep 26 at 18:47
@gilbereth No problem :-) Since you rephrased your question to "Check if number is in list of numbers", this might also be of interest to you.
– Phelype Oleinik
Sep 26 at 18:53
add a comment
|
ifcase doesn't work like the switch statement in other languages, in which you choose what values have a branch of their own. The syntax of ifcase is:
ifcase<number>
<case 0>
or <case 1>
or <case 2>
or <as many as you want>
else <other cases>
fi
you can't skip a value. In your code you'd need:
foreach x in 10, 9,..., 0
ifcasex relax
% 0
or % 1
or % 2
or % 3
or % 4
or % 5
node[] x;
or % 6
or % 7
or % 8
or % 9
or % 10
else % other cases
fi
which is a handful. For a small number of exceptions you could use ifnum:
foreach x in 10, 9,..., 0
ifnumx=5 relax
node[] x;
elseifnumx=10 relax
% do things with x=10
else
% possibly more cases
fifi
which can become a mess, once you have more than a couple of cases.
My suggestion: int_case:nnF. You can specify each case individually and a false branch in case no other is taken:
documentclassarticle
usepackagetikz
usepackageexpl3
ExplSyntaxOn
cs_new_eq:NN IntCasennF int_case:nnF
ExplSyntaxOff
begindocument
begintikzpicture
foreach x in 10, 9,..., 0
IntCasennF x
5node[] x;
10<Code for 10>
<Other cases>
endtikzpicture
enddocument
Thanks, your answer was really informative, although I accepted Schrödinger's cat's as it was a bit cleaner for my use case.
– gilbereth
Sep 26 at 18:47
@gilbereth No problem :-) Since you rephrased your question to "Check if number is in list of numbers", this might also be of interest to you.
– Phelype Oleinik
Sep 26 at 18:53
add a comment
|
ifcase doesn't work like the switch statement in other languages, in which you choose what values have a branch of their own. The syntax of ifcase is:
ifcase<number>
<case 0>
or <case 1>
or <case 2>
or <as many as you want>
else <other cases>
fi
you can't skip a value. In your code you'd need:
foreach x in 10, 9,..., 0
ifcasex relax
% 0
or % 1
or % 2
or % 3
or % 4
or % 5
node[] x;
or % 6
or % 7
or % 8
or % 9
or % 10
else % other cases
fi
which is a handful. For a small number of exceptions you could use ifnum:
foreach x in 10, 9,..., 0
ifnumx=5 relax
node[] x;
elseifnumx=10 relax
% do things with x=10
else
% possibly more cases
fifi
which can become a mess, once you have more than a couple of cases.
My suggestion: int_case:nnF. You can specify each case individually and a false branch in case no other is taken:
documentclassarticle
usepackagetikz
usepackageexpl3
ExplSyntaxOn
cs_new_eq:NN IntCasennF int_case:nnF
ExplSyntaxOff
begindocument
begintikzpicture
foreach x in 10, 9,..., 0
IntCasennF x
5node[] x;
10<Code for 10>
<Other cases>
endtikzpicture
enddocument
ifcase doesn't work like the switch statement in other languages, in which you choose what values have a branch of their own. The syntax of ifcase is:
ifcase<number>
<case 0>
or <case 1>
or <case 2>
or <as many as you want>
else <other cases>
fi
you can't skip a value. In your code you'd need:
foreach x in 10, 9,..., 0
ifcasex relax
% 0
or % 1
or % 2
or % 3
or % 4
or % 5
node[] x;
or % 6
or % 7
or % 8
or % 9
or % 10
else % other cases
fi
which is a handful. For a small number of exceptions you could use ifnum:
foreach x in 10, 9,..., 0
ifnumx=5 relax
node[] x;
elseifnumx=10 relax
% do things with x=10
else
% possibly more cases
fifi
which can become a mess, once you have more than a couple of cases.
My suggestion: int_case:nnF. You can specify each case individually and a false branch in case no other is taken:
documentclassarticle
usepackagetikz
usepackageexpl3
ExplSyntaxOn
cs_new_eq:NN IntCasennF int_case:nnF
ExplSyntaxOff
begindocument
begintikzpicture
foreach x in 10, 9,..., 0
IntCasennF x
5node[] x;
10<Code for 10>
<Other cases>
endtikzpicture
enddocument
answered Sep 26 at 18:34
Phelype OleinikPhelype Oleinik
40.6k9 gold badges71 silver badges133 bronze badges
40.6k9 gold badges71 silver badges133 bronze badges
Thanks, your answer was really informative, although I accepted Schrödinger's cat's as it was a bit cleaner for my use case.
– gilbereth
Sep 26 at 18:47
@gilbereth No problem :-) Since you rephrased your question to "Check if number is in list of numbers", this might also be of interest to you.
– Phelype Oleinik
Sep 26 at 18:53
add a comment
|
Thanks, your answer was really informative, although I accepted Schrödinger's cat's as it was a bit cleaner for my use case.
– gilbereth
Sep 26 at 18:47
@gilbereth No problem :-) Since you rephrased your question to "Check if number is in list of numbers", this might also be of interest to you.
– Phelype Oleinik
Sep 26 at 18:53
Thanks, your answer was really informative, although I accepted Schrödinger's cat's as it was a bit cleaner for my use case.
– gilbereth
Sep 26 at 18:47
Thanks, your answer was really informative, although I accepted Schrödinger's cat's as it was a bit cleaner for my use case.
– gilbereth
Sep 26 at 18:47
@gilbereth No problem :-) Since you rephrased your question to "Check if number is in list of numbers", this might also be of interest to you.
– Phelype Oleinik
Sep 26 at 18:53
@gilbereth No problem :-) Since you rephrased your question to "Check if number is in list of numbers", this might also be of interest to you.
– Phelype Oleinik
Sep 26 at 18:53
add a comment
|
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%2f509912%2fcheck-if-number-is-in-list-of-numbers%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
Use
ifnumx=10instead ofif x10.– Schrödinger's cat
Sep 26 at 18:19
@Schrödinger'scat I will later be using an ifcase statement, here I only used if for simplicity. The ifnum would need to be nested a bunch of times in my end goal, so I would like to avoid that.
– gilbereth
Sep 26 at 18:20
An
ifcasedoesn't do a comparison, it just takes a number.ifcasexrelax <case 0>or <case 1>or <case 2>or... else <other cases>fi.– Phelype Oleinik
Sep 26 at 18:22
You can use
ifcaseof course. What does this have to do withifnumvs.if? See @PhelypeOleinik comment.ifcasegoes through the integers one by one, starting from 0.– Schrödinger's cat
Sep 26 at 18:23
@PhelypeOleinik, I've edited the question to make it clearer, I don't believe it's a duplicate because I need to check a bunch of cases.
– gilbereth
Sep 26 at 18:23