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













2















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?










share|improve this question









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.
























    2















    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?










    share|improve this question









    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.






















      2












      2








      2








      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?










      share|improve this question









      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






      share|improve this question









      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.











      share|improve this question









      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.









      share|improve this question




      share|improve this question








      edited 5 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 9 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.




















          3 Answers
          3






          active

          oldest

          votes


















          3














          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.



          enter image description here






          share|improve this answer























          • 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



















          2














          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


          enter image description here



          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.






          share|improve this answer

























          • 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


















          1














          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:



          enter image description here



          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.






          share|improve this answer






















            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.









            draft saved

            draft discarded


















            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









            3














            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.



            enter image description here






            share|improve this answer























            • 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
















            3














            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.



            enter image description here






            share|improve this answer























            • 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














            3












            3








            3







            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.



            enter image description here






            share|improve this answer













            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.



            enter image description here







            share|improve this answer












            share|improve this answer



            share|improve this answer










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


















            • 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

















            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












            2














            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


            enter image description here



            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.






            share|improve this answer

























            • 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















            2














            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


            enter image description here



            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.






            share|improve this answer

























            • 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













            2












            2








            2







            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


            enter image description here



            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.






            share|improve this answer















            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


            enter image description here



            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








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

















            • 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
















            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











            1














            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:



            enter image description here



            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.






            share|improve this answer



























              1














              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:



              enter image description here



              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.






              share|improve this answer

























                1












                1








                1







                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:



                enter image description here



                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.






                share|improve this answer













                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:



                enter image description here



                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 5 hours ago









                koleygrkoleygr

                12.5k11038




                12.5k11038




















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









                    draft saved

                    draft discarded


















                    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.




                    draft saved


                    draft discarded














                    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





















































                    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

                    Popular posts from this blog

                    Frič See also Navigation menuinternal link

                    Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

                    fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox