Identifying if macro is an alias

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I use a def as an alias like so:
defaliasfoo
later on I would like to check if alias is an alias for foo. (X is an alias of Y means the current definition of X is defXY).
See the MWE below for what I tried:
MWE:
documentclassarticle
%usepackagetrace
deffoobar
defbarbar
newcommandisFoo[1]
% expandafterifexpandafternoexpandaliasnoexpandfoo %false positives
% expandafterifxaliasfoo %this one compares text
% expandafterifxexpandafternoexpandaliasnoexpandfoo %false negatives
It is a textbackslash foo #1
else
Not a textbackslash foo #1
fi
begindocument
defaliasfoo
isFooab
defaliasbar
isFooxy
defbarbaz
isFooxy
enddocument
The idea behind the expandafter/noexpand code is to expand alias exactly once and then compare the two tokens without any further expansion to avoid comparing their return values.
conditionals
add a comment |Â
up vote
2
down vote
favorite
I use a def as an alias like so:
defaliasfoo
later on I would like to check if alias is an alias for foo. (X is an alias of Y means the current definition of X is defXY).
See the MWE below for what I tried:
MWE:
documentclassarticle
%usepackagetrace
deffoobar
defbarbar
newcommandisFoo[1]
% expandafterifexpandafternoexpandaliasnoexpandfoo %false positives
% expandafterifxaliasfoo %this one compares text
% expandafterifxexpandafternoexpandaliasnoexpandfoo %false negatives
It is a textbackslash foo #1
else
Not a textbackslash foo #1
fi
begindocument
defaliasfoo
isFooab
defaliasbar
isFooxy
defbarbaz
isFooxy
enddocument
The idea behind the expandafter/noexpand code is to expand alias exactly once and then compare the two tokens without any further expansion to avoid comparing their return values.
conditionals
I don't think I understand... You want to check ifaliasexpands tofoowithout comparing if the contents offoousing, for example,expandafterifxaliasfoo?
â Phelype Oleinik
Aug 10 at 19:35
1
@PhelypeOleinik this is not fail proofed:deffoobardefbarAdefbazAexpandafterifxfoobaz trueelse falsefiyields true, though it should yield false.
â Skillmon
Aug 10 at 20:17
@Skillmon Makes sense... If OP had replied I would've done something similar to your approach, but usingstringandifxinstead ofdetokenizeandpdf@strcmp, but then I forgot :P
â Phelype Oleinik
Aug 10 at 20:28
@PhelypeOleinik: Op disappeared from the keyboard for a while,... but in my simple case what you described did not work (see Skillmon's comment) which lead me to try gettingnoexpandin the mix.
â ted
Aug 10 at 21:00
@ted No problem :) Good that OP's problem is solved ;)
â Phelype Oleinik
Aug 10 at 21:12
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I use a def as an alias like so:
defaliasfoo
later on I would like to check if alias is an alias for foo. (X is an alias of Y means the current definition of X is defXY).
See the MWE below for what I tried:
MWE:
documentclassarticle
%usepackagetrace
deffoobar
defbarbar
newcommandisFoo[1]
% expandafterifexpandafternoexpandaliasnoexpandfoo %false positives
% expandafterifxaliasfoo %this one compares text
% expandafterifxexpandafternoexpandaliasnoexpandfoo %false negatives
It is a textbackslash foo #1
else
Not a textbackslash foo #1
fi
begindocument
defaliasfoo
isFooab
defaliasbar
isFooxy
defbarbaz
isFooxy
enddocument
The idea behind the expandafter/noexpand code is to expand alias exactly once and then compare the two tokens without any further expansion to avoid comparing their return values.
conditionals
I use a def as an alias like so:
defaliasfoo
later on I would like to check if alias is an alias for foo. (X is an alias of Y means the current definition of X is defXY).
See the MWE below for what I tried:
MWE:
documentclassarticle
%usepackagetrace
deffoobar
defbarbar
newcommandisFoo[1]
% expandafterifexpandafternoexpandaliasnoexpandfoo %false positives
% expandafterifxaliasfoo %this one compares text
% expandafterifxexpandafternoexpandaliasnoexpandfoo %false negatives
It is a textbackslash foo #1
else
Not a textbackslash foo #1
fi
begindocument
defaliasfoo
isFooab
defaliasbar
isFooxy
defbarbaz
isFooxy
enddocument
The idea behind the expandafter/noexpand code is to expand alias exactly once and then compare the two tokens without any further expansion to avoid comparing their return values.
conditionals
conditionals
asked Aug 10 at 19:03
ted
1,58421440
1,58421440
I don't think I understand... You want to check ifaliasexpands tofoowithout comparing if the contents offoousing, for example,expandafterifxaliasfoo?
â Phelype Oleinik
Aug 10 at 19:35
1
@PhelypeOleinik this is not fail proofed:deffoobardefbarAdefbazAexpandafterifxfoobaz trueelse falsefiyields true, though it should yield false.
â Skillmon
Aug 10 at 20:17
@Skillmon Makes sense... If OP had replied I would've done something similar to your approach, but usingstringandifxinstead ofdetokenizeandpdf@strcmp, but then I forgot :P
â Phelype Oleinik
Aug 10 at 20:28
@PhelypeOleinik: Op disappeared from the keyboard for a while,... but in my simple case what you described did not work (see Skillmon's comment) which lead me to try gettingnoexpandin the mix.
â ted
Aug 10 at 21:00
@ted No problem :) Good that OP's problem is solved ;)
â Phelype Oleinik
Aug 10 at 21:12
add a comment |Â
I don't think I understand... You want to check ifaliasexpands tofoowithout comparing if the contents offoousing, for example,expandafterifxaliasfoo?
â Phelype Oleinik
Aug 10 at 19:35
1
@PhelypeOleinik this is not fail proofed:deffoobardefbarAdefbazAexpandafterifxfoobaz trueelse falsefiyields true, though it should yield false.
â Skillmon
Aug 10 at 20:17
@Skillmon Makes sense... If OP had replied I would've done something similar to your approach, but usingstringandifxinstead ofdetokenizeandpdf@strcmp, but then I forgot :P
â Phelype Oleinik
Aug 10 at 20:28
@PhelypeOleinik: Op disappeared from the keyboard for a while,... but in my simple case what you described did not work (see Skillmon's comment) which lead me to try gettingnoexpandin the mix.
â ted
Aug 10 at 21:00
@ted No problem :) Good that OP's problem is solved ;)
â Phelype Oleinik
Aug 10 at 21:12
I don't think I understand... You want to check if
alias expands to foo without comparing if the contents of foo using, for example, expandafterifxaliasfoo?â Phelype Oleinik
Aug 10 at 19:35
I don't think I understand... You want to check if
alias expands to foo without comparing if the contents of foo using, for example, expandafterifxaliasfoo?â Phelype Oleinik
Aug 10 at 19:35
1
1
@PhelypeOleinik this is not fail proofed:
deffoobardefbarAdefbazAexpandafterifxfoobaz trueelse falsefi yields true, though it should yield false.â Skillmon
Aug 10 at 20:17
@PhelypeOleinik this is not fail proofed:
deffoobardefbarAdefbazAexpandafterifxfoobaz trueelse falsefi yields true, though it should yield false.â Skillmon
Aug 10 at 20:17
@Skillmon Makes sense... If OP had replied I would've done something similar to your approach, but using
string and ifx instead of detokenize and pdf@strcmp, but then I forgot :Pâ Phelype Oleinik
Aug 10 at 20:28
@Skillmon Makes sense... If OP had replied I would've done something similar to your approach, but using
string and ifx instead of detokenize and pdf@strcmp, but then I forgot :Pâ Phelype Oleinik
Aug 10 at 20:28
@PhelypeOleinik: Op disappeared from the keyboard for a while,... but in my simple case what you described did not work (see Skillmon's comment) which lead me to try getting
noexpand in the mix.â ted
Aug 10 at 21:00
@PhelypeOleinik: Op disappeared from the keyboard for a while,... but in my simple case what you described did not work (see Skillmon's comment) which lead me to try getting
noexpand in the mix.â ted
Aug 10 at 21:00
@ted No problem :) Good that OP's problem is solved ;)
â Phelype Oleinik
Aug 10 at 21:12
@ted No problem :) Good that OP's problem is solved ;)
â Phelype Oleinik
Aug 10 at 21:12
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
You could also just set up another alias for foo and compare alias to that with ifx.
documentclassarticle
% deffoobar
% defbarbar
defknownaliasforfoofoo
newcommandisFoo[1]%
ifxaliasknownaliasforfoo
It is a textbackslash foo #1%
else
Not a textbackslash foo #1%
fi
begindocument
defaliasfoo
isFooab
defaliasbar
isFooxy
defbarbaz
isFooxy
enddocument

You can uncomment either or both of the commented lines and the output would remain the same.
add a comment |Â
up vote
5
down vote
With expl3:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewExpandableDocumentCommandifisaliasTFmmmm
% #1 = token to test
% #2 = macro
% #3 = true case
% #4 = false case
ted_ifisalias:nnTF #1 #2 #3 #4
prg_generate_conditional_variant:Nnn tl_if_empty:n f T,F,TF,p
prg_generate_conditional_variant:Nnn tl_if_single:n o T,F,TF,p
prg_new_conditional:Nnn ted_ifisalias:nn T,F,TF,p
token_if_macro:NTF #1
% the first argument is a macro
tl_if_empty:fTF token_get_arg_spec:N #1
% the macro has no arguments
tl_if_single:oTF #1
% the expansion is a single token
exp_last_unbraced:No token_if_eq_meaning:NNTF #1 #2
% the first level expansion of #1 is the same as #2
prg_return_true:
% false
prg_return_false:
% false
prg_return_false:
% false
prg_return_false:
% false
prg_return_false:
ExplSyntaxOff
newcommandfooWhatever
newcommandaliasfoo
newcommandnotaliasA[1]foo
newcommandnotaliasBfoofoo
begindocument
ifisaliasTFaliasfooAliasNot alias
ifisaliasTFfoofooAliasNot alias
ifisaliasTFnotaliasAfooAliasNot alias
ifisaliasTFnotaliasBfooAliasNot alias
enddocument

add a comment |Â
up vote
4
down vote
The following should do what you want.
documentclassarticle
usepackagepdftexcmds
defaliasfoo
makeatletter
newcommandcheckalias[2]
%
expandafterexpandafterexpandafter
checkalias@aexpandafterexpandafterexpandafter
expandafterdetokenizeexpandafter#1#2%
newcommandcheckalias@a[2]
%
expandafterifnum
expandafterpdf@strcmpexpandafterdetokenize#2#1=0
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
makeatother
begindocument
checkaliasaliasfoo
still an aliasno longer an alias
defaliasbar
checkaliasaliasfoo
still an aliasno longer an alias
enddocument
It doesn't check whether the first argument would grab any arguments though and is therefore not as safe as @egreg's check. I don't think it is worth it implementing the necessary checks without expl3 though.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You could also just set up another alias for foo and compare alias to that with ifx.
documentclassarticle
% deffoobar
% defbarbar
defknownaliasforfoofoo
newcommandisFoo[1]%
ifxaliasknownaliasforfoo
It is a textbackslash foo #1%
else
Not a textbackslash foo #1%
fi
begindocument
defaliasfoo
isFooab
defaliasbar
isFooxy
defbarbaz
isFooxy
enddocument

You can uncomment either or both of the commented lines and the output would remain the same.
add a comment |Â
up vote
3
down vote
accepted
You could also just set up another alias for foo and compare alias to that with ifx.
documentclassarticle
% deffoobar
% defbarbar
defknownaliasforfoofoo
newcommandisFoo[1]%
ifxaliasknownaliasforfoo
It is a textbackslash foo #1%
else
Not a textbackslash foo #1%
fi
begindocument
defaliasfoo
isFooab
defaliasbar
isFooxy
defbarbaz
isFooxy
enddocument

You can uncomment either or both of the commented lines and the output would remain the same.
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You could also just set up another alias for foo and compare alias to that with ifx.
documentclassarticle
% deffoobar
% defbarbar
defknownaliasforfoofoo
newcommandisFoo[1]%
ifxaliasknownaliasforfoo
It is a textbackslash foo #1%
else
Not a textbackslash foo #1%
fi
begindocument
defaliasfoo
isFooab
defaliasbar
isFooxy
defbarbaz
isFooxy
enddocument

You can uncomment either or both of the commented lines and the output would remain the same.
You could also just set up another alias for foo and compare alias to that with ifx.
documentclassarticle
% deffoobar
% defbarbar
defknownaliasforfoofoo
newcommandisFoo[1]%
ifxaliasknownaliasforfoo
It is a textbackslash foo #1%
else
Not a textbackslash foo #1%
fi
begindocument
defaliasfoo
isFooab
defaliasbar
isFooxy
defbarbaz
isFooxy
enddocument

You can uncomment either or both of the commented lines and the output would remain the same.
answered Aug 10 at 20:32
Circumscribe
2,033320
2,033320
add a comment |Â
add a comment |Â
up vote
5
down vote
With expl3:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewExpandableDocumentCommandifisaliasTFmmmm
% #1 = token to test
% #2 = macro
% #3 = true case
% #4 = false case
ted_ifisalias:nnTF #1 #2 #3 #4
prg_generate_conditional_variant:Nnn tl_if_empty:n f T,F,TF,p
prg_generate_conditional_variant:Nnn tl_if_single:n o T,F,TF,p
prg_new_conditional:Nnn ted_ifisalias:nn T,F,TF,p
token_if_macro:NTF #1
% the first argument is a macro
tl_if_empty:fTF token_get_arg_spec:N #1
% the macro has no arguments
tl_if_single:oTF #1
% the expansion is a single token
exp_last_unbraced:No token_if_eq_meaning:NNTF #1 #2
% the first level expansion of #1 is the same as #2
prg_return_true:
% false
prg_return_false:
% false
prg_return_false:
% false
prg_return_false:
% false
prg_return_false:
ExplSyntaxOff
newcommandfooWhatever
newcommandaliasfoo
newcommandnotaliasA[1]foo
newcommandnotaliasBfoofoo
begindocument
ifisaliasTFaliasfooAliasNot alias
ifisaliasTFfoofooAliasNot alias
ifisaliasTFnotaliasAfooAliasNot alias
ifisaliasTFnotaliasBfooAliasNot alias
enddocument

add a comment |Â
up vote
5
down vote
With expl3:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewExpandableDocumentCommandifisaliasTFmmmm
% #1 = token to test
% #2 = macro
% #3 = true case
% #4 = false case
ted_ifisalias:nnTF #1 #2 #3 #4
prg_generate_conditional_variant:Nnn tl_if_empty:n f T,F,TF,p
prg_generate_conditional_variant:Nnn tl_if_single:n o T,F,TF,p
prg_new_conditional:Nnn ted_ifisalias:nn T,F,TF,p
token_if_macro:NTF #1
% the first argument is a macro
tl_if_empty:fTF token_get_arg_spec:N #1
% the macro has no arguments
tl_if_single:oTF #1
% the expansion is a single token
exp_last_unbraced:No token_if_eq_meaning:NNTF #1 #2
% the first level expansion of #1 is the same as #2
prg_return_true:
% false
prg_return_false:
% false
prg_return_false:
% false
prg_return_false:
% false
prg_return_false:
ExplSyntaxOff
newcommandfooWhatever
newcommandaliasfoo
newcommandnotaliasA[1]foo
newcommandnotaliasBfoofoo
begindocument
ifisaliasTFaliasfooAliasNot alias
ifisaliasTFfoofooAliasNot alias
ifisaliasTFnotaliasAfooAliasNot alias
ifisaliasTFnotaliasBfooAliasNot alias
enddocument

add a comment |Â
up vote
5
down vote
up vote
5
down vote
With expl3:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewExpandableDocumentCommandifisaliasTFmmmm
% #1 = token to test
% #2 = macro
% #3 = true case
% #4 = false case
ted_ifisalias:nnTF #1 #2 #3 #4
prg_generate_conditional_variant:Nnn tl_if_empty:n f T,F,TF,p
prg_generate_conditional_variant:Nnn tl_if_single:n o T,F,TF,p
prg_new_conditional:Nnn ted_ifisalias:nn T,F,TF,p
token_if_macro:NTF #1
% the first argument is a macro
tl_if_empty:fTF token_get_arg_spec:N #1
% the macro has no arguments
tl_if_single:oTF #1
% the expansion is a single token
exp_last_unbraced:No token_if_eq_meaning:NNTF #1 #2
% the first level expansion of #1 is the same as #2
prg_return_true:
% false
prg_return_false:
% false
prg_return_false:
% false
prg_return_false:
% false
prg_return_false:
ExplSyntaxOff
newcommandfooWhatever
newcommandaliasfoo
newcommandnotaliasA[1]foo
newcommandnotaliasBfoofoo
begindocument
ifisaliasTFaliasfooAliasNot alias
ifisaliasTFfoofooAliasNot alias
ifisaliasTFnotaliasAfooAliasNot alias
ifisaliasTFnotaliasBfooAliasNot alias
enddocument

With expl3:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewExpandableDocumentCommandifisaliasTFmmmm
% #1 = token to test
% #2 = macro
% #3 = true case
% #4 = false case
ted_ifisalias:nnTF #1 #2 #3 #4
prg_generate_conditional_variant:Nnn tl_if_empty:n f T,F,TF,p
prg_generate_conditional_variant:Nnn tl_if_single:n o T,F,TF,p
prg_new_conditional:Nnn ted_ifisalias:nn T,F,TF,p
token_if_macro:NTF #1
% the first argument is a macro
tl_if_empty:fTF token_get_arg_spec:N #1
% the macro has no arguments
tl_if_single:oTF #1
% the expansion is a single token
exp_last_unbraced:No token_if_eq_meaning:NNTF #1 #2
% the first level expansion of #1 is the same as #2
prg_return_true:
% false
prg_return_false:
% false
prg_return_false:
% false
prg_return_false:
% false
prg_return_false:
ExplSyntaxOff
newcommandfooWhatever
newcommandaliasfoo
newcommandnotaliasA[1]foo
newcommandnotaliasBfoofoo
begindocument
ifisaliasTFaliasfooAliasNot alias
ifisaliasTFfoofooAliasNot alias
ifisaliasTFnotaliasAfooAliasNot alias
ifisaliasTFnotaliasBfooAliasNot alias
enddocument

answered Aug 10 at 20:20
egreg
682k8318143061
682k8318143061
add a comment |Â
add a comment |Â
up vote
4
down vote
The following should do what you want.
documentclassarticle
usepackagepdftexcmds
defaliasfoo
makeatletter
newcommandcheckalias[2]
%
expandafterexpandafterexpandafter
checkalias@aexpandafterexpandafterexpandafter
expandafterdetokenizeexpandafter#1#2%
newcommandcheckalias@a[2]
%
expandafterifnum
expandafterpdf@strcmpexpandafterdetokenize#2#1=0
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
makeatother
begindocument
checkaliasaliasfoo
still an aliasno longer an alias
defaliasbar
checkaliasaliasfoo
still an aliasno longer an alias
enddocument
It doesn't check whether the first argument would grab any arguments though and is therefore not as safe as @egreg's check. I don't think it is worth it implementing the necessary checks without expl3 though.
add a comment |Â
up vote
4
down vote
The following should do what you want.
documentclassarticle
usepackagepdftexcmds
defaliasfoo
makeatletter
newcommandcheckalias[2]
%
expandafterexpandafterexpandafter
checkalias@aexpandafterexpandafterexpandafter
expandafterdetokenizeexpandafter#1#2%
newcommandcheckalias@a[2]
%
expandafterifnum
expandafterpdf@strcmpexpandafterdetokenize#2#1=0
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
makeatother
begindocument
checkaliasaliasfoo
still an aliasno longer an alias
defaliasbar
checkaliasaliasfoo
still an aliasno longer an alias
enddocument
It doesn't check whether the first argument would grab any arguments though and is therefore not as safe as @egreg's check. I don't think it is worth it implementing the necessary checks without expl3 though.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
The following should do what you want.
documentclassarticle
usepackagepdftexcmds
defaliasfoo
makeatletter
newcommandcheckalias[2]
%
expandafterexpandafterexpandafter
checkalias@aexpandafterexpandafterexpandafter
expandafterdetokenizeexpandafter#1#2%
newcommandcheckalias@a[2]
%
expandafterifnum
expandafterpdf@strcmpexpandafterdetokenize#2#1=0
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
makeatother
begindocument
checkaliasaliasfoo
still an aliasno longer an alias
defaliasbar
checkaliasaliasfoo
still an aliasno longer an alias
enddocument
It doesn't check whether the first argument would grab any arguments though and is therefore not as safe as @egreg's check. I don't think it is worth it implementing the necessary checks without expl3 though.
The following should do what you want.
documentclassarticle
usepackagepdftexcmds
defaliasfoo
makeatletter
newcommandcheckalias[2]
%
expandafterexpandafterexpandafter
checkalias@aexpandafterexpandafterexpandafter
expandafterdetokenizeexpandafter#1#2%
newcommandcheckalias@a[2]
%
expandafterifnum
expandafterpdf@strcmpexpandafterdetokenize#2#1=0
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
makeatother
begindocument
checkaliasaliasfoo
still an aliasno longer an alias
defaliasbar
checkaliasaliasfoo
still an aliasno longer an alias
enddocument
It doesn't check whether the first argument would grab any arguments though and is therefore not as safe as @egreg's check. I don't think it is worth it implementing the necessary checks without expl3 though.
edited Aug 10 at 20:45
answered Aug 10 at 20:11
Skillmon
17.7k11535
17.7k11535
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f445544%2fidentifying-if-macro-is-an-alias%23new-answer', 'question_page');
);
Post as a guest
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
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
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
I don't think I understand... You want to check if
aliasexpands tofoowithout comparing if the contents offoousing, for example,expandafterifxaliasfoo?â Phelype Oleinik
Aug 10 at 19:35
1
@PhelypeOleinik this is not fail proofed:
deffoobardefbarAdefbazAexpandafterifxfoobaz trueelse falsefiyields true, though it should yield false.â Skillmon
Aug 10 at 20:17
@Skillmon Makes sense... If OP had replied I would've done something similar to your approach, but using
stringandifxinstead ofdetokenizeandpdf@strcmp, but then I forgot :Pâ Phelype Oleinik
Aug 10 at 20:28
@PhelypeOleinik: Op disappeared from the keyboard for a while,... but in my simple case what you described did not work (see Skillmon's comment) which lead me to try getting
noexpandin the mix.â ted
Aug 10 at 21:00
@ted No problem :) Good that OP's problem is solved ;)
â Phelype Oleinik
Aug 10 at 21:12