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
A three room house but a three headED dog
MTG: Can I kill an opponent in response to lethal activated abilities, and not take the damage?
Is Gradient Descent central to every optimizer?
Extra alignment tab has been changed to cr. } using table, tabular and resizebox
Rejected in 4th interview round citing insufficient years of experience
Why is there a voltage between the mains ground and my radiator?
Why doesn't this Google Translate ad use the word "Translation" instead of "Translate"?
A question on the ultrafilter number
They call me Inspector Morse
BitNot does not flip bits in the way I expected
Append a note to one of three files based on user choice
Low budget alien movie about the Earth being cooked
What are some noteworthy "mic-drop" moments in math?
How do you like my writing?
Is "history" a male-biased word ("his+story")?
PTIJ: How can I halachically kill a vampire?
Could you please stop shuffling the deck and play already?
Unreachable code, but reachable with exception
Examples of a statistic that is not independent of sample's distribution?
Do Bugbears' arms literally get longer when it's their turn?
Who deserves to be first and second author? PhD student who collected data, research associate who wrote the paper or supervisor?
Why the color red for the Republican Party
Is there any way to damage Intellect Devourer(s) when already within a creature's skull?
Why would one plane in this picture not have gear down yet?
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
Rincewind is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
Rincewind is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
Rincewind is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
Rincewind is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Rincewind is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 12 hours ago
Rincewind
New contributor
Rincewind is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 15 hours ago
RincewindRincewind
113
113
New contributor
Rincewind is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Rincewind is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Rincewind is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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
12 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
12 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(nownestlowfalsenormallow(#1))elsenownestlowfalsenormallow(#1)fi%
ifnownestlow(nownestlowtruenormallow#1)elsenownestlowtruenormallow#1fi%
endgroup
makeatother
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad
lowlowSigma qquad
lowlowlowSigma qquad
]
[
lowSigma_b qquad
lowlowSigma_b qquad
lowlowlowSigma_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
10 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
6 hours 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)
]
[
low[/]g(z)
]
[
low[/]F(x)
]
letDefLowArgoldDefLowArg
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
12 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
12 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
12 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
12 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 13 hours ago
![](https://i.stack.imgur.com/wJWMb.png?s=32&g=1)
![](https://i.stack.imgur.com/wJWMb.png?s=32&g=1)
egregegreg
726k8819193228
726k8819193228
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
12 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
12 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
12 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
12 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
12 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
12 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
12 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
12 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(nownestlowfalsenormallow(#1))elsenownestlowfalsenormallow(#1)fi%
ifnownestlow(nownestlowtruenormallow#1)elsenownestlowtruenormallow#1fi%
endgroup
makeatother
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad
lowlowSigma qquad
lowlowlowSigma qquad
]
[
lowSigma_b qquad
lowlowSigma_b qquad
lowlowlowSigma_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
10 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
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(nownestlowfalsenormallow(#1))elsenownestlowfalsenormallow(#1)fi%
ifnownestlow(nownestlowtruenormallow#1)elsenownestlowtruenormallow#1fi%
endgroup
makeatother
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad
lowlowSigma qquad
lowlowlowSigma qquad
]
[
lowSigma_b qquad
lowlowSigma_b qquad
lowlowlowSigma_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
10 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
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(nownestlowfalsenormallow(#1))elsenownestlowfalsenormallow(#1)fi%
ifnownestlow(nownestlowtruenormallow#1)elsenownestlowtruenormallow#1fi%
endgroup
makeatother
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad
lowlowSigma qquad
lowlowlowSigma qquad
]
[
lowSigma_b qquad
lowlowSigma_b qquad
lowlowlowSigma_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(nownestlowfalsenormallow(#1))elsenownestlowfalsenormallow(#1)fi%
ifnownestlow(nownestlowtruenormallow#1)elsenownestlowtruenormallow#1fi%
endgroup
makeatother
newcommandnormallow[1]#1_l_mathcalA
begindocument
[
lowSigma qquad
lowlowSigma qquad
lowlowlowSigma qquad
]
[
lowSigma_b qquad
lowlowSigma_b qquad
lowlowlowSigma_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 6 hours ago
answered 11 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
10 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
6 hours 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
10 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
6 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
10 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
10 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
6 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
6 hours 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)
]
[
low[/]g(z)
]
[
low[/]F(x)
]
letDefLowArgoldDefLowArg
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)
]
[
low[/]g(z)
]
[
low[/]F(x)
]
letDefLowArgoldDefLowArg
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)
]
[
low[/]g(z)
]
[
low[/]F(x)
]
letDefLowArgoldDefLowArg
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)
]
[
low[/]g(z)
]
[
low[/]F(x)
]
letDefLowArgoldDefLowArg
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 11 hours ago
![](https://i.stack.imgur.com/x9cGT.jpg?s=32&g=1)
![](https://i.stack.imgur.com/x9cGT.jpg?s=32&g=1)
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