What are the values for parameters of an edef at the time of definition?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite
1












Suppose we have the following short document



documentclassarticle

deffoo#1if#1XXelse not Xfi

edefbar#1if#1XXelse not Xfi

begindocument
foo: X = fooX, Y = fooY

bar: X = barX, Y = barY
enddocument


which results in




foo: X = X, Y = not X

bar: X = not X, Y = not X




The first case is quite simple to understand, expansion of foo happens at the time the macro is called, #1 is replaced by X or Y, and the if is executed with either of these two characters.



What is not so clear is the result of bar. I understand that the expansion of if here happens at the time the macro is defined and thus the concrete value of #1 not yet available; TeX leaves "holes" for the parameters in the expanded replacement text to be filled in later when the macro is actually called. But to properly expand the if here, some value for #1 is needed.



The parameter doesn't seem to be just left empty, otherwise bar shouldn't give not X, because if XX would always be true. They also don't seem to be relaxed, as



edefbaz#1ifx#1relax yeselse nofi


always gives no.



Then what values are used for the parameters at the time of definition?










share|improve this question

























    up vote
    4
    down vote

    favorite
    1












    Suppose we have the following short document



    documentclassarticle

    deffoo#1if#1XXelse not Xfi

    edefbar#1if#1XXelse not Xfi

    begindocument
    foo: X = fooX, Y = fooY

    bar: X = barX, Y = barY
    enddocument


    which results in




    foo: X = X, Y = not X

    bar: X = not X, Y = not X




    The first case is quite simple to understand, expansion of foo happens at the time the macro is called, #1 is replaced by X or Y, and the if is executed with either of these two characters.



    What is not so clear is the result of bar. I understand that the expansion of if here happens at the time the macro is defined and thus the concrete value of #1 not yet available; TeX leaves "holes" for the parameters in the expanded replacement text to be filled in later when the macro is actually called. But to properly expand the if here, some value for #1 is needed.



    The parameter doesn't seem to be just left empty, otherwise bar shouldn't give not X, because if XX would always be true. They also don't seem to be relaxed, as



    edefbaz#1ifx#1relax yeselse nofi


    always gives no.



    Then what values are used for the parameters at the time of definition?










    share|improve this question























      up vote
      4
      down vote

      favorite
      1









      up vote
      4
      down vote

      favorite
      1






      1





      Suppose we have the following short document



      documentclassarticle

      deffoo#1if#1XXelse not Xfi

      edefbar#1if#1XXelse not Xfi

      begindocument
      foo: X = fooX, Y = fooY

      bar: X = barX, Y = barY
      enddocument


      which results in




      foo: X = X, Y = not X

      bar: X = not X, Y = not X




      The first case is quite simple to understand, expansion of foo happens at the time the macro is called, #1 is replaced by X or Y, and the if is executed with either of these two characters.



      What is not so clear is the result of bar. I understand that the expansion of if here happens at the time the macro is defined and thus the concrete value of #1 not yet available; TeX leaves "holes" for the parameters in the expanded replacement text to be filled in later when the macro is actually called. But to properly expand the if here, some value for #1 is needed.



      The parameter doesn't seem to be just left empty, otherwise bar shouldn't give not X, because if XX would always be true. They also don't seem to be relaxed, as



      edefbaz#1ifx#1relax yeselse nofi


      always gives no.



      Then what values are used for the parameters at the time of definition?










      share|improve this question













      Suppose we have the following short document



      documentclassarticle

      deffoo#1if#1XXelse not Xfi

      edefbar#1if#1XXelse not Xfi

      begindocument
      foo: X = fooX, Y = fooY

      bar: X = barX, Y = barY
      enddocument


      which results in




      foo: X = X, Y = not X

      bar: X = not X, Y = not X




      The first case is quite simple to understand, expansion of foo happens at the time the macro is called, #1 is replaced by X or Y, and the if is executed with either of these two characters.



      What is not so clear is the result of bar. I understand that the expansion of if here happens at the time the macro is defined and thus the concrete value of #1 not yet available; TeX leaves "holes" for the parameters in the expanded replacement text to be filled in later when the macro is actually called. But to properly expand the if here, some value for #1 is needed.



      The parameter doesn't seem to be just left empty, otherwise bar shouldn't give not X, because if XX would always be true. They also don't seem to be relaxed, as



      edefbaz#1ifx#1relax yeselse nofi


      always gives no.



      Then what values are used for the parameters at the time of definition?







      tex-core expansion






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      siracusa

      3,6021926




      3,6021926




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote













          When TeX does edef<macro><parameter text>1<replacement text>2 it sets aside the <macro> the replacement text and 1, then does full expansion to <replacement text> until finding the matching 2. Finally it does



          def<macro><parameter text><full expansion of the replacement text>


          The full expansion of if#1XXelse not Xfi is not X, because # and 1 are different character tokens (by character code).



          Thus your edef becomes the same as



          defbar#1not X


          As another example,



          edefrightbracestringstring}


          is perfectly legal, even if it appears to have unbalanced braces, because when the expansion is being done, the first } has already been tokenized as }12 when encountered. This proves that TeX doesn't first absorb the whole <replacement text>, but performs expansion just like in normal circumstances, with the difference that the matching }2 stops the process and resumes the assignment.






          share|improve this answer



























            up vote
            2
            down vote













            I would say that it compares # with 1 hance always goes for the false branch. (Compares second test where it compares # with # and chooses true branch)



            edefbar#1if#1#1 (TRUE)else #1 (FALSE)fi

            showbar


            edefbar#1if##1 (TRUE)else (FALSE)fi

            showbar

            bye


            Gives



            > bar=macro:
            #1->#1 (FALSE).
            l.3 showbar

            > bar=macro:
            #1->1 (TRUE).
            l.8 showbar





            share|improve this answer




















            • try also edefbar#1if###1 (TRUE)else (FALSE)fi, bar X.
              – jfbu
              58 mins ago










            • Yes, if one tries (with pdftex) edefbar#1unlessif#1XXelse not Xfi, showbar will output that bar is a macro doing #1->XX.
              – egreg
              46 mins ago










            • @egreg good test case indeed.
              – jfbu
              40 mins ago










            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "85"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f452947%2fwhat-are-the-values-for-parameters-of-an-edef-at-the-time-of-definition%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote













            When TeX does edef<macro><parameter text>1<replacement text>2 it sets aside the <macro> the replacement text and 1, then does full expansion to <replacement text> until finding the matching 2. Finally it does



            def<macro><parameter text><full expansion of the replacement text>


            The full expansion of if#1XXelse not Xfi is not X, because # and 1 are different character tokens (by character code).



            Thus your edef becomes the same as



            defbar#1not X


            As another example,



            edefrightbracestringstring}


            is perfectly legal, even if it appears to have unbalanced braces, because when the expansion is being done, the first } has already been tokenized as }12 when encountered. This proves that TeX doesn't first absorb the whole <replacement text>, but performs expansion just like in normal circumstances, with the difference that the matching }2 stops the process and resumes the assignment.






            share|improve this answer
























              up vote
              3
              down vote













              When TeX does edef<macro><parameter text>1<replacement text>2 it sets aside the <macro> the replacement text and 1, then does full expansion to <replacement text> until finding the matching 2. Finally it does



              def<macro><parameter text><full expansion of the replacement text>


              The full expansion of if#1XXelse not Xfi is not X, because # and 1 are different character tokens (by character code).



              Thus your edef becomes the same as



              defbar#1not X


              As another example,



              edefrightbracestringstring}


              is perfectly legal, even if it appears to have unbalanced braces, because when the expansion is being done, the first } has already been tokenized as }12 when encountered. This proves that TeX doesn't first absorb the whole <replacement text>, but performs expansion just like in normal circumstances, with the difference that the matching }2 stops the process and resumes the assignment.






              share|improve this answer






















                up vote
                3
                down vote










                up vote
                3
                down vote









                When TeX does edef<macro><parameter text>1<replacement text>2 it sets aside the <macro> the replacement text and 1, then does full expansion to <replacement text> until finding the matching 2. Finally it does



                def<macro><parameter text><full expansion of the replacement text>


                The full expansion of if#1XXelse not Xfi is not X, because # and 1 are different character tokens (by character code).



                Thus your edef becomes the same as



                defbar#1not X


                As another example,



                edefrightbracestringstring}


                is perfectly legal, even if it appears to have unbalanced braces, because when the expansion is being done, the first } has already been tokenized as }12 when encountered. This proves that TeX doesn't first absorb the whole <replacement text>, but performs expansion just like in normal circumstances, with the difference that the matching }2 stops the process and resumes the assignment.






                share|improve this answer












                When TeX does edef<macro><parameter text>1<replacement text>2 it sets aside the <macro> the replacement text and 1, then does full expansion to <replacement text> until finding the matching 2. Finally it does



                def<macro><parameter text><full expansion of the replacement text>


                The full expansion of if#1XXelse not Xfi is not X, because # and 1 are different character tokens (by character code).



                Thus your edef becomes the same as



                defbar#1not X


                As another example,



                edefrightbracestringstring}


                is perfectly legal, even if it appears to have unbalanced braces, because when the expansion is being done, the first } has already been tokenized as }12 when encountered. This proves that TeX doesn't first absorb the whole <replacement text>, but performs expansion just like in normal circumstances, with the difference that the matching }2 stops the process and resumes the assignment.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 30 mins ago









                egreg

                685k8418273077




                685k8418273077




















                    up vote
                    2
                    down vote













                    I would say that it compares # with 1 hance always goes for the false branch. (Compares second test where it compares # with # and chooses true branch)



                    edefbar#1if#1#1 (TRUE)else #1 (FALSE)fi

                    showbar


                    edefbar#1if##1 (TRUE)else (FALSE)fi

                    showbar

                    bye


                    Gives



                    > bar=macro:
                    #1->#1 (FALSE).
                    l.3 showbar

                    > bar=macro:
                    #1->1 (TRUE).
                    l.8 showbar





                    share|improve this answer




















                    • try also edefbar#1if###1 (TRUE)else (FALSE)fi, bar X.
                      – jfbu
                      58 mins ago










                    • Yes, if one tries (with pdftex) edefbar#1unlessif#1XXelse not Xfi, showbar will output that bar is a macro doing #1->XX.
                      – egreg
                      46 mins ago










                    • @egreg good test case indeed.
                      – jfbu
                      40 mins ago














                    up vote
                    2
                    down vote













                    I would say that it compares # with 1 hance always goes for the false branch. (Compares second test where it compares # with # and chooses true branch)



                    edefbar#1if#1#1 (TRUE)else #1 (FALSE)fi

                    showbar


                    edefbar#1if##1 (TRUE)else (FALSE)fi

                    showbar

                    bye


                    Gives



                    > bar=macro:
                    #1->#1 (FALSE).
                    l.3 showbar

                    > bar=macro:
                    #1->1 (TRUE).
                    l.8 showbar





                    share|improve this answer




















                    • try also edefbar#1if###1 (TRUE)else (FALSE)fi, bar X.
                      – jfbu
                      58 mins ago










                    • Yes, if one tries (with pdftex) edefbar#1unlessif#1XXelse not Xfi, showbar will output that bar is a macro doing #1->XX.
                      – egreg
                      46 mins ago










                    • @egreg good test case indeed.
                      – jfbu
                      40 mins ago












                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    I would say that it compares # with 1 hance always goes for the false branch. (Compares second test where it compares # with # and chooses true branch)



                    edefbar#1if#1#1 (TRUE)else #1 (FALSE)fi

                    showbar


                    edefbar#1if##1 (TRUE)else (FALSE)fi

                    showbar

                    bye


                    Gives



                    > bar=macro:
                    #1->#1 (FALSE).
                    l.3 showbar

                    > bar=macro:
                    #1->1 (TRUE).
                    l.8 showbar





                    share|improve this answer












                    I would say that it compares # with 1 hance always goes for the false branch. (Compares second test where it compares # with # and chooses true branch)



                    edefbar#1if#1#1 (TRUE)else #1 (FALSE)fi

                    showbar


                    edefbar#1if##1 (TRUE)else (FALSE)fi

                    showbar

                    bye


                    Gives



                    > bar=macro:
                    #1->#1 (FALSE).
                    l.3 showbar

                    > bar=macro:
                    #1->1 (TRUE).
                    l.8 showbar






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 1 hour ago









                    jfbu

                    42.3k63136




                    42.3k63136











                    • try also edefbar#1if###1 (TRUE)else (FALSE)fi, bar X.
                      – jfbu
                      58 mins ago










                    • Yes, if one tries (with pdftex) edefbar#1unlessif#1XXelse not Xfi, showbar will output that bar is a macro doing #1->XX.
                      – egreg
                      46 mins ago










                    • @egreg good test case indeed.
                      – jfbu
                      40 mins ago
















                    • try also edefbar#1if###1 (TRUE)else (FALSE)fi, bar X.
                      – jfbu
                      58 mins ago










                    • Yes, if one tries (with pdftex) edefbar#1unlessif#1XXelse not Xfi, showbar will output that bar is a macro doing #1->XX.
                      – egreg
                      46 mins ago










                    • @egreg good test case indeed.
                      – jfbu
                      40 mins ago















                    try also edefbar#1if###1 (TRUE)else (FALSE)fi, bar X.
                    – jfbu
                    58 mins ago




                    try also edefbar#1if###1 (TRUE)else (FALSE)fi, bar X.
                    – jfbu
                    58 mins ago












                    Yes, if one tries (with pdftex) edefbar#1unlessif#1XXelse not Xfi, showbar will output that bar is a macro doing #1->XX.
                    – egreg
                    46 mins ago




                    Yes, if one tries (with pdftex) edefbar#1unlessif#1XXelse not Xfi, showbar will output that bar is a macro doing #1->XX.
                    – egreg
                    46 mins ago












                    @egreg good test case indeed.
                    – jfbu
                    40 mins ago




                    @egreg good test case indeed.
                    – jfbu
                    40 mins ago

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f452947%2fwhat-are-the-values-for-parameters-of-an-edef-at-the-time-of-definition%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    Installing NextGIS Connect into QGIS 3?

                    One-line joke