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













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




















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



















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


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


















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



          [
          low[/]g(z)
          ]
          [
          low[/]F(x)
          ]

          letDefLowArgoldDefLowArg

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
















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














            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 13 hours ago









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


















            • 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

















            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












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


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















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


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













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


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


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

















            • 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
















            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











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



            [
            low[/]g(z)
            ]
            [
            low[/]F(x)
            ]

            letDefLowArgoldDefLowArg

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



              [
              low[/]g(z)
              ]
              [
              low[/]F(x)
              ]

              letDefLowArgoldDefLowArg

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



                [
                low[/]g(z)
                ]
                [
                low[/]F(x)
                ]

                letDefLowArgoldDefLowArg

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



                [
                low[/]g(z)
                ]
                [
                low[/]F(x)
                ]

                letDefLowArgoldDefLowArg

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







                    Popular posts from this blog

                    Magento 2 duplicate PHPSESSID cookie when using session_start() in custom php scriptMagento 2: User cant logged in into to account page, no error showing!Magento duplicate on subdomainGrabbing storeview from cookie (after using language selector)How do I run php custom script on magento2Magento 2: Include PHP script in headerSession lock after using Cm_RedisSessionscript php to update stockMagento set cookie popupMagento 2 session id cookie - where to find it?How to import Configurable product from csv with custom attributes using php scriptMagento 2 run custom PHP script

                    Can not update quote_id field of “quote_item” table magento 2Magento 2.1 - We can't remove the item. (Shopping Cart doesnt allow us to remove items before becomes empty)Add value for custom quote item attribute using REST apiREST API endpoint v1/carts/cartId/items always returns error messageCorrect way to save entries to databaseHow to remove all associated quote objects of a customer completelyMagento 2 - Save value from custom input field to quote_itemGet quote_item data using quote id and product id filter in Magento 2How to set additional data to quote_item table from controller in Magento 2?What is the purpose of additional_data column in quote_item table in magento2Set Custom Price to Quote item magento2 from controller

                    How to solve knockout JS error in Magento 2 Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?(Magento2) knockout.js:3012 Uncaught ReferenceError: Unable to process bindingUnable to process binding Knockout.js magento 2Cannot read property `scopeLabel` of undefined on Product Detail PageCan't get Customer Data on frontend in Magento 2Magento2 Order Summary - unable to process bindingKO templates are not loading in Magento 2.1 applicationgetting knockout js error magento 2Product grid not load -— Unable to process binding Knockout.js magento 2Product form not loaded in magento2Uncaught ReferenceError: Unable to process binding “if: function()return (isShowLegend()) ” magento 2