How to write a macro that is braces sensitive?Context-sensitive newline macroCreate my own NSDHow to write a...

What do "features" mean/refer to in this sentence?

Why do falling prices hurt debtors?

Mathematical cryptic clues

Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

Risk of getting Chronic Wasting Disease in the US?

Smoothness of finite-dimensional functional calculus

Have astronauts in space suits ever taken selfies? If so, how?

US citizen flying to France today and my passport expires in less than 2 months

Which models of the Boeing 737 are still in production?

Finding angle with pure Geometry.

Why don't electron-positron collisions release infinite energy?

Test whether all array elements are factors of a number

Schoenfled Residua test shows proportionality hazard assumptions holds but Kaplan-Meier plots intersect

Is it possible to do 50 km distance without any previous training?

Font hinting is lost in Chrome-like browsers (for some languages )

Can a Warlock become Neutral Good?

What is the offset in a seaplane's hull?

Show that if two triangles built on parallel lines, with equal bases have the same perimeter only if they are congruent.

Languages that we cannot (dis)prove to be Context-Free

String Manipulation Interpreter

How old can references or sources in a thesis be?

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

Is it legal for company to use my work email to pretend I still work there?



How to write a macro that is braces sensitive?


Context-sensitive newline macroCreate my own NSDHow to write a parameter-driven macro?How can I create a better cryptogram environment?How to write a TeX macro that accepts a number or a count register as argument?Macro to close all open environments, groups and argument delimitersTexshop macro for bracesWrite an unpar macroInput length sensitive optional argument macrocontext-sensitive macro: look behind?













4















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 foo{a} 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.










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 foo{a} should be considered equivalent (when the argument consists of a single token as in this case).

    – egreg
    1 hour ago













  • 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
    1 hour ago








  • 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
    1 hour ago













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

    – Weijun Zhou
    52 mins ago
















4















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 foo{a} 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.










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 foo{a} should be considered equivalent (when the argument consists of a single token as in this case).

    – egreg
    1 hour ago













  • 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
    1 hour ago








  • 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
    1 hour ago













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

    – Weijun Zhou
    52 mins ago














4












4








4








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 foo{a} 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.










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 foo{a} 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.







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






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 1 hour ago









Weijun ZhouWeijun Zhou

1234




1234




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 foo{a} should be considered equivalent (when the argument consists of a single token as in this case).

    – egreg
    1 hour ago













  • 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
    1 hour ago








  • 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
    1 hour ago













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

    – Weijun Zhou
    52 mins ago



















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

    – egreg
    1 hour ago













  • 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
    1 hour ago








  • 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
    1 hour ago













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

    – Weijun Zhou
    52 mins ago

















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

– egreg
1 hour ago







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

– egreg
1 hour ago















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
1 hour ago







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
1 hour ago






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
1 hour ago







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
1 hour ago















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

– Weijun Zhou
52 mins ago





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

– Weijun Zhou
52 mins ago










2 Answers
2






active

oldest

votes


















4














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



deffoo{futureletfootokenfooaux}
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
    56 mins ago



















2














You can use futurelet



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


foo a

foo{a}

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{%
romannumeral0expandaftersecondoftwoexpandafter{expandafter{%
string#1.}expandafterfirstoftwoexpandafter{expandafter
secondoftwostring}expandafterexpandafterfirstoftwo{ }{}%
firstoftwo}{expandafterexpandafterfirstoftwo{ }{}secondoftwo}%
}%

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.}%

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 have some fun: Give [ the same functionality as {:
catcode`[=thecatcode`{

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.}%

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={

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{bgroupegroup 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{bgroup 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={

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{Xegroup 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{X 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






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
    15 mins ago













  • If you do so, don't be confused by the romannumeral0-thingie: romannumeral does not deliver any token in case it finds a non-positive number but while searching for more digits or a space which terminates the number and gets discarded, it triggers expansion of expandable tokens. Thus here it is used only for keeping expansion going. I will edit my answer and attach a bit of explanation. But that may take some minutes.

    – Ulrich Diez
    12 mins ago













  • I see. You may add this to the answer.

    – Weijun Zhou
    10 mins ago












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









4














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



deffoo{futureletfootokenfooaux}
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
    56 mins ago
















4














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



deffoo{futureletfootokenfooaux}
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
    56 mins ago














4












4








4







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



deffoo{futureletfootokenfooaux}
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



deffoo{futureletfootokenfooaux}
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 6 mins ago









frougon

783611




783611










answered 1 hour ago









Joseph WrightJoseph Wright

205k23563891




205k23563891













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

    – Weijun Zhou
    56 mins ago



















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

    – Weijun Zhou
    56 mins ago

















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

– Weijun Zhou
56 mins ago





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

– Weijun Zhou
56 mins ago











2














You can use futurelet



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


foo a

foo{a}

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{%
romannumeral0expandaftersecondoftwoexpandafter{expandafter{%
string#1.}expandafterfirstoftwoexpandafter{expandafter
secondoftwostring}expandafterexpandafterfirstoftwo{ }{}%
firstoftwo}{expandafterexpandafterfirstoftwo{ }{}secondoftwo}%
}%

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.}%

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 have some fun: Give [ the same functionality as {:
catcode`[=thecatcode`{

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.}%

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={

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{bgroupegroup 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{bgroup 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={

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{Xegroup 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{X 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






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
    15 mins ago













  • If you do so, don't be confused by the romannumeral0-thingie: romannumeral does not deliver any token in case it finds a non-positive number but while searching for more digits or a space which terminates the number and gets discarded, it triggers expansion of expandable tokens. Thus here it is used only for keeping expansion going. I will edit my answer and attach a bit of explanation. But that may take some minutes.

    – Ulrich Diez
    12 mins ago













  • I see. You may add this to the answer.

    – Weijun Zhou
    10 mins ago
















2














You can use futurelet



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


foo a

foo{a}

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{%
romannumeral0expandaftersecondoftwoexpandafter{expandafter{%
string#1.}expandafterfirstoftwoexpandafter{expandafter
secondoftwostring}expandafterexpandafterfirstoftwo{ }{}%
firstoftwo}{expandafterexpandafterfirstoftwo{ }{}secondoftwo}%
}%

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.}%

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 have some fun: Give [ the same functionality as {:
catcode`[=thecatcode`{

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.}%

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={

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{bgroupegroup 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{bgroup 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={

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{Xegroup 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{X 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






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
    15 mins ago













  • If you do so, don't be confused by the romannumeral0-thingie: romannumeral does not deliver any token in case it finds a non-positive number but while searching for more digits or a space which terminates the number and gets discarded, it triggers expansion of expandable tokens. Thus here it is used only for keeping expansion going. I will edit my answer and attach a bit of explanation. But that may take some minutes.

    – Ulrich Diez
    12 mins ago













  • I see. You may add this to the answer.

    – Weijun Zhou
    10 mins ago














2












2








2







You can use futurelet



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


foo a

foo{a}

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{%
romannumeral0expandaftersecondoftwoexpandafter{expandafter{%
string#1.}expandafterfirstoftwoexpandafter{expandafter
secondoftwostring}expandafterexpandafterfirstoftwo{ }{}%
firstoftwo}{expandafterexpandafterfirstoftwo{ }{}secondoftwo}%
}%

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.}%

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 have some fun: Give [ the same functionality as {:
catcode`[=thecatcode`{

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.}%

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={

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{bgroupegroup 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{bgroup 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={

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{Xegroup 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{X 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






share|improve this answer













You can use futurelet



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


foo a

foo{a}

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{%
romannumeral0expandaftersecondoftwoexpandafter{expandafter{%
string#1.}expandafterfirstoftwoexpandafter{expandafter
secondoftwostring}expandafterexpandafterfirstoftwo{ }{}%
firstoftwo}{expandafterexpandafterfirstoftwo{ }{}secondoftwo}%
}%

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.}%

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 have some fun: Give [ the same functionality as {:
catcode`[=thecatcode`{

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.}%

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={

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{bgroupegroup 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{bgroup 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={

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{Xegroup 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{X 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







share|improve this answer












share|improve this answer



share|improve this answer










answered 21 mins ago









Ulrich DiezUlrich Diez

5,570620




5,570620













  • 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
    15 mins ago













  • If you do so, don't be confused by the romannumeral0-thingie: romannumeral does not deliver any token in case it finds a non-positive number but while searching for more digits or a space which terminates the number and gets discarded, it triggers expansion of expandable tokens. Thus here it is used only for keeping expansion going. I will edit my answer and attach a bit of explanation. But that may take some minutes.

    – Ulrich Diez
    12 mins ago













  • I see. You may add this to the answer.

    – Weijun Zhou
    10 mins ago



















  • 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
    15 mins ago













  • If you do so, don't be confused by the romannumeral0-thingie: romannumeral does not deliver any token in case it finds a non-positive number but while searching for more digits or a space which terminates the number and gets discarded, it triggers expansion of expandable tokens. Thus here it is used only for keeping expansion going. I will edit my answer and attach a bit of explanation. But that may take some minutes.

    – Ulrich Diez
    12 mins ago













  • I see. You may add this to the answer.

    – Weijun Zhou
    10 mins ago

















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
15 mins ago







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
15 mins ago















If you do so, don't be confused by the romannumeral0-thingie: romannumeral does not deliver any token in case it finds a non-positive number but while searching for more digits or a space which terminates the number and gets discarded, it triggers expansion of expandable tokens. Thus here it is used only for keeping expansion going. I will edit my answer and attach a bit of explanation. But that may take some minutes.

– Ulrich Diez
12 mins ago







If you do so, don't be confused by the romannumeral0-thingie: romannumeral does not deliver any token in case it finds a non-positive number but while searching for more digits or a space which terminates the number and gets discarded, it triggers expansion of expandable tokens. Thus here it is used only for keeping expansion going. I will edit my answer and attach a bit of explanation. But that may take some minutes.

– Ulrich Diez
12 mins ago















I see. You may add this to the answer.

– Weijun Zhou
10 mins ago





I see. You may add this to the answer.

– Weijun Zhou
10 mins ago










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

Щит и меч (фильм) Содержание Названия серий | Сюжет |...

Венесуэла на летних Олимпийских играх 2000 Содержание Состав...

Meter-Bus Содержание Параметры шины | Стандартизация |...