How to write a macro that is braces sensitive? The 2019 Stack Overflow Developer Survey Results Are InContext-sensitive newline macroCreate my own NSDWhy isn't everything expandable?How to write a parameter-driven macro?How to write a TeX macro that accepts a number or a count register as argument?Texshop macro for bracesWrite an unpar macroLaTeX for plain TeX users?LaTeX Utility/Template for Generic Reusable Codecontext-sensitive macro: look behind?

How come people say “Would of”?

What is the most effective way of iterating a std::vector and why?

Falsification in Math vs Science

Should I use my personal e-mail address, or my workplace one, when registering to external websites for work purposes?

How to type this arrow in math mode?

Why do UK politicians seemingly ignore opinion polls on Brexit?

What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?

Can one be advised by a professor who is very far away?

Button changing it's text & action. Good or terrible?

Delete all lines which don't have n characters before delimiter

Protecting Dualbooting Windows from dangerous code (like rm -rf)

When should I buy a clipper card after flying to OAK?

Pokemon Turn Based battle (Python)

Does the shape of a die affect the probability of a number being rolled?

How are circuits which use complex ICs normally simulated?

What to do when moving next to a bird sanctuary with a loosely-domesticated cat?

Why did Acorn's A3000 have red function keys?

Time travel alters history but people keep saying nothing's changed

Are there any other methods to apply to solving simultaneous equations?

Why isn't airport relocation done gradually?

Does coating your armor in silver add any effects?

Right tool to dig six foot holes?

Is bread bad for ducks?

Is a "Democratic" Oligarchy-Style System Possible?



How to write a macro that is braces sensitive?



The 2019 Stack Overflow Developer Survey Results Are InContext-sensitive newline macroCreate my own NSDWhy isn't everything expandable?How to write a parameter-driven macro?How to write a TeX macro that accepts a number or a count register as argument?Texshop macro for bracesWrite an unpar macroLaTeX for plain TeX users?LaTeX Utility/Template for Generic Reusable Codecontext-sensitive macro: look behind?










7















In the xparse package, there is the g type of argument which captures things inside a pair of TeX group tokens. This makes it possible to define commands foo that behaves differently for fooa and foo a. I am interested in whether such type of macro is possible in plain TeX (I guess yes) and if it is possible, how can it be implemented. I am new to plain TeX and I appreciate detailed explanation of the workflow of such a macro. I would also be happy to learn about other possibilities such as in e-TeX instead of plain TeX.



Edit



Only after reading the answers did I realize that it was impossible at first look because all I was thinking was deffoo#1... (and indeed it is impossible if this form is used).










share|improve this question









New contributor




Weijun Zhou is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • It's possible, but it's bad syntax. Under normal TeX conventions, foo a and fooa should be considered equivalent (when the argument consists of a single token as in this case).

    – egreg
    Apr 6 at 21:18












  • Thank you for your quick reply. I know it's bad syntax otherwise xparse would not declare it as obsolete, but it is (extensively) used in e.g. physics package. I am just not sure about whether it can be done in plain, or it requires some features of the engine.

    – Weijun Zhou
    Apr 6 at 21:22







  • 1





    It's indeed used in physics. My opinion about the package is that it has good ideas, but I can't recommend its usage. The weird syntax is just one among the several reasons for not recommending it.

    – egreg
    Apr 6 at 21:23







  • 1





    Due to the weird syntax I end up adding relax here and there ... but I guess I will still use it.

    – Weijun Zhou
    Apr 6 at 21:31















7















In the xparse package, there is the g type of argument which captures things inside a pair of TeX group tokens. This makes it possible to define commands foo that behaves differently for fooa and foo a. I am interested in whether such type of macro is possible in plain TeX (I guess yes) and if it is possible, how can it be implemented. I am new to plain TeX and I appreciate detailed explanation of the workflow of such a macro. I would also be happy to learn about other possibilities such as in e-TeX instead of plain TeX.



Edit



Only after reading the answers did I realize that it was impossible at first look because all I was thinking was deffoo#1... (and indeed it is impossible if this form is used).










share|improve this question









New contributor




Weijun Zhou is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • It's possible, but it's bad syntax. Under normal TeX conventions, foo a and fooa should be considered equivalent (when the argument consists of a single token as in this case).

    – egreg
    Apr 6 at 21:18












  • Thank you for your quick reply. I know it's bad syntax otherwise xparse would not declare it as obsolete, but it is (extensively) used in e.g. physics package. I am just not sure about whether it can be done in plain, or it requires some features of the engine.

    – Weijun Zhou
    Apr 6 at 21:22







  • 1





    It's indeed used in physics. My opinion about the package is that it has good ideas, but I can't recommend its usage. The weird syntax is just one among the several reasons for not recommending it.

    – egreg
    Apr 6 at 21:23







  • 1





    Due to the weird syntax I end up adding relax here and there ... but I guess I will still use it.

    – Weijun Zhou
    Apr 6 at 21:31













7












7








7








In the xparse package, there is the g type of argument which captures things inside a pair of TeX group tokens. This makes it possible to define commands foo that behaves differently for fooa and foo a. I am interested in whether such type of macro is possible in plain TeX (I guess yes) and if it is possible, how can it be implemented. I am new to plain TeX and I appreciate detailed explanation of the workflow of such a macro. I would also be happy to learn about other possibilities such as in e-TeX instead of plain TeX.



Edit



Only after reading the answers did I realize that it was impossible at first look because all I was thinking was deffoo#1... (and indeed it is impossible if this form is used).










share|improve this question









New contributor




Weijun Zhou is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












In the xparse package, there is the g type of argument which captures things inside a pair of TeX group tokens. This makes it possible to define commands foo that behaves differently for fooa and foo a. I am interested in whether such type of macro is possible in plain TeX (I guess yes) and if it is possible, how can it be implemented. I am new to plain TeX and I appreciate detailed explanation of the workflow of such a macro. I would also be happy to learn about other possibilities such as in e-TeX instead of plain TeX.



Edit



Only after reading the answers did I realize that it was impossible at first look because all I was thinking was deffoo#1... (and indeed it is impossible if this form is used).







macros plain-tex braces






share|improve this question









New contributor




Weijun Zhou is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Weijun Zhou is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Apr 7 at 0:20







Weijun Zhou













New contributor




Weijun Zhou is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Apr 6 at 21:14









Weijun ZhouWeijun Zhou

1405




1405




New contributor




Weijun Zhou is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Weijun Zhou is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Weijun Zhou is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • It's possible, but it's bad syntax. Under normal TeX conventions, foo a and fooa should be considered equivalent (when the argument consists of a single token as in this case).

    – egreg
    Apr 6 at 21:18












  • Thank you for your quick reply. I know it's bad syntax otherwise xparse would not declare it as obsolete, but it is (extensively) used in e.g. physics package. I am just not sure about whether it can be done in plain, or it requires some features of the engine.

    – Weijun Zhou
    Apr 6 at 21:22







  • 1





    It's indeed used in physics. My opinion about the package is that it has good ideas, but I can't recommend its usage. The weird syntax is just one among the several reasons for not recommending it.

    – egreg
    Apr 6 at 21:23







  • 1





    Due to the weird syntax I end up adding relax here and there ... but I guess I will still use it.

    – Weijun Zhou
    Apr 6 at 21:31

















  • It's possible, but it's bad syntax. Under normal TeX conventions, foo a and fooa should be considered equivalent (when the argument consists of a single token as in this case).

    – egreg
    Apr 6 at 21:18












  • Thank you for your quick reply. I know it's bad syntax otherwise xparse would not declare it as obsolete, but it is (extensively) used in e.g. physics package. I am just not sure about whether it can be done in plain, or it requires some features of the engine.

    – Weijun Zhou
    Apr 6 at 21:22







  • 1





    It's indeed used in physics. My opinion about the package is that it has good ideas, but I can't recommend its usage. The weird syntax is just one among the several reasons for not recommending it.

    – egreg
    Apr 6 at 21:23







  • 1





    Due to the weird syntax I end up adding relax here and there ... but I guess I will still use it.

    – Weijun Zhou
    Apr 6 at 21:31
















It's possible, but it's bad syntax. Under normal TeX conventions, foo a and fooa should be considered equivalent (when the argument consists of a single token as in this case).

– egreg
Apr 6 at 21:18






It's possible, but it's bad syntax. Under normal TeX conventions, foo a and fooa should be considered equivalent (when the argument consists of a single token as in this case).

– egreg
Apr 6 at 21:18














Thank you for your quick reply. I know it's bad syntax otherwise xparse would not declare it as obsolete, but it is (extensively) used in e.g. physics package. I am just not sure about whether it can be done in plain, or it requires some features of the engine.

– Weijun Zhou
Apr 6 at 21:22






Thank you for your quick reply. I know it's bad syntax otherwise xparse would not declare it as obsolete, but it is (extensively) used in e.g. physics package. I am just not sure about whether it can be done in plain, or it requires some features of the engine.

– Weijun Zhou
Apr 6 at 21:22





1




1





It's indeed used in physics. My opinion about the package is that it has good ideas, but I can't recommend its usage. The weird syntax is just one among the several reasons for not recommending it.

– egreg
Apr 6 at 21:23






It's indeed used in physics. My opinion about the package is that it has good ideas, but I can't recommend its usage. The weird syntax is just one among the several reasons for not recommending it.

– egreg
Apr 6 at 21:23





1




1





Due to the weird syntax I end up adding relax here and there ... but I guess I will still use it.

– Weijun Zhou
Apr 6 at 21:31





Due to the weird syntax I end up adding relax here and there ... but I guess I will still use it.

– Weijun Zhou
Apr 6 at 21:31










2 Answers
2






active

oldest

votes


















6














You can use futurelet



letleftbracechar=
deffoo%
begingroup
futureletfootempinnerfoo
%
definnerfoo%
expandafterendgroup
ifxfootempleftbracechar
expandafterfooatleftbrace
else
expandafterfooatnoleftbrace
fi
%
deffooatleftbrace#1Argument in braces is: bf #1
deffooatnoleftbrace#1Argument without braces is: bf #1


foo a

fooa

bye


enter image description here



, but be aware that this can be confused by implicit characters, i.e., by things like foobgroup huh?...



Besides this, the check is only about tokens (be they explicit or implicit character tokens) where the category code is 1 (begin group) and the character code equals the character code of the curly-opening-brace-character. The check does not work out with character tokens where the category code is 1 (begin group) but the character code is different.



But you can implement a full expandable check which tells you whether the first token inside a macro argument is an explicit character token of category code 1 (begin group) no matter what its character code might be:



%%-----------------------------------------------------------------------------
%% Check whether argument's first token is an explicit catcode-1-character
%%.............................................................................
%% UDCheckWhetherBrace<Argument which is to be checked>%
%% <Tokens to be delivered in case that argument
%% which is to be checked has leading
%% catcode-1-token>%
%% <Tokens to be delivered in case that argument
%% which is to be checked has no leading
%% catcode-1-token>%
longdeffirstoftwo#1#2#1%
longdefsecondoftwo#1#2#2%
longdefUDCheckWhetherBrace#1%
romannumeral0expandaftersecondoftwoexpandafterexpandafter%
string#1.expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwoexpandafterexpandafterfirstoftwo secondoftwo%
%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% Now let's have some fun: Give [ the same functionality as :
catcode`[=thecatcode`

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBrace[Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBrace[Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% Now let's see that the test on explicit characters is not fooled by implicit characters:
letbgroup=

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBracebgroupegroup Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBracebgroup Testegroup%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% The test is also not fooled by implicit active characters:
catcode`X=13
let X=

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceXegroup Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceX Testegroup%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%


bye


enter image description here




In order to see how UDCheckWhetherBrace works, let's write it with different line-breaking and different indentation:



The gist is: Have the argument's first token hit by string and use TeX's catching of brace-balanced arguments for finding out whether an opening brace or something else was neutralized/was turned into a catcode-12-sequence:



longdefUDCheckWhetherBrace#1%
romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string#1.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
%


For example



UDCheckWhetherBracetestbraceno brace%


yields:



romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string and thus stringifying "t" from the phrase "test":



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo%← This is the interesting secondoftwo.
%← This is the interesting secondoftwo's first argument's opening brace.
%
&langle;character t, due to string now of category code 12 (other)&rangle;est.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the interesting secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out expandafter and firstoftwo and thus delivering the space that was inside braces:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter&langle;space token&rangle;secondoftwo%
braceno brace%


yields carrying out expandafter and secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
&langle;space token&rangle;no brace


Now romannumeral finds the space token and discards it and aborts gathering digits. As by now it has only found the digit "0" which does not form a positive number, it does silently not deliver any token:



%romannumeral expansion done:
no brace



For example



UDCheckWhetherBracetestbraceno brace%


yields:



romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
stringtest.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
stringtest.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string and thus stringifying the left curly brace/the openening curly brace from the phrase "test":



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo%← This is the interesting secondoftwo.
%← This is the interesting secondoftwo's first argument's opening brace.
%
&langle;character , due to string now of category code 12 (other)&rangle;test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the interesting secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
firstoftwo%
secondoftwo&langle;character , due to string now of category code 12 (other)&rangle;expandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out firstoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo&langle;character , due to string now of category code 12 (other)&rangle;expandafterexpandafterfirstoftwo %
firstoftwo
braceno brace%


yields carrying out secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterexpandafterfirstoftwo %
firstoftwo
braceno brace%


yields carrying out expandafter and firstoftwo and thus delivering the space that was inside braces:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter&langle;space token&rangle;firstoftwo%
braceno brace%


yields carrying out expandafter and firstoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
&langle;space token&rangle;brace


Now romannumeral finds the space token and discards it and aborts gathering digits. As by now it has only found the digit "0" which does not form a positive number, it does silently not deliver any token:



%romannumeral expansion done:
brace





share|improve this answer

























  • This is much more extensive. I will spend some time understanding and learning from it. I think the idea of checking catcode instead of char code is very interesting and useful. It would be great if some more explanation can be added for the latter case.

    – Weijun Zhou
    Apr 6 at 22:08












  • Thank you very much for your effort explaining how it works.

    – Weijun Zhou
    Apr 6 at 23:40


















8














Fundamentally you just need to use futurelet as you do for any other look ahead



deffoofutureletfootokenfooaux
deffooaux%
ifxfootokenbgroup
% Brace group
else
% Something else
fi



The only reason this 'looks different' to other peek ahead situations is that you can't use an explicit {, but rather the implicit token bgroup.






share|improve this answer

























  • That's clear enough and much simpler than I originally imagined.

    – Weijun Zhou
    Apr 6 at 21:27











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/3.0/"u003ecc by-sa 3.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
);



);






Weijun Zhou is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f483588%2fhow-to-write-a-macro-that-is-braces-sensitive%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









6














You can use futurelet



letleftbracechar=
deffoo%
begingroup
futureletfootempinnerfoo
%
definnerfoo%
expandafterendgroup
ifxfootempleftbracechar
expandafterfooatleftbrace
else
expandafterfooatnoleftbrace
fi
%
deffooatleftbrace#1Argument in braces is: bf #1
deffooatnoleftbrace#1Argument without braces is: bf #1


foo a

fooa

bye


enter image description here



, but be aware that this can be confused by implicit characters, i.e., by things like foobgroup huh?...



Besides this, the check is only about tokens (be they explicit or implicit character tokens) where the category code is 1 (begin group) and the character code equals the character code of the curly-opening-brace-character. The check does not work out with character tokens where the category code is 1 (begin group) but the character code is different.



But you can implement a full expandable check which tells you whether the first token inside a macro argument is an explicit character token of category code 1 (begin group) no matter what its character code might be:



%%-----------------------------------------------------------------------------
%% Check whether argument's first token is an explicit catcode-1-character
%%.............................................................................
%% UDCheckWhetherBrace<Argument which is to be checked>%
%% <Tokens to be delivered in case that argument
%% which is to be checked has leading
%% catcode-1-token>%
%% <Tokens to be delivered in case that argument
%% which is to be checked has no leading
%% catcode-1-token>%
longdeffirstoftwo#1#2#1%
longdefsecondoftwo#1#2#2%
longdefUDCheckWhetherBrace#1%
romannumeral0expandaftersecondoftwoexpandafterexpandafter%
string#1.expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwoexpandafterexpandafterfirstoftwo secondoftwo%
%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% Now let's have some fun: Give [ the same functionality as :
catcode`[=thecatcode`

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBrace[Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBrace[Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% Now let's see that the test on explicit characters is not fooled by implicit characters:
letbgroup=

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBracebgroupegroup Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBracebgroup Testegroup%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% The test is also not fooled by implicit active characters:
catcode`X=13
let X=

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceXegroup Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceX Testegroup%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%


bye


enter image description here




In order to see how UDCheckWhetherBrace works, let's write it with different line-breaking and different indentation:



The gist is: Have the argument's first token hit by string and use TeX's catching of brace-balanced arguments for finding out whether an opening brace or something else was neutralized/was turned into a catcode-12-sequence:



longdefUDCheckWhetherBrace#1%
romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string#1.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
%


For example



UDCheckWhetherBracetestbraceno brace%


yields:



romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string and thus stringifying "t" from the phrase "test":



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo%← This is the interesting secondoftwo.
%← This is the interesting secondoftwo's first argument's opening brace.
%
&langle;character t, due to string now of category code 12 (other)&rangle;est.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the interesting secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out expandafter and firstoftwo and thus delivering the space that was inside braces:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter&langle;space token&rangle;secondoftwo%
braceno brace%


yields carrying out expandafter and secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
&langle;space token&rangle;no brace


Now romannumeral finds the space token and discards it and aborts gathering digits. As by now it has only found the digit "0" which does not form a positive number, it does silently not deliver any token:



%romannumeral expansion done:
no brace



For example



UDCheckWhetherBracetestbraceno brace%


yields:



romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
stringtest.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
stringtest.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string and thus stringifying the left curly brace/the openening curly brace from the phrase "test":



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo%← This is the interesting secondoftwo.
%← This is the interesting secondoftwo's first argument's opening brace.
%
&langle;character , due to string now of category code 12 (other)&rangle;test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the interesting secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
firstoftwo%
secondoftwo&langle;character , due to string now of category code 12 (other)&rangle;expandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out firstoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo&langle;character , due to string now of category code 12 (other)&rangle;expandafterexpandafterfirstoftwo %
firstoftwo
braceno brace%


yields carrying out secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterexpandafterfirstoftwo %
firstoftwo
braceno brace%


yields carrying out expandafter and firstoftwo and thus delivering the space that was inside braces:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter&langle;space token&rangle;firstoftwo%
braceno brace%


yields carrying out expandafter and firstoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
&langle;space token&rangle;brace


Now romannumeral finds the space token and discards it and aborts gathering digits. As by now it has only found the digit "0" which does not form a positive number, it does silently not deliver any token:



%romannumeral expansion done:
brace





share|improve this answer

























  • This is much more extensive. I will spend some time understanding and learning from it. I think the idea of checking catcode instead of char code is very interesting and useful. It would be great if some more explanation can be added for the latter case.

    – Weijun Zhou
    Apr 6 at 22:08












  • Thank you very much for your effort explaining how it works.

    – Weijun Zhou
    Apr 6 at 23:40















6














You can use futurelet



letleftbracechar=
deffoo%
begingroup
futureletfootempinnerfoo
%
definnerfoo%
expandafterendgroup
ifxfootempleftbracechar
expandafterfooatleftbrace
else
expandafterfooatnoleftbrace
fi
%
deffooatleftbrace#1Argument in braces is: bf #1
deffooatnoleftbrace#1Argument without braces is: bf #1


foo a

fooa

bye


enter image description here



, but be aware that this can be confused by implicit characters, i.e., by things like foobgroup huh?...



Besides this, the check is only about tokens (be they explicit or implicit character tokens) where the category code is 1 (begin group) and the character code equals the character code of the curly-opening-brace-character. The check does not work out with character tokens where the category code is 1 (begin group) but the character code is different.



But you can implement a full expandable check which tells you whether the first token inside a macro argument is an explicit character token of category code 1 (begin group) no matter what its character code might be:



%%-----------------------------------------------------------------------------
%% Check whether argument's first token is an explicit catcode-1-character
%%.............................................................................
%% UDCheckWhetherBrace<Argument which is to be checked>%
%% <Tokens to be delivered in case that argument
%% which is to be checked has leading
%% catcode-1-token>%
%% <Tokens to be delivered in case that argument
%% which is to be checked has no leading
%% catcode-1-token>%
longdeffirstoftwo#1#2#1%
longdefsecondoftwo#1#2#2%
longdefUDCheckWhetherBrace#1%
romannumeral0expandaftersecondoftwoexpandafterexpandafter%
string#1.expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwoexpandafterexpandafterfirstoftwo secondoftwo%
%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% Now let's have some fun: Give [ the same functionality as :
catcode`[=thecatcode`

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBrace[Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBrace[Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% Now let's see that the test on explicit characters is not fooled by implicit characters:
letbgroup=

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBracebgroupegroup Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBracebgroup Testegroup%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% The test is also not fooled by implicit active characters:
catcode`X=13
let X=

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceXegroup Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceX Testegroup%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%


bye


enter image description here




In order to see how UDCheckWhetherBrace works, let's write it with different line-breaking and different indentation:



The gist is: Have the argument's first token hit by string and use TeX's catching of brace-balanced arguments for finding out whether an opening brace or something else was neutralized/was turned into a catcode-12-sequence:



longdefUDCheckWhetherBrace#1%
romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string#1.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
%


For example



UDCheckWhetherBracetestbraceno brace%


yields:



romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string and thus stringifying "t" from the phrase "test":



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo%← This is the interesting secondoftwo.
%← This is the interesting secondoftwo's first argument's opening brace.
%
&langle;character t, due to string now of category code 12 (other)&rangle;est.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the interesting secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out expandafter and firstoftwo and thus delivering the space that was inside braces:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter&langle;space token&rangle;secondoftwo%
braceno brace%


yields carrying out expandafter and secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
&langle;space token&rangle;no brace


Now romannumeral finds the space token and discards it and aborts gathering digits. As by now it has only found the digit "0" which does not form a positive number, it does silently not deliver any token:



%romannumeral expansion done:
no brace



For example



UDCheckWhetherBracetestbraceno brace%


yields:



romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
stringtest.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
stringtest.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string and thus stringifying the left curly brace/the openening curly brace from the phrase "test":



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo%← This is the interesting secondoftwo.
%← This is the interesting secondoftwo's first argument's opening brace.
%
&langle;character , due to string now of category code 12 (other)&rangle;test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the interesting secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
firstoftwo%
secondoftwo&langle;character , due to string now of category code 12 (other)&rangle;expandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out firstoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo&langle;character , due to string now of category code 12 (other)&rangle;expandafterexpandafterfirstoftwo %
firstoftwo
braceno brace%


yields carrying out secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterexpandafterfirstoftwo %
firstoftwo
braceno brace%


yields carrying out expandafter and firstoftwo and thus delivering the space that was inside braces:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter&langle;space token&rangle;firstoftwo%
braceno brace%


yields carrying out expandafter and firstoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
&langle;space token&rangle;brace


Now romannumeral finds the space token and discards it and aborts gathering digits. As by now it has only found the digit "0" which does not form a positive number, it does silently not deliver any token:



%romannumeral expansion done:
brace





share|improve this answer

























  • This is much more extensive. I will spend some time understanding and learning from it. I think the idea of checking catcode instead of char code is very interesting and useful. It would be great if some more explanation can be added for the latter case.

    – Weijun Zhou
    Apr 6 at 22:08












  • Thank you very much for your effort explaining how it works.

    – Weijun Zhou
    Apr 6 at 23:40













6












6








6







You can use futurelet



letleftbracechar=
deffoo%
begingroup
futureletfootempinnerfoo
%
definnerfoo%
expandafterendgroup
ifxfootempleftbracechar
expandafterfooatleftbrace
else
expandafterfooatnoleftbrace
fi
%
deffooatleftbrace#1Argument in braces is: bf #1
deffooatnoleftbrace#1Argument without braces is: bf #1


foo a

fooa

bye


enter image description here



, but be aware that this can be confused by implicit characters, i.e., by things like foobgroup huh?...



Besides this, the check is only about tokens (be they explicit or implicit character tokens) where the category code is 1 (begin group) and the character code equals the character code of the curly-opening-brace-character. The check does not work out with character tokens where the category code is 1 (begin group) but the character code is different.



But you can implement a full expandable check which tells you whether the first token inside a macro argument is an explicit character token of category code 1 (begin group) no matter what its character code might be:



%%-----------------------------------------------------------------------------
%% Check whether argument's first token is an explicit catcode-1-character
%%.............................................................................
%% UDCheckWhetherBrace<Argument which is to be checked>%
%% <Tokens to be delivered in case that argument
%% which is to be checked has leading
%% catcode-1-token>%
%% <Tokens to be delivered in case that argument
%% which is to be checked has no leading
%% catcode-1-token>%
longdeffirstoftwo#1#2#1%
longdefsecondoftwo#1#2#2%
longdefUDCheckWhetherBrace#1%
romannumeral0expandaftersecondoftwoexpandafterexpandafter%
string#1.expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwoexpandafterexpandafterfirstoftwo secondoftwo%
%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% Now let's have some fun: Give [ the same functionality as :
catcode`[=thecatcode`

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBrace[Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBrace[Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% Now let's see that the test on explicit characters is not fooled by implicit characters:
letbgroup=

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBracebgroupegroup Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBracebgroup Testegroup%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% The test is also not fooled by implicit active characters:
catcode`X=13
let X=

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceXegroup Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceX Testegroup%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%


bye


enter image description here




In order to see how UDCheckWhetherBrace works, let's write it with different line-breaking and different indentation:



The gist is: Have the argument's first token hit by string and use TeX's catching of brace-balanced arguments for finding out whether an opening brace or something else was neutralized/was turned into a catcode-12-sequence:



longdefUDCheckWhetherBrace#1%
romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string#1.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
%


For example



UDCheckWhetherBracetestbraceno brace%


yields:



romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string and thus stringifying "t" from the phrase "test":



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo%← This is the interesting secondoftwo.
%← This is the interesting secondoftwo's first argument's opening brace.
%
&langle;character t, due to string now of category code 12 (other)&rangle;est.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the interesting secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out expandafter and firstoftwo and thus delivering the space that was inside braces:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter&langle;space token&rangle;secondoftwo%
braceno brace%


yields carrying out expandafter and secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
&langle;space token&rangle;no brace


Now romannumeral finds the space token and discards it and aborts gathering digits. As by now it has only found the digit "0" which does not form a positive number, it does silently not deliver any token:



%romannumeral expansion done:
no brace



For example



UDCheckWhetherBracetestbraceno brace%


yields:



romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
stringtest.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
stringtest.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string and thus stringifying the left curly brace/the openening curly brace from the phrase "test":



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo%← This is the interesting secondoftwo.
%← This is the interesting secondoftwo's first argument's opening brace.
%
&langle;character , due to string now of category code 12 (other)&rangle;test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the interesting secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
firstoftwo%
secondoftwo&langle;character , due to string now of category code 12 (other)&rangle;expandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out firstoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo&langle;character , due to string now of category code 12 (other)&rangle;expandafterexpandafterfirstoftwo %
firstoftwo
braceno brace%


yields carrying out secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterexpandafterfirstoftwo %
firstoftwo
braceno brace%


yields carrying out expandafter and firstoftwo and thus delivering the space that was inside braces:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter&langle;space token&rangle;firstoftwo%
braceno brace%


yields carrying out expandafter and firstoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
&langle;space token&rangle;brace


Now romannumeral finds the space token and discards it and aborts gathering digits. As by now it has only found the digit "0" which does not form a positive number, it does silently not deliver any token:



%romannumeral expansion done:
brace





share|improve this answer















You can use futurelet



letleftbracechar=
deffoo%
begingroup
futureletfootempinnerfoo
%
definnerfoo%
expandafterendgroup
ifxfootempleftbracechar
expandafterfooatleftbrace
else
expandafterfooatnoleftbrace
fi
%
deffooatleftbrace#1Argument in braces is: bf #1
deffooatnoleftbrace#1Argument without braces is: bf #1


foo a

fooa

bye


enter image description here



, but be aware that this can be confused by implicit characters, i.e., by things like foobgroup huh?...



Besides this, the check is only about tokens (be they explicit or implicit character tokens) where the category code is 1 (begin group) and the character code equals the character code of the curly-opening-brace-character. The check does not work out with character tokens where the category code is 1 (begin group) but the character code is different.



But you can implement a full expandable check which tells you whether the first token inside a macro argument is an explicit character token of category code 1 (begin group) no matter what its character code might be:



%%-----------------------------------------------------------------------------
%% Check whether argument's first token is an explicit catcode-1-character
%%.............................................................................
%% UDCheckWhetherBrace<Argument which is to be checked>%
%% <Tokens to be delivered in case that argument
%% which is to be checked has leading
%% catcode-1-token>%
%% <Tokens to be delivered in case that argument
%% which is to be checked has no leading
%% catcode-1-token>%
longdeffirstoftwo#1#2#1%
longdefsecondoftwo#1#2#2%
longdefUDCheckWhetherBrace#1%
romannumeral0expandaftersecondoftwoexpandafterexpandafter%
string#1.expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwoexpandafterexpandafterfirstoftwo secondoftwo%
%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% Now let's have some fun: Give [ the same functionality as :
catcode`[=thecatcode`

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBrace[Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBrace[Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% Now let's see that the test on explicit characters is not fooled by implicit characters:
letbgroup=

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBracebgroupegroup Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBracebgroup Testegroup%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

leavevmodehrulefillnull

% The test is also not fooled by implicit active characters:
catcode`X=13
let X=

UDCheckWhetherBraceTest%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceXegroup Test%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%

UDCheckWhetherBraceX Testegroup%
The first token of the arg is an explicit catcode 1 char.%
The first token of the arg is not an explicit catcode 1 char.%


bye


enter image description here




In order to see how UDCheckWhetherBrace works, let's write it with different line-breaking and different indentation:



The gist is: Have the argument's first token hit by string and use TeX's catching of brace-balanced arguments for finding out whether an opening brace or something else was neutralized/was turned into a catcode-12-sequence:



longdefUDCheckWhetherBrace#1%
romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string#1.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
%


For example



UDCheckWhetherBracetestbraceno brace%


yields:



romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
string test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string and thus stringifying "t" from the phrase "test":



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo%← This is the interesting secondoftwo.
%← This is the interesting secondoftwo's first argument's opening brace.
%
&langle;character t, due to string now of category code 12 (other)&rangle;est.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the interesting secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out expandafter and firstoftwo and thus delivering the space that was inside braces:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter&langle;space token&rangle;secondoftwo%
braceno brace%


yields carrying out expandafter and secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
&langle;space token&rangle;no brace


Now romannumeral finds the space token and discards it and aborts gathering digits. As by now it has only found the digit "0" which does not form a positive number, it does silently not deliver any token:



%romannumeral expansion done:
no brace



For example



UDCheckWhetherBracetestbraceno brace%


yields:



romannumeral0%
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
stringtest.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter
secondoftwo%← This is the interesting secondoftwo.
expandafter%← This is the interesting secondoftwo's first argument's opening brace.
expandafter%
stringtest.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string and thus stringifying the left curly brace/the openening curly brace from the phrase "test":



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo%← This is the interesting secondoftwo.
%← This is the interesting secondoftwo's first argument's opening brace.
%
&langle;character , due to string now of category code 12 (other)&rangle;test.%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% , which itself must be brace-balanced, had an opening-brace as first token.
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the interesting secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterfirstoftwoexpandafterexpandafter
secondoftwostringexpandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out the expandafter-chain and string:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
firstoftwo%
secondoftwo&langle;character , due to string now of category code 12 (other)&rangle;expandafterexpandafterfirstoftwo %
firstoftwo%← This is the interesting secondoftwo's first argument's closing brace in case the argument
% did not have an opening-brace as first token
expandafterexpandafterfirstoftwo secondoftwo%
braceno brace%


yields carrying out firstoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
secondoftwo&langle;character , due to string now of category code 12 (other)&rangle;expandafterexpandafterfirstoftwo %
firstoftwo
braceno brace%


yields carrying out secondoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafterexpandafterfirstoftwo %
firstoftwo
braceno brace%


yields carrying out expandafter and firstoftwo and thus delivering the space that was inside braces:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
expandafter&langle;space token&rangle;firstoftwo%
braceno brace%


yields carrying out expandafter and firstoftwo:



%romannumeral expansion in progress as by now romannumeral only found the digit 0 and searches for more digits:
&langle;space token&rangle;brace


Now romannumeral finds the space token and discards it and aborts gathering digits. As by now it has only found the digit "0" which does not form a positive number, it does silently not deliver any token:



%romannumeral expansion done:
brace






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered Apr 6 at 22:02









Ulrich DiezUlrich Diez

5,625620




5,625620












  • This is much more extensive. I will spend some time understanding and learning from it. I think the idea of checking catcode instead of char code is very interesting and useful. It would be great if some more explanation can be added for the latter case.

    – Weijun Zhou
    Apr 6 at 22:08












  • Thank you very much for your effort explaining how it works.

    – Weijun Zhou
    Apr 6 at 23:40

















  • This is much more extensive. I will spend some time understanding and learning from it. I think the idea of checking catcode instead of char code is very interesting and useful. It would be great if some more explanation can be added for the latter case.

    – Weijun Zhou
    Apr 6 at 22:08












  • Thank you very much for your effort explaining how it works.

    – Weijun Zhou
    Apr 6 at 23:40
















This is much more extensive. I will spend some time understanding and learning from it. I think the idea of checking catcode instead of char code is very interesting and useful. It would be great if some more explanation can be added for the latter case.

– Weijun Zhou
Apr 6 at 22:08






This is much more extensive. I will spend some time understanding and learning from it. I think the idea of checking catcode instead of char code is very interesting and useful. It would be great if some more explanation can be added for the latter case.

– Weijun Zhou
Apr 6 at 22:08














Thank you very much for your effort explaining how it works.

– Weijun Zhou
Apr 6 at 23:40





Thank you very much for your effort explaining how it works.

– Weijun Zhou
Apr 6 at 23:40











8














Fundamentally you just need to use futurelet as you do for any other look ahead



deffoofutureletfootokenfooaux
deffooaux%
ifxfootokenbgroup
% Brace group
else
% Something else
fi



The only reason this 'looks different' to other peek ahead situations is that you can't use an explicit {, but rather the implicit token bgroup.






share|improve this answer

























  • That's clear enough and much simpler than I originally imagined.

    – Weijun Zhou
    Apr 6 at 21:27















8














Fundamentally you just need to use futurelet as you do for any other look ahead



deffoofutureletfootokenfooaux
deffooaux%
ifxfootokenbgroup
% Brace group
else
% Something else
fi



The only reason this 'looks different' to other peek ahead situations is that you can't use an explicit {, but rather the implicit token bgroup.






share|improve this answer

























  • That's clear enough and much simpler than I originally imagined.

    – Weijun Zhou
    Apr 6 at 21:27













8












8








8







Fundamentally you just need to use futurelet as you do for any other look ahead



deffoofutureletfootokenfooaux
deffooaux%
ifxfootokenbgroup
% Brace group
else
% Something else
fi



The only reason this 'looks different' to other peek ahead situations is that you can't use an explicit {, but rather the implicit token bgroup.






share|improve this answer















Fundamentally you just need to use futurelet as you do for any other look ahead



deffoofutureletfootokenfooaux
deffooaux%
ifxfootokenbgroup
% Brace group
else
% Something else
fi



The only reason this 'looks different' to other peek ahead situations is that you can't use an explicit {, but rather the implicit token bgroup.







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 6 at 22:18









frougon

880711




880711










answered Apr 6 at 21:22









Joseph WrightJoseph Wright

205k23564892




205k23564892












  • That's clear enough and much simpler than I originally imagined.

    – Weijun Zhou
    Apr 6 at 21:27

















  • That's clear enough and much simpler than I originally imagined.

    – Weijun Zhou
    Apr 6 at 21:27
















That's clear enough and much simpler than I originally imagined.

– Weijun Zhou
Apr 6 at 21:27





That's clear enough and much simpler than I originally imagined.

– Weijun Zhou
Apr 6 at 21:27










Weijun Zhou is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















Weijun Zhou is a new contributor. Be nice, and check out our Code of Conduct.












Weijun Zhou is a new contributor. Be nice, and check out our Code of Conduct.











Weijun Zhou is a new contributor. Be nice, and check out our Code of Conduct.














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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f483588%2fhow-to-write-a-macro-that-is-braces-sensitive%23new-answer', 'question_page');

);

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







Popular posts from this blog

Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

Vilaño, A Laracha Índice Patrimonio | Lugares e parroquias | Véxase tamén | Menú de navegación43°14′52″N 8°36′03″O / 43.24775, -8.60070

Cegueira Índice Epidemioloxía | Deficiencia visual | Tipos de cegueira | Principais causas de cegueira | Tratamento | Técnicas de adaptación e axudas | Vida dos cegos | Primeiros auxilios | Crenzas respecto das persoas cegas | Crenzas das persoas cegas | O neno deficiente visual | Aspectos psicolóxicos da cegueira | Notas | Véxase tamén | Menú de navegación54.054.154.436928256blindnessDicionario da Real Academia GalegaPortal das Palabras"International Standards: Visual Standards — Aspects and Ranges of Vision Loss with Emphasis on Population Surveys.""Visual impairment and blindness""Presentan un plan para previr a cegueira"o orixinalACCDV Associació Catalana de Cecs i Disminuïts Visuals - PMFTrachoma"Effect of gene therapy on visual function in Leber's congenital amaurosis"1844137110.1056/NEJMoa0802268Cans guía - os mellores amigos dos cegosArquivadoEscola de cans guía para cegos en Mortágua, PortugalArquivado"Tecnología para ciegos y deficientes visuales. Recopilación de recursos gratuitos en la Red""Colorino""‘COL.diesis’, escuchar los sonidos del color""COL.diesis: Transforming Colour into Melody and Implementing the Result in a Colour Sensor Device"o orixinal"Sistema de desarrollo de sinestesia color-sonido para invidentes utilizando un protocolo de audio""Enseñanza táctil - geometría y color. Juegos didácticos para niños ciegos y videntes""Sistema Constanz"L'ocupació laboral dels cecs a l'Estat espanyol està pràcticament equiparada a la de les persones amb visió, entrevista amb Pedro ZuritaONCE (Organización Nacional de Cegos de España)Prevención da cegueiraDescrición de deficiencias visuais (Disc@pnet)Braillín, un boneco atractivo para calquera neno, con ou sen discapacidade, que permite familiarizarse co sistema de escritura e lectura brailleAxudas Técnicas36838ID00897494007150-90057129528256DOID:1432HP:0000618D001766C10.597.751.941.162C97109C0155020