Detecting subscript in command argumentPassing multiple arguments to an ifthenelse routineScan argument for parenthesesNested commands with parametrized namesSubscripts after a macro for left( and right) parenthesesMerge double subscripts in macroInput command arguments from filesubscript and superscript in Lyx leave large gaps in onscreen editorConditionals inside tableAccented characters in LaTeX subscript in math modePrinting arguments in newcommand at different parts of document
Good for you! in Russian
What wound would be of little consequence to a biped but terrible for a quadruped?
Should I take out a loan for a friend to invest on my behalf?
How to pass a string to a command that expects a file?
Aliens englobed the Solar System: will we notice?
What's the "normal" opposite of flautando?
If the Captain's screens are out, does he switch seats with the co-pilot?
Is there an elementary proof that there are infinitely many primes that are *not* completely split in an abelian extension?
Grey hair or white hair
Does splitting a potentially monolithic application into several smaller ones help prevent bugs?
Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?
Are babies of evil humanoid species inherently evil?
infinitive telling the purpose
How can I budget to build up a down payment for a house over the course of a year?
Why do different render engines generate different z pass?
Word for a person who has no opinion about whether god exists
Best approach to update all entries in a list that is paginated?
Does "variables should live in the smallest scope as possible" include the case "variables should not exist if possible"?
Rejected in 4th interview round citing insufficient years of experience
How did Alan Turing break the enigma code using the hint given by the lady in the bar?
My story is written in English, but is set in my home country. What language should I use for the dialogue?
Make a transparent 448*448 image
Why is this plane circling around the LKO airport every day?
Reverse string, can I make it faster?
Detecting subscript in command argument
Passing multiple arguments to an ifthenelse routineScan argument for parenthesesNested commands with parametrized namesSubscripts after a macro for left( and right) parenthesesMerge double subscripts in macroInput command arguments from filesubscript and superscript in Lyx leave large gaps in onscreen editorConditionals inside tableAccented characters in LaTeX subscript in math modePrinting arguments in newcommand at different parts of document
I have defined a command low
that adds a subscript to an argument:
newcommandlow[1]#1_l_mathcalA
However, if the argument of low
contains subscripts itsself (for example in the case of lowlowSigma
) it is not easy to see that the subscripts introduced by the outer low
belong to the whole argument and not just to the first subscript. Therefore I would like to automatically introduce parentheses if the argument of low
contains a subscript (so lowlowSigma
should look like low(lowSigma)
.
In order to achieve this I tried the following using the xifthen package:
newcommandlow[1]ifthenelseisin_#1(#1)_l_mathcalA#1_l_mathcalA
However to my surprise this command only sometimes inserts the parantheses.
In particular it does not do so in the example given above. Why does this happen and what can I do to fix this?
EDIT: It seems that the problem is that isin
does not unroll the definitions of commands. @egreg has already provided an answer that allows me to check for nested calls of low
but does not work for arguments that contain other commands with subscripts. Does anyone have a solution that works for arbitrary arguments?
macros conditionals subscripts ifthenelse xifthen
New contributor
add a comment |
I have defined a command low
that adds a subscript to an argument:
newcommandlow[1]#1_l_mathcalA
However, if the argument of low
contains subscripts itsself (for example in the case of lowlowSigma
) it is not easy to see that the subscripts introduced by the outer low
belong to the whole argument and not just to the first subscript. Therefore I would like to automatically introduce parentheses if the argument of low
contains a subscript (so lowlowSigma
should look like low(lowSigma)
.
In order to achieve this I tried the following using the xifthen package:
newcommandlow[1]ifthenelseisin_#1(#1)_l_mathcalA#1_l_mathcalA
However to my surprise this command only sometimes inserts the parantheses.
In particular it does not do so in the example given above. Why does this happen and what can I do to fix this?
EDIT: It seems that the problem is that isin
does not unroll the definitions of commands. @egreg has already provided an answer that allows me to check for nested calls of low
but does not work for arguments that contain other commands with subscripts. Does anyone have a solution that works for arbitrary arguments?
macros conditionals subscripts ifthenelse xifthen
New contributor
add a comment |
I have defined a command low
that adds a subscript to an argument:
newcommandlow[1]#1_l_mathcalA
However, if the argument of low
contains subscripts itsself (for example in the case of lowlowSigma
) it is not easy to see that the subscripts introduced by the outer low
belong to the whole argument and not just to the first subscript. Therefore I would like to automatically introduce parentheses if the argument of low
contains a subscript (so lowlowSigma
should look like low(lowSigma)
.
In order to achieve this I tried the following using the xifthen package:
newcommandlow[1]ifthenelseisin_#1(#1)_l_mathcalA#1_l_mathcalA
However to my surprise this command only sometimes inserts the parantheses.
In particular it does not do so in the example given above. Why does this happen and what can I do to fix this?
EDIT: It seems that the problem is that isin
does not unroll the definitions of commands. @egreg has already provided an answer that allows me to check for nested calls of low
but does not work for arguments that contain other commands with subscripts. Does anyone have a solution that works for arbitrary arguments?
macros conditionals subscripts ifthenelse xifthen
New contributor
I have defined a command low
that adds a subscript to an argument:
newcommandlow[1]#1_l_mathcalA
However, if the argument of low
contains subscripts itsself (for example in the case of lowlowSigma
) it is not easy to see that the subscripts introduced by the outer low
belong to the whole argument and not just to the first subscript. Therefore I would like to automatically introduce parentheses if the argument of low
contains a subscript (so lowlowSigma
should look like low(lowSigma)
.
In order to achieve this I tried the following using the xifthen package:
newcommandlow[1]ifthenelseisin_#1(#1)_l_mathcalA#1_l_mathcalA
However to my surprise this command only sometimes inserts the parantheses.
In particular it does not do so in the example given above. Why does this happen and what can I do to fix this?
EDIT: It seems that the problem is that isin
does not unroll the definitions of commands. @egreg has already provided an answer that allows me to check for nested calls of low
but does not work for arguments that contain other commands with subscripts. Does anyone have a solution that works for arbitrary arguments?
macros conditionals subscripts ifthenelse xifthen
macros conditionals subscripts ifthenelse xifthen
New contributor
New contributor
edited 5 hours ago
Rincewind
New contributor
asked 9 hours ago
RincewindRincewind
113
113
New contributor
New contributor
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can use a conditional. If it is true, add parentheses, if it is false, set it to true and don't add parentheses.
documentclassarticle
usepackageamsmath
newififnestedlow
newcommandlow[1]%
begingroup
ifnestedlow
(normallow#1)%
else
nestedlowtrue
normallow#1%
fi
endgroup
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad lowlowSigma qquad lowSigma
]
enddocument
Doing this in a group ensures the conditional will be false at the next call.
Thanks for the suggestion. This does, however, only check whether I have nested calls of low. It does not seem to do anything in cases where I have for examplelowSigma_1
or evenlowsubs
wheresubs
produces some output with a subscript.
– Rincewind
6 hours ago
@Rincewind The problem is underspecified, then. And very hard to solve in the general case:subs
may expand to something containing_
. If you allow whatever command that may expand to something with_
, then no, you can't do it.
– egreg
6 hours ago
add a comment |
In some cases applying protected@edef
and @onelevel@sanitize
before checking for a "stringified" _
might work out:
documentclassarticle
usepackageamsmath
usepackagexifthen
newififnownestlownownestlowfalse
makeatletter
DeclareRobustCommandlow[1]%
begingroup
begingroup
protected@edef@tempa#1%
@onelevel@sanitize@tempa
expandafterexpandafterexpandafterendgroup
expandafterexpandafterexpandafterifthenelse
expandafterexpandafterexpandafter%
expandafterexpandafterexpandafterisin
expandafterexpandafterexpandafter%
expandafterexpandafterstring_%
expandafter%
expandafter%
@tempa%
ifnownestlow(normallow(#1))elsenownestlowtruenormallow(#1)fi%
ifnownestlow(normallow#1)elsenownestlowtruenormallow#1fi%
endgroup
makeatother
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad
lowlowSigma qquad
lowSigma_b qquad
lowSigma_b qquad
lowb_lowc_lowSigma_d qquad
(Sigma_b)_l_mathcalA
]
enddocument
expandafter
causes that the next but one token —if expandable—gets expanded exactly once before the next token gets expanded if expandable. (La)TeX considers the work of expandafter
done when expansion of the next but one token is done. Therefore you can use chains/dequences of expandafter
to have (La)TeX "jump" over k tokens for first expanding the (k+1)-th token.
@onelevel@sanitizemacro
changes the definition of macro
so that macro
spits out a sequence of character tokens of category code 12(other) that looks like the token-sequence that would have been "spit out" by macro
before applying @onelevel@sanitize
. It is almost like redefining macro
to what you get by applying string
to each token of macro
's definition.
protected@edef
defines a macro but before doing so, it expands all expandable tokens of the definition-text except those that either are defined via DeclareRobustCommand
or are preceded by the token protect
. You might say: protected@edef
does "unroll" the definitions of the tokens contained in its definition-text before actually performing the assignment.
@tempa
is a scratch-macro which gets defined by means of protected@edef
to expand to the argument #1
with all definitions in #1
"unrolled".
The ifthenelseisin...
-test does not find _
that are nested in curly-braces as curly braces usually have a special function. Therefore @onelevel@sanitize
is applied for turning all tokens, and thus also the curly braces, into ordinary harmless character-tokens of category code 12(other) which do not disturb the ifthenelseisin...
test.
To be honest, I don't really understand what is going on here. What doprotected@edef
,tempa
,@onelevel@sanitize
,string
andexpandafter
do? As it is it does not seem to be doing what I want. I'd likelowSigma_b
to have the same output as(Sigma_b)_l_mathcalA
. Because I don't really understand what's going on, I can't tell whether this can be done by slightly modifying your solution or not.
– Rincewind
4 hours ago
@Rincewind I have revised and modified my answer. I hope the code now does what you need. I also tried to add some explanation. ;-)
– Ulrich Diez
11 mins ago
add a comment |
An idea (not straight forward solution) is to place the argument inside a box and check its height with the height of a character you suppose is not too tall to need parenthesis but not too short to add parenthesis to your Sigma.
And guess what will be our default argument: Sigma
... P
The code (containing some tests) is this:
documentclassarticle
defDefLowArg$Sigma$
letoldDefLowArgDefLowArg
newsaveboxmyAbox
newsaveboxmyBbox
newcommandlow[2][DefLowArg]saveboxmyAboxvbox#1saveboxmyBboxvboxensuremath#2
ifdimdimexprhtmyAbox+dpmyAbox<dimexprhtmyBbox+dpmyBboxrelax
left(#2right)_l_mathcalA
else #2_l_mathcalAfi
begindocument
[lowlowSigma]
[lowSigma]
[
lowsum_i=3^5 F(x)
]
[
lowfracF(x)x+5
]
[lowF_x]
[low[1/4]F(x)]
[lowx^2]
[
lowG_x
]
These commands may be should add without parentheses
[
lowg(z)
]
[
lowF(x)
]
bfseries Solution 1 Add an tall optional argument in the command like: verb
[
low[/]g(z)
]
[
low[/]F(x)
]
):
xdefDefLowArg/
[
lowg(z)
]
[
lowF(x)
]
letDefLowArgoldDefLowArg
bfseries And back to default
[
lowF(X)
]
enddocument
That produces:
PS:Of course manual solutions should be added in special cases but anyway in your command I am sure you would have exceptions for many cases.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "85"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/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
);
);
Rincewind is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f479070%2fdetecting-subscript-in-command-argument%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use a conditional. If it is true, add parentheses, if it is false, set it to true and don't add parentheses.
documentclassarticle
usepackageamsmath
newififnestedlow
newcommandlow[1]%
begingroup
ifnestedlow
(normallow#1)%
else
nestedlowtrue
normallow#1%
fi
endgroup
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad lowlowSigma qquad lowSigma
]
enddocument
Doing this in a group ensures the conditional will be false at the next call.
Thanks for the suggestion. This does, however, only check whether I have nested calls of low. It does not seem to do anything in cases where I have for examplelowSigma_1
or evenlowsubs
wheresubs
produces some output with a subscript.
– Rincewind
6 hours ago
@Rincewind The problem is underspecified, then. And very hard to solve in the general case:subs
may expand to something containing_
. If you allow whatever command that may expand to something with_
, then no, you can't do it.
– egreg
6 hours ago
add a comment |
You can use a conditional. If it is true, add parentheses, if it is false, set it to true and don't add parentheses.
documentclassarticle
usepackageamsmath
newififnestedlow
newcommandlow[1]%
begingroup
ifnestedlow
(normallow#1)%
else
nestedlowtrue
normallow#1%
fi
endgroup
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad lowlowSigma qquad lowSigma
]
enddocument
Doing this in a group ensures the conditional will be false at the next call.
Thanks for the suggestion. This does, however, only check whether I have nested calls of low. It does not seem to do anything in cases where I have for examplelowSigma_1
or evenlowsubs
wheresubs
produces some output with a subscript.
– Rincewind
6 hours ago
@Rincewind The problem is underspecified, then. And very hard to solve in the general case:subs
may expand to something containing_
. If you allow whatever command that may expand to something with_
, then no, you can't do it.
– egreg
6 hours ago
add a comment |
You can use a conditional. If it is true, add parentheses, if it is false, set it to true and don't add parentheses.
documentclassarticle
usepackageamsmath
newififnestedlow
newcommandlow[1]%
begingroup
ifnestedlow
(normallow#1)%
else
nestedlowtrue
normallow#1%
fi
endgroup
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad lowlowSigma qquad lowSigma
]
enddocument
Doing this in a group ensures the conditional will be false at the next call.
You can use a conditional. If it is true, add parentheses, if it is false, set it to true and don't add parentheses.
documentclassarticle
usepackageamsmath
newififnestedlow
newcommandlow[1]%
begingroup
ifnestedlow
(normallow#1)%
else
nestedlowtrue
normallow#1%
fi
endgroup
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad lowlowSigma qquad lowSigma
]
enddocument
Doing this in a group ensures the conditional will be false at the next call.
answered 7 hours ago
egregegreg
726k8819193226
726k8819193226
Thanks for the suggestion. This does, however, only check whether I have nested calls of low. It does not seem to do anything in cases where I have for examplelowSigma_1
or evenlowsubs
wheresubs
produces some output with a subscript.
– Rincewind
6 hours ago
@Rincewind The problem is underspecified, then. And very hard to solve in the general case:subs
may expand to something containing_
. If you allow whatever command that may expand to something with_
, then no, you can't do it.
– egreg
6 hours ago
add a comment |
Thanks for the suggestion. This does, however, only check whether I have nested calls of low. It does not seem to do anything in cases where I have for examplelowSigma_1
or evenlowsubs
wheresubs
produces some output with a subscript.
– Rincewind
6 hours ago
@Rincewind The problem is underspecified, then. And very hard to solve in the general case:subs
may expand to something containing_
. If you allow whatever command that may expand to something with_
, then no, you can't do it.
– egreg
6 hours ago
Thanks for the suggestion. This does, however, only check whether I have nested calls of low. It does not seem to do anything in cases where I have for example
lowSigma_1
or even lowsubs
where subs
produces some output with a subscript.– Rincewind
6 hours ago
Thanks for the suggestion. This does, however, only check whether I have nested calls of low. It does not seem to do anything in cases where I have for example
lowSigma_1
or even lowsubs
where subs
produces some output with a subscript.– Rincewind
6 hours ago
@Rincewind The problem is underspecified, then. And very hard to solve in the general case:
subs
may expand to something containing _
. If you allow whatever command that may expand to something with _
, then no, you can't do it.– egreg
6 hours ago
@Rincewind The problem is underspecified, then. And very hard to solve in the general case:
subs
may expand to something containing _
. If you allow whatever command that may expand to something with _
, then no, you can't do it.– egreg
6 hours ago
add a comment |
In some cases applying protected@edef
and @onelevel@sanitize
before checking for a "stringified" _
might work out:
documentclassarticle
usepackageamsmath
usepackagexifthen
newififnownestlownownestlowfalse
makeatletter
DeclareRobustCommandlow[1]%
begingroup
begingroup
protected@edef@tempa#1%
@onelevel@sanitize@tempa
expandafterexpandafterexpandafterendgroup
expandafterexpandafterexpandafterifthenelse
expandafterexpandafterexpandafter%
expandafterexpandafterexpandafterisin
expandafterexpandafterexpandafter%
expandafterexpandafterstring_%
expandafter%
expandafter%
@tempa%
ifnownestlow(normallow(#1))elsenownestlowtruenormallow(#1)fi%
ifnownestlow(normallow#1)elsenownestlowtruenormallow#1fi%
endgroup
makeatother
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad
lowlowSigma qquad
lowSigma_b qquad
lowSigma_b qquad
lowb_lowc_lowSigma_d qquad
(Sigma_b)_l_mathcalA
]
enddocument
expandafter
causes that the next but one token —if expandable—gets expanded exactly once before the next token gets expanded if expandable. (La)TeX considers the work of expandafter
done when expansion of the next but one token is done. Therefore you can use chains/dequences of expandafter
to have (La)TeX "jump" over k tokens for first expanding the (k+1)-th token.
@onelevel@sanitizemacro
changes the definition of macro
so that macro
spits out a sequence of character tokens of category code 12(other) that looks like the token-sequence that would have been "spit out" by macro
before applying @onelevel@sanitize
. It is almost like redefining macro
to what you get by applying string
to each token of macro
's definition.
protected@edef
defines a macro but before doing so, it expands all expandable tokens of the definition-text except those that either are defined via DeclareRobustCommand
or are preceded by the token protect
. You might say: protected@edef
does "unroll" the definitions of the tokens contained in its definition-text before actually performing the assignment.
@tempa
is a scratch-macro which gets defined by means of protected@edef
to expand to the argument #1
with all definitions in #1
"unrolled".
The ifthenelseisin...
-test does not find _
that are nested in curly-braces as curly braces usually have a special function. Therefore @onelevel@sanitize
is applied for turning all tokens, and thus also the curly braces, into ordinary harmless character-tokens of category code 12(other) which do not disturb the ifthenelseisin...
test.
To be honest, I don't really understand what is going on here. What doprotected@edef
,tempa
,@onelevel@sanitize
,string
andexpandafter
do? As it is it does not seem to be doing what I want. I'd likelowSigma_b
to have the same output as(Sigma_b)_l_mathcalA
. Because I don't really understand what's going on, I can't tell whether this can be done by slightly modifying your solution or not.
– Rincewind
4 hours ago
@Rincewind I have revised and modified my answer. I hope the code now does what you need. I also tried to add some explanation. ;-)
– Ulrich Diez
11 mins ago
add a comment |
In some cases applying protected@edef
and @onelevel@sanitize
before checking for a "stringified" _
might work out:
documentclassarticle
usepackageamsmath
usepackagexifthen
newififnownestlownownestlowfalse
makeatletter
DeclareRobustCommandlow[1]%
begingroup
begingroup
protected@edef@tempa#1%
@onelevel@sanitize@tempa
expandafterexpandafterexpandafterendgroup
expandafterexpandafterexpandafterifthenelse
expandafterexpandafterexpandafter%
expandafterexpandafterexpandafterisin
expandafterexpandafterexpandafter%
expandafterexpandafterstring_%
expandafter%
expandafter%
@tempa%
ifnownestlow(normallow(#1))elsenownestlowtruenormallow(#1)fi%
ifnownestlow(normallow#1)elsenownestlowtruenormallow#1fi%
endgroup
makeatother
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad
lowlowSigma qquad
lowSigma_b qquad
lowSigma_b qquad
lowb_lowc_lowSigma_d qquad
(Sigma_b)_l_mathcalA
]
enddocument
expandafter
causes that the next but one token —if expandable—gets expanded exactly once before the next token gets expanded if expandable. (La)TeX considers the work of expandafter
done when expansion of the next but one token is done. Therefore you can use chains/dequences of expandafter
to have (La)TeX "jump" over k tokens for first expanding the (k+1)-th token.
@onelevel@sanitizemacro
changes the definition of macro
so that macro
spits out a sequence of character tokens of category code 12(other) that looks like the token-sequence that would have been "spit out" by macro
before applying @onelevel@sanitize
. It is almost like redefining macro
to what you get by applying string
to each token of macro
's definition.
protected@edef
defines a macro but before doing so, it expands all expandable tokens of the definition-text except those that either are defined via DeclareRobustCommand
or are preceded by the token protect
. You might say: protected@edef
does "unroll" the definitions of the tokens contained in its definition-text before actually performing the assignment.
@tempa
is a scratch-macro which gets defined by means of protected@edef
to expand to the argument #1
with all definitions in #1
"unrolled".
The ifthenelseisin...
-test does not find _
that are nested in curly-braces as curly braces usually have a special function. Therefore @onelevel@sanitize
is applied for turning all tokens, and thus also the curly braces, into ordinary harmless character-tokens of category code 12(other) which do not disturb the ifthenelseisin...
test.
To be honest, I don't really understand what is going on here. What doprotected@edef
,tempa
,@onelevel@sanitize
,string
andexpandafter
do? As it is it does not seem to be doing what I want. I'd likelowSigma_b
to have the same output as(Sigma_b)_l_mathcalA
. Because I don't really understand what's going on, I can't tell whether this can be done by slightly modifying your solution or not.
– Rincewind
4 hours ago
@Rincewind I have revised and modified my answer. I hope the code now does what you need. I also tried to add some explanation. ;-)
– Ulrich Diez
11 mins ago
add a comment |
In some cases applying protected@edef
and @onelevel@sanitize
before checking for a "stringified" _
might work out:
documentclassarticle
usepackageamsmath
usepackagexifthen
newififnownestlownownestlowfalse
makeatletter
DeclareRobustCommandlow[1]%
begingroup
begingroup
protected@edef@tempa#1%
@onelevel@sanitize@tempa
expandafterexpandafterexpandafterendgroup
expandafterexpandafterexpandafterifthenelse
expandafterexpandafterexpandafter%
expandafterexpandafterexpandafterisin
expandafterexpandafterexpandafter%
expandafterexpandafterstring_%
expandafter%
expandafter%
@tempa%
ifnownestlow(normallow(#1))elsenownestlowtruenormallow(#1)fi%
ifnownestlow(normallow#1)elsenownestlowtruenormallow#1fi%
endgroup
makeatother
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad
lowlowSigma qquad
lowSigma_b qquad
lowSigma_b qquad
lowb_lowc_lowSigma_d qquad
(Sigma_b)_l_mathcalA
]
enddocument
expandafter
causes that the next but one token —if expandable—gets expanded exactly once before the next token gets expanded if expandable. (La)TeX considers the work of expandafter
done when expansion of the next but one token is done. Therefore you can use chains/dequences of expandafter
to have (La)TeX "jump" over k tokens for first expanding the (k+1)-th token.
@onelevel@sanitizemacro
changes the definition of macro
so that macro
spits out a sequence of character tokens of category code 12(other) that looks like the token-sequence that would have been "spit out" by macro
before applying @onelevel@sanitize
. It is almost like redefining macro
to what you get by applying string
to each token of macro
's definition.
protected@edef
defines a macro but before doing so, it expands all expandable tokens of the definition-text except those that either are defined via DeclareRobustCommand
or are preceded by the token protect
. You might say: protected@edef
does "unroll" the definitions of the tokens contained in its definition-text before actually performing the assignment.
@tempa
is a scratch-macro which gets defined by means of protected@edef
to expand to the argument #1
with all definitions in #1
"unrolled".
The ifthenelseisin...
-test does not find _
that are nested in curly-braces as curly braces usually have a special function. Therefore @onelevel@sanitize
is applied for turning all tokens, and thus also the curly braces, into ordinary harmless character-tokens of category code 12(other) which do not disturb the ifthenelseisin...
test.
In some cases applying protected@edef
and @onelevel@sanitize
before checking for a "stringified" _
might work out:
documentclassarticle
usepackageamsmath
usepackagexifthen
newififnownestlownownestlowfalse
makeatletter
DeclareRobustCommandlow[1]%
begingroup
begingroup
protected@edef@tempa#1%
@onelevel@sanitize@tempa
expandafterexpandafterexpandafterendgroup
expandafterexpandafterexpandafterifthenelse
expandafterexpandafterexpandafter%
expandafterexpandafterexpandafterisin
expandafterexpandafterexpandafter%
expandafterexpandafterstring_%
expandafter%
expandafter%
@tempa%
ifnownestlow(normallow(#1))elsenownestlowtruenormallow(#1)fi%
ifnownestlow(normallow#1)elsenownestlowtruenormallow#1fi%
endgroup
makeatother
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad
lowlowSigma qquad
lowSigma_b qquad
lowSigma_b qquad
lowb_lowc_lowSigma_d qquad
(Sigma_b)_l_mathcalA
]
enddocument
expandafter
causes that the next but one token —if expandable—gets expanded exactly once before the next token gets expanded if expandable. (La)TeX considers the work of expandafter
done when expansion of the next but one token is done. Therefore you can use chains/dequences of expandafter
to have (La)TeX "jump" over k tokens for first expanding the (k+1)-th token.
@onelevel@sanitizemacro
changes the definition of macro
so that macro
spits out a sequence of character tokens of category code 12(other) that looks like the token-sequence that would have been "spit out" by macro
before applying @onelevel@sanitize
. It is almost like redefining macro
to what you get by applying string
to each token of macro
's definition.
protected@edef
defines a macro but before doing so, it expands all expandable tokens of the definition-text except those that either are defined via DeclareRobustCommand
or are preceded by the token protect
. You might say: protected@edef
does "unroll" the definitions of the tokens contained in its definition-text before actually performing the assignment.
@tempa
is a scratch-macro which gets defined by means of protected@edef
to expand to the argument #1
with all definitions in #1
"unrolled".
The ifthenelseisin...
-test does not find _
that are nested in curly-braces as curly braces usually have a special function. Therefore @onelevel@sanitize
is applied for turning all tokens, and thus also the curly braces, into ordinary harmless character-tokens of category code 12(other) which do not disturb the ifthenelseisin...
test.
edited 16 mins ago
answered 5 hours ago
Ulrich DiezUlrich Diez
5,390619
5,390619
To be honest, I don't really understand what is going on here. What doprotected@edef
,tempa
,@onelevel@sanitize
,string
andexpandafter
do? As it is it does not seem to be doing what I want. I'd likelowSigma_b
to have the same output as(Sigma_b)_l_mathcalA
. Because I don't really understand what's going on, I can't tell whether this can be done by slightly modifying your solution or not.
– Rincewind
4 hours ago
@Rincewind I have revised and modified my answer. I hope the code now does what you need. I also tried to add some explanation. ;-)
– Ulrich Diez
11 mins ago
add a comment |
To be honest, I don't really understand what is going on here. What doprotected@edef
,tempa
,@onelevel@sanitize
,string
andexpandafter
do? As it is it does not seem to be doing what I want. I'd likelowSigma_b
to have the same output as(Sigma_b)_l_mathcalA
. Because I don't really understand what's going on, I can't tell whether this can be done by slightly modifying your solution or not.
– Rincewind
4 hours ago
@Rincewind I have revised and modified my answer. I hope the code now does what you need. I also tried to add some explanation. ;-)
– Ulrich Diez
11 mins ago
To be honest, I don't really understand what is going on here. What do
protected@edef
, tempa
,@onelevel@sanitize
, string
and expandafter
do? As it is it does not seem to be doing what I want. I'd like lowSigma_b
to have the same output as (Sigma_b)_l_mathcalA
. Because I don't really understand what's going on, I can't tell whether this can be done by slightly modifying your solution or not.– Rincewind
4 hours ago
To be honest, I don't really understand what is going on here. What do
protected@edef
, tempa
,@onelevel@sanitize
, string
and expandafter
do? As it is it does not seem to be doing what I want. I'd like lowSigma_b
to have the same output as (Sigma_b)_l_mathcalA
. Because I don't really understand what's going on, I can't tell whether this can be done by slightly modifying your solution or not.– Rincewind
4 hours ago
@Rincewind I have revised and modified my answer. I hope the code now does what you need. I also tried to add some explanation. ;-)
– Ulrich Diez
11 mins ago
@Rincewind I have revised and modified my answer. I hope the code now does what you need. I also tried to add some explanation. ;-)
– Ulrich Diez
11 mins ago
add a comment |
An idea (not straight forward solution) is to place the argument inside a box and check its height with the height of a character you suppose is not too tall to need parenthesis but not too short to add parenthesis to your Sigma.
And guess what will be our default argument: Sigma
... P
The code (containing some tests) is this:
documentclassarticle
defDefLowArg$Sigma$
letoldDefLowArgDefLowArg
newsaveboxmyAbox
newsaveboxmyBbox
newcommandlow[2][DefLowArg]saveboxmyAboxvbox#1saveboxmyBboxvboxensuremath#2
ifdimdimexprhtmyAbox+dpmyAbox<dimexprhtmyBbox+dpmyBboxrelax
left(#2right)_l_mathcalA
else #2_l_mathcalAfi
begindocument
[lowlowSigma]
[lowSigma]
[
lowsum_i=3^5 F(x)
]
[
lowfracF(x)x+5
]
[lowF_x]
[low[1/4]F(x)]
[lowx^2]
[
lowG_x
]
These commands may be should add without parentheses
[
lowg(z)
]
[
lowF(x)
]
bfseries Solution 1 Add an tall optional argument in the command like: verb
[
low[/]g(z)
]
[
low[/]F(x)
]
):
xdefDefLowArg/
[
lowg(z)
]
[
lowF(x)
]
letDefLowArgoldDefLowArg
bfseries And back to default
[
lowF(X)
]
enddocument
That produces:
PS:Of course manual solutions should be added in special cases but anyway in your command I am sure you would have exceptions for many cases.
add a comment |
An idea (not straight forward solution) is to place the argument inside a box and check its height with the height of a character you suppose is not too tall to need parenthesis but not too short to add parenthesis to your Sigma.
And guess what will be our default argument: Sigma
... P
The code (containing some tests) is this:
documentclassarticle
defDefLowArg$Sigma$
letoldDefLowArgDefLowArg
newsaveboxmyAbox
newsaveboxmyBbox
newcommandlow[2][DefLowArg]saveboxmyAboxvbox#1saveboxmyBboxvboxensuremath#2
ifdimdimexprhtmyAbox+dpmyAbox<dimexprhtmyBbox+dpmyBboxrelax
left(#2right)_l_mathcalA
else #2_l_mathcalAfi
begindocument
[lowlowSigma]
[lowSigma]
[
lowsum_i=3^5 F(x)
]
[
lowfracF(x)x+5
]
[lowF_x]
[low[1/4]F(x)]
[lowx^2]
[
lowG_x
]
These commands may be should add without parentheses
[
lowg(z)
]
[
lowF(x)
]
bfseries Solution 1 Add an tall optional argument in the command like: verb
[
low[/]g(z)
]
[
low[/]F(x)
]
):
xdefDefLowArg/
[
lowg(z)
]
[
lowF(x)
]
letDefLowArgoldDefLowArg
bfseries And back to default
[
lowF(X)
]
enddocument
That produces:
PS:Of course manual solutions should be added in special cases but anyway in your command I am sure you would have exceptions for many cases.
add a comment |
An idea (not straight forward solution) is to place the argument inside a box and check its height with the height of a character you suppose is not too tall to need parenthesis but not too short to add parenthesis to your Sigma.
And guess what will be our default argument: Sigma
... P
The code (containing some tests) is this:
documentclassarticle
defDefLowArg$Sigma$
letoldDefLowArgDefLowArg
newsaveboxmyAbox
newsaveboxmyBbox
newcommandlow[2][DefLowArg]saveboxmyAboxvbox#1saveboxmyBboxvboxensuremath#2
ifdimdimexprhtmyAbox+dpmyAbox<dimexprhtmyBbox+dpmyBboxrelax
left(#2right)_l_mathcalA
else #2_l_mathcalAfi
begindocument
[lowlowSigma]
[lowSigma]
[
lowsum_i=3^5 F(x)
]
[
lowfracF(x)x+5
]
[lowF_x]
[low[1/4]F(x)]
[lowx^2]
[
lowG_x
]
These commands may be should add without parentheses
[
lowg(z)
]
[
lowF(x)
]
bfseries Solution 1 Add an tall optional argument in the command like: verb
[
low[/]g(z)
]
[
low[/]F(x)
]
):
xdefDefLowArg/
[
lowg(z)
]
[
lowF(x)
]
letDefLowArgoldDefLowArg
bfseries And back to default
[
lowF(X)
]
enddocument
That produces:
PS:Of course manual solutions should be added in special cases but anyway in your command I am sure you would have exceptions for many cases.
An idea (not straight forward solution) is to place the argument inside a box and check its height with the height of a character you suppose is not too tall to need parenthesis but not too short to add parenthesis to your Sigma.
And guess what will be our default argument: Sigma
... P
The code (containing some tests) is this:
documentclassarticle
defDefLowArg$Sigma$
letoldDefLowArgDefLowArg
newsaveboxmyAbox
newsaveboxmyBbox
newcommandlow[2][DefLowArg]saveboxmyAboxvbox#1saveboxmyBboxvboxensuremath#2
ifdimdimexprhtmyAbox+dpmyAbox<dimexprhtmyBbox+dpmyBboxrelax
left(#2right)_l_mathcalA
else #2_l_mathcalAfi
begindocument
[lowlowSigma]
[lowSigma]
[
lowsum_i=3^5 F(x)
]
[
lowfracF(x)x+5
]
[lowF_x]
[low[1/4]F(x)]
[lowx^2]
[
lowG_x
]
These commands may be should add without parentheses
[
lowg(z)
]
[
lowF(x)
]
bfseries Solution 1 Add an tall optional argument in the command like: verb
[
low[/]g(z)
]
[
low[/]F(x)
]
):
xdefDefLowArg/
[
lowg(z)
]
[
lowF(x)
]
letDefLowArgoldDefLowArg
bfseries And back to default
[
lowF(X)
]
enddocument
That produces:
PS:Of course manual solutions should be added in special cases but anyway in your command I am sure you would have exceptions for many cases.
answered 5 hours ago
koleygrkoleygr
12.5k11038
12.5k11038
add a comment |
add a comment |
Rincewind is a new contributor. Be nice, and check out our Code of Conduct.
Rincewind is a new contributor. Be nice, and check out our Code of Conduct.
Rincewind is a new contributor. Be nice, and check out our Code of Conduct.
Rincewind 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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f479070%2fdetecting-subscript-in-command-argument%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
-conditionals, ifthenelse, macros, subscripts, xifthen