How do I reset the current AtBeginDocument?











up vote
6
down vote

favorite












The following is actually a simplified version from a large job.



documentclass{memoir}

AtBeginDocument{First}

AtBeginDocument{Second}

AtBeginDocument{Third}

% I want to cancel the above at this point

AtBeginDocument{New First}

AtBeginDocument{New Second}

AtBeginDocument{New Third}

begin{document}

end{document}


I need to cancel the current AtBeginDocument accumulations at some midpoint (coming from another homegrown style file from which I need to use some of the macros) and then add some more.



How do I do this?










share|improve this question


























    up vote
    6
    down vote

    favorite












    The following is actually a simplified version from a large job.



    documentclass{memoir}

    AtBeginDocument{First}

    AtBeginDocument{Second}

    AtBeginDocument{Third}

    % I want to cancel the above at this point

    AtBeginDocument{New First}

    AtBeginDocument{New Second}

    AtBeginDocument{New Third}

    begin{document}

    end{document}


    I need to cancel the current AtBeginDocument accumulations at some midpoint (coming from another homegrown style file from which I need to use some of the macros) and then add some more.



    How do I do this?










    share|improve this question
























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      The following is actually a simplified version from a large job.



      documentclass{memoir}

      AtBeginDocument{First}

      AtBeginDocument{Second}

      AtBeginDocument{Third}

      % I want to cancel the above at this point

      AtBeginDocument{New First}

      AtBeginDocument{New Second}

      AtBeginDocument{New Third}

      begin{document}

      end{document}


      I need to cancel the current AtBeginDocument accumulations at some midpoint (coming from another homegrown style file from which I need to use some of the macros) and then add some more.



      How do I do this?










      share|improve this question













      The following is actually a simplified version from a large job.



      documentclass{memoir}

      AtBeginDocument{First}

      AtBeginDocument{Second}

      AtBeginDocument{Third}

      % I want to cancel the above at this point

      AtBeginDocument{New First}

      AtBeginDocument{New Second}

      AtBeginDocument{New Third}

      begin{document}

      end{document}


      I need to cancel the current AtBeginDocument accumulations at some midpoint (coming from another homegrown style file from which I need to use some of the macros) and then add some more.



      How do I do this?







      macros






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      Masroor

      11k64685




      11k64685






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          7
          down vote













          AtBeginDocument just adds (appends) code to the 'hook' @begindocumenthook, at the beginning of the document the collected code is executed.



          In theory you can clear the hook at any time with



          makeatletter
          let@begindocumenthook@empty
          makeatother


          but that might be a bit risky since many packages expect AtBeginDocument to work properly.



          If there are only a few AtBeginDocuments you want to skip over, it might be safer to disable the command temporarily with



          makeatletter
          letAtBeginDocument@gobble
          makeatother


          Of course you can also save the contents of the hook



          makeatletter
          let@masroor@saved@begindocumenthook@begindocumenthook
          makeatother


          and then after a few AtBeginDocuments you want to ignore restore it again



          makeatletter
          let@begindocumenthook@masroor@saved@begindocumenthook
          makeatother





          share|improve this answer























          • Tried this, does not seem to work. Previous accumulations are retained.
            – Masroor
            1 hour ago










          • @Masroor relax was not a good idea, try @empty instead (see edit).
            – moewe
            1 hour ago










          • Generates, '! LaTeX Error: Missing begin{document}.'
            – Masroor
            1 hour ago










          • @Masroor Not for me in the MWE above, but it may well be that some code you try to add to the hook causes such problems. I suggest you ask a new question about that with a suitable MWE.
            – moewe
            1 hour ago






          • 1




            Don't write letAtBeginDocument@empty. Write let@begindocumenthook@empty to reset the hook or letAtBeginDocument@gobble to disable AtBeginDocument completely.
            – moewe
            1 hour ago


















          up vote
          1
          down vote













          I'm not sure I understand your use case precisely. Left to myself, I'd be hugely reluctant to fiddle with AtBeginDocument (or @begindocumenthook) directly, because lots of packages (and the internal workings of LaTeX itself) use them for various purposes, and I don't see how I could be sure that I wouldn't break something unintentionally.



          Might it be better to define a separate accumulator macro of your own, and then to have that executed AtBeginDocument? You can then safely mess with that macro to your heart's content, without clobbering anything else that has been innocently making use of AtBeginDocument.



          Something like:



          documentclass{memoir}

          makeatletter
          defMyAtBeginDocument{g@addto@macromy@begindocumenthook}
          letmy@begindocumenthook@empty
          defMyResetBeginDocument{globalletmy@begindocumenthook@empty}
          AtBeginDocument{my@begindocumenthook}
          makeatother

          AtBeginDocument{THATpar}
          MyAtBeginDocument{NOT THISpar}
          MyResetBeginDocument
          MyAtBeginDocument{THISpar}

          begin{document}
          We should see THIS then THAT!
          end{document}


          Note that THIS comes before THAT because my@begindocument is executed as part of @begindocumenthook before the additional material added by AtBeginDocument{THATpar} because it comes in via my@begindocumenthook which in turn got added to @begindocumenthook first. You might need to (and could) delay that addition until later.






          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',
            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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f464682%2fhow-do-i-reset-the-current-atbegindocument%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            7
            down vote













            AtBeginDocument just adds (appends) code to the 'hook' @begindocumenthook, at the beginning of the document the collected code is executed.



            In theory you can clear the hook at any time with



            makeatletter
            let@begindocumenthook@empty
            makeatother


            but that might be a bit risky since many packages expect AtBeginDocument to work properly.



            If there are only a few AtBeginDocuments you want to skip over, it might be safer to disable the command temporarily with



            makeatletter
            letAtBeginDocument@gobble
            makeatother


            Of course you can also save the contents of the hook



            makeatletter
            let@masroor@saved@begindocumenthook@begindocumenthook
            makeatother


            and then after a few AtBeginDocuments you want to ignore restore it again



            makeatletter
            let@begindocumenthook@masroor@saved@begindocumenthook
            makeatother





            share|improve this answer























            • Tried this, does not seem to work. Previous accumulations are retained.
              – Masroor
              1 hour ago










            • @Masroor relax was not a good idea, try @empty instead (see edit).
              – moewe
              1 hour ago










            • Generates, '! LaTeX Error: Missing begin{document}.'
              – Masroor
              1 hour ago










            • @Masroor Not for me in the MWE above, but it may well be that some code you try to add to the hook causes such problems. I suggest you ask a new question about that with a suitable MWE.
              – moewe
              1 hour ago






            • 1




              Don't write letAtBeginDocument@empty. Write let@begindocumenthook@empty to reset the hook or letAtBeginDocument@gobble to disable AtBeginDocument completely.
              – moewe
              1 hour ago















            up vote
            7
            down vote













            AtBeginDocument just adds (appends) code to the 'hook' @begindocumenthook, at the beginning of the document the collected code is executed.



            In theory you can clear the hook at any time with



            makeatletter
            let@begindocumenthook@empty
            makeatother


            but that might be a bit risky since many packages expect AtBeginDocument to work properly.



            If there are only a few AtBeginDocuments you want to skip over, it might be safer to disable the command temporarily with



            makeatletter
            letAtBeginDocument@gobble
            makeatother


            Of course you can also save the contents of the hook



            makeatletter
            let@masroor@saved@begindocumenthook@begindocumenthook
            makeatother


            and then after a few AtBeginDocuments you want to ignore restore it again



            makeatletter
            let@begindocumenthook@masroor@saved@begindocumenthook
            makeatother





            share|improve this answer























            • Tried this, does not seem to work. Previous accumulations are retained.
              – Masroor
              1 hour ago










            • @Masroor relax was not a good idea, try @empty instead (see edit).
              – moewe
              1 hour ago










            • Generates, '! LaTeX Error: Missing begin{document}.'
              – Masroor
              1 hour ago










            • @Masroor Not for me in the MWE above, but it may well be that some code you try to add to the hook causes such problems. I suggest you ask a new question about that with a suitable MWE.
              – moewe
              1 hour ago






            • 1




              Don't write letAtBeginDocument@empty. Write let@begindocumenthook@empty to reset the hook or letAtBeginDocument@gobble to disable AtBeginDocument completely.
              – moewe
              1 hour ago













            up vote
            7
            down vote










            up vote
            7
            down vote









            AtBeginDocument just adds (appends) code to the 'hook' @begindocumenthook, at the beginning of the document the collected code is executed.



            In theory you can clear the hook at any time with



            makeatletter
            let@begindocumenthook@empty
            makeatother


            but that might be a bit risky since many packages expect AtBeginDocument to work properly.



            If there are only a few AtBeginDocuments you want to skip over, it might be safer to disable the command temporarily with



            makeatletter
            letAtBeginDocument@gobble
            makeatother


            Of course you can also save the contents of the hook



            makeatletter
            let@masroor@saved@begindocumenthook@begindocumenthook
            makeatother


            and then after a few AtBeginDocuments you want to ignore restore it again



            makeatletter
            let@begindocumenthook@masroor@saved@begindocumenthook
            makeatother





            share|improve this answer














            AtBeginDocument just adds (appends) code to the 'hook' @begindocumenthook, at the beginning of the document the collected code is executed.



            In theory you can clear the hook at any time with



            makeatletter
            let@begindocumenthook@empty
            makeatother


            but that might be a bit risky since many packages expect AtBeginDocument to work properly.



            If there are only a few AtBeginDocuments you want to skip over, it might be safer to disable the command temporarily with



            makeatletter
            letAtBeginDocument@gobble
            makeatother


            Of course you can also save the contents of the hook



            makeatletter
            let@masroor@saved@begindocumenthook@begindocumenthook
            makeatother


            and then after a few AtBeginDocuments you want to ignore restore it again



            makeatletter
            let@begindocumenthook@masroor@saved@begindocumenthook
            makeatother






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 54 mins ago

























            answered 1 hour ago









            moewe

            84.7k9108325




            84.7k9108325












            • Tried this, does not seem to work. Previous accumulations are retained.
              – Masroor
              1 hour ago










            • @Masroor relax was not a good idea, try @empty instead (see edit).
              – moewe
              1 hour ago










            • Generates, '! LaTeX Error: Missing begin{document}.'
              – Masroor
              1 hour ago










            • @Masroor Not for me in the MWE above, but it may well be that some code you try to add to the hook causes such problems. I suggest you ask a new question about that with a suitable MWE.
              – moewe
              1 hour ago






            • 1




              Don't write letAtBeginDocument@empty. Write let@begindocumenthook@empty to reset the hook or letAtBeginDocument@gobble to disable AtBeginDocument completely.
              – moewe
              1 hour ago


















            • Tried this, does not seem to work. Previous accumulations are retained.
              – Masroor
              1 hour ago










            • @Masroor relax was not a good idea, try @empty instead (see edit).
              – moewe
              1 hour ago










            • Generates, '! LaTeX Error: Missing begin{document}.'
              – Masroor
              1 hour ago










            • @Masroor Not for me in the MWE above, but it may well be that some code you try to add to the hook causes such problems. I suggest you ask a new question about that with a suitable MWE.
              – moewe
              1 hour ago






            • 1




              Don't write letAtBeginDocument@empty. Write let@begindocumenthook@empty to reset the hook or letAtBeginDocument@gobble to disable AtBeginDocument completely.
              – moewe
              1 hour ago
















            Tried this, does not seem to work. Previous accumulations are retained.
            – Masroor
            1 hour ago




            Tried this, does not seem to work. Previous accumulations are retained.
            – Masroor
            1 hour ago












            @Masroor relax was not a good idea, try @empty instead (see edit).
            – moewe
            1 hour ago




            @Masroor relax was not a good idea, try @empty instead (see edit).
            – moewe
            1 hour ago












            Generates, '! LaTeX Error: Missing begin{document}.'
            – Masroor
            1 hour ago




            Generates, '! LaTeX Error: Missing begin{document}.'
            – Masroor
            1 hour ago












            @Masroor Not for me in the MWE above, but it may well be that some code you try to add to the hook causes such problems. I suggest you ask a new question about that with a suitable MWE.
            – moewe
            1 hour ago




            @Masroor Not for me in the MWE above, but it may well be that some code you try to add to the hook causes such problems. I suggest you ask a new question about that with a suitable MWE.
            – moewe
            1 hour ago




            1




            1




            Don't write letAtBeginDocument@empty. Write let@begindocumenthook@empty to reset the hook or letAtBeginDocument@gobble to disable AtBeginDocument completely.
            – moewe
            1 hour ago




            Don't write letAtBeginDocument@empty. Write let@begindocumenthook@empty to reset the hook or letAtBeginDocument@gobble to disable AtBeginDocument completely.
            – moewe
            1 hour ago










            up vote
            1
            down vote













            I'm not sure I understand your use case precisely. Left to myself, I'd be hugely reluctant to fiddle with AtBeginDocument (or @begindocumenthook) directly, because lots of packages (and the internal workings of LaTeX itself) use them for various purposes, and I don't see how I could be sure that I wouldn't break something unintentionally.



            Might it be better to define a separate accumulator macro of your own, and then to have that executed AtBeginDocument? You can then safely mess with that macro to your heart's content, without clobbering anything else that has been innocently making use of AtBeginDocument.



            Something like:



            documentclass{memoir}

            makeatletter
            defMyAtBeginDocument{g@addto@macromy@begindocumenthook}
            letmy@begindocumenthook@empty
            defMyResetBeginDocument{globalletmy@begindocumenthook@empty}
            AtBeginDocument{my@begindocumenthook}
            makeatother

            AtBeginDocument{THATpar}
            MyAtBeginDocument{NOT THISpar}
            MyResetBeginDocument
            MyAtBeginDocument{THISpar}

            begin{document}
            We should see THIS then THAT!
            end{document}


            Note that THIS comes before THAT because my@begindocument is executed as part of @begindocumenthook before the additional material added by AtBeginDocument{THATpar} because it comes in via my@begindocumenthook which in turn got added to @begindocumenthook first. You might need to (and could) delay that addition until later.






            share|improve this answer

























              up vote
              1
              down vote













              I'm not sure I understand your use case precisely. Left to myself, I'd be hugely reluctant to fiddle with AtBeginDocument (or @begindocumenthook) directly, because lots of packages (and the internal workings of LaTeX itself) use them for various purposes, and I don't see how I could be sure that I wouldn't break something unintentionally.



              Might it be better to define a separate accumulator macro of your own, and then to have that executed AtBeginDocument? You can then safely mess with that macro to your heart's content, without clobbering anything else that has been innocently making use of AtBeginDocument.



              Something like:



              documentclass{memoir}

              makeatletter
              defMyAtBeginDocument{g@addto@macromy@begindocumenthook}
              letmy@begindocumenthook@empty
              defMyResetBeginDocument{globalletmy@begindocumenthook@empty}
              AtBeginDocument{my@begindocumenthook}
              makeatother

              AtBeginDocument{THATpar}
              MyAtBeginDocument{NOT THISpar}
              MyResetBeginDocument
              MyAtBeginDocument{THISpar}

              begin{document}
              We should see THIS then THAT!
              end{document}


              Note that THIS comes before THAT because my@begindocument is executed as part of @begindocumenthook before the additional material added by AtBeginDocument{THATpar} because it comes in via my@begindocumenthook which in turn got added to @begindocumenthook first. You might need to (and could) delay that addition until later.






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                I'm not sure I understand your use case precisely. Left to myself, I'd be hugely reluctant to fiddle with AtBeginDocument (or @begindocumenthook) directly, because lots of packages (and the internal workings of LaTeX itself) use them for various purposes, and I don't see how I could be sure that I wouldn't break something unintentionally.



                Might it be better to define a separate accumulator macro of your own, and then to have that executed AtBeginDocument? You can then safely mess with that macro to your heart's content, without clobbering anything else that has been innocently making use of AtBeginDocument.



                Something like:



                documentclass{memoir}

                makeatletter
                defMyAtBeginDocument{g@addto@macromy@begindocumenthook}
                letmy@begindocumenthook@empty
                defMyResetBeginDocument{globalletmy@begindocumenthook@empty}
                AtBeginDocument{my@begindocumenthook}
                makeatother

                AtBeginDocument{THATpar}
                MyAtBeginDocument{NOT THISpar}
                MyResetBeginDocument
                MyAtBeginDocument{THISpar}

                begin{document}
                We should see THIS then THAT!
                end{document}


                Note that THIS comes before THAT because my@begindocument is executed as part of @begindocumenthook before the additional material added by AtBeginDocument{THATpar} because it comes in via my@begindocumenthook which in turn got added to @begindocumenthook first. You might need to (and could) delay that addition until later.






                share|improve this answer












                I'm not sure I understand your use case precisely. Left to myself, I'd be hugely reluctant to fiddle with AtBeginDocument (or @begindocumenthook) directly, because lots of packages (and the internal workings of LaTeX itself) use them for various purposes, and I don't see how I could be sure that I wouldn't break something unintentionally.



                Might it be better to define a separate accumulator macro of your own, and then to have that executed AtBeginDocument? You can then safely mess with that macro to your heart's content, without clobbering anything else that has been innocently making use of AtBeginDocument.



                Something like:



                documentclass{memoir}

                makeatletter
                defMyAtBeginDocument{g@addto@macromy@begindocumenthook}
                letmy@begindocumenthook@empty
                defMyResetBeginDocument{globalletmy@begindocumenthook@empty}
                AtBeginDocument{my@begindocumenthook}
                makeatother

                AtBeginDocument{THATpar}
                MyAtBeginDocument{NOT THISpar}
                MyResetBeginDocument
                MyAtBeginDocument{THISpar}

                begin{document}
                We should see THIS then THAT!
                end{document}


                Note that THIS comes before THAT because my@begindocument is executed as part of @begindocumenthook before the additional material added by AtBeginDocument{THATpar} because it comes in via my@begindocumenthook which in turn got added to @begindocumenthook first. You might need to (and could) delay that addition until later.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 19 mins ago









                Paul Stanley

                13.8k42745




                13.8k42745






























                    draft saved

                    draft discarded




















































                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f464682%2fhow-do-i-reset-the-current-atbegindocument%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

                    What visual should I use to simply compare current year value vs last year in Power BI desktop

                    How to ignore python UserWarning in pytest?

                    Alexandru Averescu