How do I reset the current AtBeginDocument?











up vote
12
down vote

favorite
1












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
    12
    down vote

    favorite
    1












    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
      12
      down vote

      favorite
      1









      up vote
      12
      down vote

      favorite
      1






      1





      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 Dec 13 at 12:27









      Masroor

      11k64686




      11k64686






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          9
          down vote



          accepted










          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 particularly liked the idea of not messing with @begindocumenthook. Otherwise, packages innocently using AtBeginDocument would never know what hit them. For my scenario, your solution turned out to be the most suitable one. The one from Peter Grill was a very close second. There is no special reason behind this, I had started working on your's first.
            – Masroor
            Dec 14 at 2:10


















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























          • Your solution is a very nice one. It also let me understand how exactly AtBeginDocument works. But eventually I failed to use it since in the homegrown style file, many packages were innocently using AtBeginDocument. And not all can be ignored.
            – Masroor
            Dec 13 at 18:00




















          up vote
          4
          down vote













          This is similar to Paul Stanley's answer but with slightly different UI. This
          defines AllowAtBeginDocumentToBeResetable which makes all subsequent uses of AtBeginDocument resetable. To reset these you invoke ClearResetableAtBeginDocumentList.
          Thus your example code would look like:



          AllowAtBeginDocumentToBeResetable%% ALL EXTERNAL PACKAGES INCLUDED BEFORE HERE

          AtBeginDocument{Firstpar}
          AtBeginDocument{Secondpar}
          AtBeginDocument{Thirdpar}

          ClearResetableAtBeginDocumentList% I want to cancel the above at this point

          AtBeginDocument{New Firstpar}
          AtBeginDocument{New Secondpar}
          AtBeginDocument{New Thirdpar}


          which produces:



          enter image description here



          Notes:




          • Note that this requires that all external packages MUST be included before you issue AllowAtBeginDocumentToBeResetable.


          Code:



          documentclass{memoir}

          makeatletter
          let@OldAtBeginDocumentAtBeginDocument
          newcommand{@ResetableBegindDoumentItems}{}%
          newcommand{AllowAtBeginDocumentToBeResetable}{%
          ifdefined@AtBeginDocumentIncludesResetableListelse
          gdef@AtBeginDocumentIncludesResetableList{}% Don't execute this again
          AtBeginDocument{@ResetableBegindDoumentItems}%
          fi
          renewcommand{AtBeginDocument}[1]{%
          g@addto@macro@ResetableBegindDoumentItems{{##1}}%
          }%
          }
          newcommand*{ClearResetableAtBeginDocumentList}{%
          gdef@ResetableBegindDoumentItems{}%
          }
          makeatother


          AllowAtBeginDocumentToBeResetable%% ALL EXTERNAL PACKAGES INCLUDED BEFORE HERE

          AtBeginDocument{Firstpar}
          AtBeginDocument{Secondpar}
          AtBeginDocument{Thirdpar}

          ClearResetableAtBeginDocumentList% I want to cancel the above at this point

          AtBeginDocument{New Firstpar}
          AtBeginDocument{New Secondpar}
          AtBeginDocument{New Thirdpar}

          begin{document}

          end{document}





          share|improve this answer




























            up vote
            4
            down vote













            I can offer a few routines for introducing sub-hooks within the AtBeginDocument-hook and maintaining them.



            1) AtBeginDocumentSubHook



            Syntax:



            AtBeginDocumentSubHook{⟨name of AtBeginDocument-sub-hook⟩}%
            {⟨tokens⟩}%


            Adds a new AtBeginDocument-sub-hook whose name is ⟨name of AtBeginDocument-sub-hook⟩ to the AtBeginDocument-hook and defines that AtBeginDocument-sub-hook to deliver ⟨tokens⟩.



            2) AddToContentOfAtBeginDocumentSubHook



            Syntax:



            AddToContentOfAtBeginDocumentSubHook{⟨name of AtBeginDocument-sub-hook⟩}%
            {⟨prepend-tokens⟩}%
            {⟨append-tokens⟩}%


            Redefines the AtBeginDocument-sub-hook whose name is ⟨name of AtBeginDocument-sub-hook⟩ to additionally deliver ⟨prepend-tokens⟩ in front of the tokens that are already in this sub-hook and to additionally deliver ⟨append-tokens⟩ behind the tokens that are already in this sub-hook.



            3) ReplaceContentOfAtBeginDocumentSubHook



            Syntax:



            ReplaceContentOfAtBeginDocumentSubHook{⟨name of AtBeginDocument-sub-hook⟩}%
            {⟨tokens⟩}%


            Redefines the AtBeginDocument-sub-hook whose name is ⟨name of AtBeginDocument-sub-hook⟩ to deliver ⟨tokens⟩.



            4) RemoveAtBeginDocumentSubHook



            Syntax:



            RemoveAtBeginDocumentSubHook{⟨name of AtBeginDocument-sub-hook⟩}%


            Removes the AtBeginDocument-sub-hook whose name is ⟨name of AtBeginDocument-sub-hook⟩ from the AtBeginDocument-hook.



            documentclass{memoir}

            makeatletter
            %%----------------------------------------------------------------------
            %% Paraphernalia
            %%----------------------------------------------------------------------
            newcommandUD@firstoftwo[2]{#1}%
            newcommandUD@secondoftwo[2]{#2}%
            newcommandUD@Exchange[2]{#2#1}%
            newcommandUD@name{}longdefUD@name#1#{romannumeral0UD@innername{#1}}%
            newcommandUD@innername[2]{%
            expandafterUD@Exchangeexpandafter{csname#2endcsname}{ #1}%
            }%
            %%----------------------------------------------------------------------
            %% Check whether argument is empty:
            %%......................................................................
            %% UD@CheckWhetherNull{<Argument which is to be checked>}%
            %% {<Tokens to be delivered in case that argument
            %% which is to be checked is empty>}%
            %% {<Tokens to be delivered in case that argument
            %% which is to be checked is not empty>}%
            %%
            %% The gist of this macro comes from Robert R. Schneck's ifempty-macro:
            %% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
            newcommandUD@CheckWhetherNull[1]{%
            romannumeral0expandafterUD@secondoftwostring{expandafter
            UD@secondoftwoexpandafter{expandafter{string#1}expandafter
            UD@secondoftwostring}expandafterUD@firstoftwoexpandafter{expandafter
            UD@secondoftwostring}expandafterexpandafterUD@firstoftwo{ }{}%
            UD@secondoftwo}{expandafterexpandafterUD@firstoftwo{ }{}UD@firstoftwo}%
            }%
            %%----------------------------------------------------------------------
            %% Remove AtBeginDocument-sub-hook from AtBeginDocument-hook:
            %%......................................................................
            %% RemoveAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}
            %% removes the AtBeginDocument-sub-hook whose name is
            %% <name of AtBeginDocument-sub-hook> from the AtBeginDocument-hook.
            newcommand*RemoveAtBeginDocumentSubHook[1]{%
            begingroup
            UD@namelongdefkeep##1{@begindocumentsubhook@#1}{##1}%
            UD@namelongdefremove##1{@begindocumentsubhook@#1}{}%
            expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
            expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
            expandafter
            UD@CheckWhetherNull
            expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
            expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
            expandafter
            {UD@nameexpandafterremove@begindocumenthook{@begindocumentsubhook@#1}}%
            {%
            endgroup
            @latex@info{Removal of undefined stringAtBeginDocument-sub-hookMessageBreak`#1' obsolete}%
            }{%
            @latex@info{Removing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
            expandafterUD@Exchangeexpandafter{%
            expandaftertoks@expandafter{thetoks@}%
            }{%
            expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
            endgroup
            expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
            toks@
            expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
            {expandafterexpandafterexpandafter@gobbleexpandafterkeepexpandafter.@begindocumenthook}%
            xdef@begindocumenthook{thetoks@}%
            }%
            }%
            UD@namegloballet{@begindocumentsubhook@#1}=UndEFineD
            }%
            %%----------------------------------------------------------------------
            %% Introduce new AtBeginDocument-sub-hook:
            %%......................................................................
            %% AtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
            %% {<tokens>}%
            %% Adds a new AtBeginDocument-sub-hook whose name is
            %% <name of AtBeginDocument-sub-hook> to the AtBeginDocument-hook
            %% and defines that AtBeginDocument-sub-hook to deliver <tokens>.
            newcommandAtBeginDocumentSubHook[2]{%
            @ifundefined{@begindocumentsubhook@#1}{%
            @latex@info{Introducing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
            UD@namenewcommand{@begindocumentsubhook@#1}{}%
            expandafterUD@Exchangeexpandafter{%
            expandaftertoks@expandafter{thetoks@}%
            UD@nameAtBeginDocument{@begindocumentsubhook@#1}%
            }{%
            toks@{#2}%
            UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
            }%
            }{%
            @latex@error{Sub-Hook `#1' already definedon@line}{%
            Use either stringRemoveAtBeginDocumentSubHookspace for removing the sub-hook,MessageBreak
            or stringReplaceContentOfAtBeginDocumentSubHookspace for replacing the content of theMessageBreak
            sub-hook,MessageBreak
            or stringAddToContentOfAtBeginDocumentSubHookspace for adding tokens to the sub-hook.%
            }%
            }%
            }%
            %%----------------------------------------------------------------------
            %% Replace all tokens of an AtBeginDocument-sub-hook:
            %%......................................................................
            %% ReplaceContentOfAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
            %% {<tokens>}%
            %% Redefines the AtBeginDocument-sub-hook whose name is
            %% <name of AtBeginDocument-sub-hook> to deliver <tokens>.
            newcommandReplaceContentOfAtBeginDocumentSubHook[2]{%
            @ifundefined{@begindocumentsubhook@#1}{%
            @latex@info{To be replaced stringAtBeginDocument-sub-hookMessageBreak`#1' does not exist}%
            AtBeginDocumentSubHook{#1}{#2}%
            }{%
            @latex@info{Replacing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
            expandafterUD@Exchangeexpandafter{%
            expandaftertoks@expandafter{thetoks@}%
            }{%
            toks@{#2}%
            UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
            }%
            }%
            }%
            %%----------------------------------------------------------------------
            %% Add tokens to an AtBeginDocument-sub-hook:
            %%......................................................................
            %% AddToContentOfAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
            %% {<prepend-tokens>}%
            %% {<append-tokens>}%
            %% Redefines the AtBeginDocument-sub-hook whose name is
            %% <name of AtBeginDocument-sub-hook> to additionally deliver <prepend-tokens>
            %% in front of the tokens that are already in this hook and to additionally
            %% deliver <append-tokens> behind the tokens that are already in this hook.
            newcommandAddToContentOfAtBeginDocumentSubHook[3]{%
            @ifundefined{@begindocumentsubhook@#1}{%
            @latex@info{stringAtBeginDocument-sub-hook `#1' where tokensMessageBreak shall be added does not exist}%
            AtBeginDocumentSubHook{#1}{#2#3}%
            }{%
            @latex@info{Adding tokens to stringAtBeginDocument-sub-hookMessageBreak`#1'}%
            expandafterUD@Exchangeexpandafter{%
            expandaftertoks@expandafter{thetoks@}%
            }{%
            toks@{#2}%
            toks@expandafterexpandafterexpandafter
            expandafterexpandafterexpandafterexpandafter{%
            UD@nametheexpandaftertoks@{@begindocumentsubhook@#1}#3%
            }%
            UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
            }%
            }%
            }%
            AtBeginDocument{%
            renewcommandRemoveAtBeginDocumentSubHook[1]{%
            @bsphack
            UD@namegloballet{@begindocumentsubhook@#1}=UndEFineD
            @latex@info{Removal of stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
            @esphack
            }%
            renewcommandAtBeginDocumentSubHook[2]{%
            @bsphack
            @latex@info{Introducing stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
            @esphack
            #2%
            }%
            renewcommandReplaceContentOfAtBeginDocumentSubHook[2]{%
            @bsphack
            @latex@info{Replacing stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
            @esphack
            #2%
            }%
            renewcommandAddToContentOfAtBeginDocumentSubHook[3]{%
            @bsphack
            @latex@info{Adding to stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
            @esphack
            #2#3%
            }%
            }%
            makeatother

            %expandaftershowcsname @begindocumenthookendcsname

            AtBeginDocumentSubHook{SomeSubHook}{Somepar}

            AtBeginDocumentSubHook{FirstSubHook}{Firstpar}

            AtBeginDocumentSubHook{SecondSubHook}{Secondpar}

            AtBeginDocumentSubHook{ThirdSubHook}{Thirdpar}

            AtBeginDocumentSubHook{FourthSubHook}{Fourthpar}

            % Let's cancel/change some of the above at this point

            RemoveAtBeginDocumentSubHook{SomeSubHook}

            ReplaceContentOfAtBeginDocumentSubHook{FirstSubHook}{New Firstpar}

            ReplaceContentOfAtBeginDocumentSubHook{SecondSubHook}{New Secondpar}

            ReplaceContentOfAtBeginDocumentSubHook{ThirdSubHook}{Thi}

            AddToContentOfAtBeginDocumentSubHook{ThirdSubHook}{New }{rdpar}

            %expandaftershowcsname @begindocumenthookendcsname

            begin{document}

            end{document}


            enter image description here






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


              }
              });














              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

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              9
              down vote



              accepted










              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 particularly liked the idea of not messing with @begindocumenthook. Otherwise, packages innocently using AtBeginDocument would never know what hit them. For my scenario, your solution turned out to be the most suitable one. The one from Peter Grill was a very close second. There is no special reason behind this, I had started working on your's first.
                – Masroor
                Dec 14 at 2:10















              up vote
              9
              down vote



              accepted










              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 particularly liked the idea of not messing with @begindocumenthook. Otherwise, packages innocently using AtBeginDocument would never know what hit them. For my scenario, your solution turned out to be the most suitable one. The one from Peter Grill was a very close second. There is no special reason behind this, I had started working on your's first.
                – Masroor
                Dec 14 at 2:10













              up vote
              9
              down vote



              accepted







              up vote
              9
              down vote



              accepted






              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 Dec 13 at 14:03









              Paul Stanley

              14k42746




              14k42746












              • I particularly liked the idea of not messing with @begindocumenthook. Otherwise, packages innocently using AtBeginDocument would never know what hit them. For my scenario, your solution turned out to be the most suitable one. The one from Peter Grill was a very close second. There is no special reason behind this, I had started working on your's first.
                – Masroor
                Dec 14 at 2:10


















              • I particularly liked the idea of not messing with @begindocumenthook. Otherwise, packages innocently using AtBeginDocument would never know what hit them. For my scenario, your solution turned out to be the most suitable one. The one from Peter Grill was a very close second. There is no special reason behind this, I had started working on your's first.
                – Masroor
                Dec 14 at 2:10
















              I particularly liked the idea of not messing with @begindocumenthook. Otherwise, packages innocently using AtBeginDocument would never know what hit them. For my scenario, your solution turned out to be the most suitable one. The one from Peter Grill was a very close second. There is no special reason behind this, I had started working on your's first.
              – Masroor
              Dec 14 at 2:10




              I particularly liked the idea of not messing with @begindocumenthook. Otherwise, packages innocently using AtBeginDocument would never know what hit them. For my scenario, your solution turned out to be the most suitable one. The one from Peter Grill was a very close second. There is no special reason behind this, I had started working on your's first.
              – Masroor
              Dec 14 at 2:10










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























              • Your solution is a very nice one. It also let me understand how exactly AtBeginDocument works. But eventually I failed to use it since in the homegrown style file, many packages were innocently using AtBeginDocument. And not all can be ignored.
                – Masroor
                Dec 13 at 18:00

















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























              • Your solution is a very nice one. It also let me understand how exactly AtBeginDocument works. But eventually I failed to use it since in the homegrown style file, many packages were innocently using AtBeginDocument. And not all can be ignored.
                – Masroor
                Dec 13 at 18:00















              up vote
              11
              down vote










              up vote
              11
              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 Dec 13 at 13:29

























              answered Dec 13 at 12:36









              moewe

              85.2k9108328




              85.2k9108328












              • Your solution is a very nice one. It also let me understand how exactly AtBeginDocument works. But eventually I failed to use it since in the homegrown style file, many packages were innocently using AtBeginDocument. And not all can be ignored.
                – Masroor
                Dec 13 at 18:00




















              • Your solution is a very nice one. It also let me understand how exactly AtBeginDocument works. But eventually I failed to use it since in the homegrown style file, many packages were innocently using AtBeginDocument. And not all can be ignored.
                – Masroor
                Dec 13 at 18:00


















              Your solution is a very nice one. It also let me understand how exactly AtBeginDocument works. But eventually I failed to use it since in the homegrown style file, many packages were innocently using AtBeginDocument. And not all can be ignored.
              – Masroor
              Dec 13 at 18:00






              Your solution is a very nice one. It also let me understand how exactly AtBeginDocument works. But eventually I failed to use it since in the homegrown style file, many packages were innocently using AtBeginDocument. And not all can be ignored.
              – Masroor
              Dec 13 at 18:00












              up vote
              4
              down vote













              This is similar to Paul Stanley's answer but with slightly different UI. This
              defines AllowAtBeginDocumentToBeResetable which makes all subsequent uses of AtBeginDocument resetable. To reset these you invoke ClearResetableAtBeginDocumentList.
              Thus your example code would look like:



              AllowAtBeginDocumentToBeResetable%% ALL EXTERNAL PACKAGES INCLUDED BEFORE HERE

              AtBeginDocument{Firstpar}
              AtBeginDocument{Secondpar}
              AtBeginDocument{Thirdpar}

              ClearResetableAtBeginDocumentList% I want to cancel the above at this point

              AtBeginDocument{New Firstpar}
              AtBeginDocument{New Secondpar}
              AtBeginDocument{New Thirdpar}


              which produces:



              enter image description here



              Notes:




              • Note that this requires that all external packages MUST be included before you issue AllowAtBeginDocumentToBeResetable.


              Code:



              documentclass{memoir}

              makeatletter
              let@OldAtBeginDocumentAtBeginDocument
              newcommand{@ResetableBegindDoumentItems}{}%
              newcommand{AllowAtBeginDocumentToBeResetable}{%
              ifdefined@AtBeginDocumentIncludesResetableListelse
              gdef@AtBeginDocumentIncludesResetableList{}% Don't execute this again
              AtBeginDocument{@ResetableBegindDoumentItems}%
              fi
              renewcommand{AtBeginDocument}[1]{%
              g@addto@macro@ResetableBegindDoumentItems{{##1}}%
              }%
              }
              newcommand*{ClearResetableAtBeginDocumentList}{%
              gdef@ResetableBegindDoumentItems{}%
              }
              makeatother


              AllowAtBeginDocumentToBeResetable%% ALL EXTERNAL PACKAGES INCLUDED BEFORE HERE

              AtBeginDocument{Firstpar}
              AtBeginDocument{Secondpar}
              AtBeginDocument{Thirdpar}

              ClearResetableAtBeginDocumentList% I want to cancel the above at this point

              AtBeginDocument{New Firstpar}
              AtBeginDocument{New Secondpar}
              AtBeginDocument{New Thirdpar}

              begin{document}

              end{document}





              share|improve this answer

























                up vote
                4
                down vote













                This is similar to Paul Stanley's answer but with slightly different UI. This
                defines AllowAtBeginDocumentToBeResetable which makes all subsequent uses of AtBeginDocument resetable. To reset these you invoke ClearResetableAtBeginDocumentList.
                Thus your example code would look like:



                AllowAtBeginDocumentToBeResetable%% ALL EXTERNAL PACKAGES INCLUDED BEFORE HERE

                AtBeginDocument{Firstpar}
                AtBeginDocument{Secondpar}
                AtBeginDocument{Thirdpar}

                ClearResetableAtBeginDocumentList% I want to cancel the above at this point

                AtBeginDocument{New Firstpar}
                AtBeginDocument{New Secondpar}
                AtBeginDocument{New Thirdpar}


                which produces:



                enter image description here



                Notes:




                • Note that this requires that all external packages MUST be included before you issue AllowAtBeginDocumentToBeResetable.


                Code:



                documentclass{memoir}

                makeatletter
                let@OldAtBeginDocumentAtBeginDocument
                newcommand{@ResetableBegindDoumentItems}{}%
                newcommand{AllowAtBeginDocumentToBeResetable}{%
                ifdefined@AtBeginDocumentIncludesResetableListelse
                gdef@AtBeginDocumentIncludesResetableList{}% Don't execute this again
                AtBeginDocument{@ResetableBegindDoumentItems}%
                fi
                renewcommand{AtBeginDocument}[1]{%
                g@addto@macro@ResetableBegindDoumentItems{{##1}}%
                }%
                }
                newcommand*{ClearResetableAtBeginDocumentList}{%
                gdef@ResetableBegindDoumentItems{}%
                }
                makeatother


                AllowAtBeginDocumentToBeResetable%% ALL EXTERNAL PACKAGES INCLUDED BEFORE HERE

                AtBeginDocument{Firstpar}
                AtBeginDocument{Secondpar}
                AtBeginDocument{Thirdpar}

                ClearResetableAtBeginDocumentList% I want to cancel the above at this point

                AtBeginDocument{New Firstpar}
                AtBeginDocument{New Secondpar}
                AtBeginDocument{New Thirdpar}

                begin{document}

                end{document}





                share|improve this answer























                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  This is similar to Paul Stanley's answer but with slightly different UI. This
                  defines AllowAtBeginDocumentToBeResetable which makes all subsequent uses of AtBeginDocument resetable. To reset these you invoke ClearResetableAtBeginDocumentList.
                  Thus your example code would look like:



                  AllowAtBeginDocumentToBeResetable%% ALL EXTERNAL PACKAGES INCLUDED BEFORE HERE

                  AtBeginDocument{Firstpar}
                  AtBeginDocument{Secondpar}
                  AtBeginDocument{Thirdpar}

                  ClearResetableAtBeginDocumentList% I want to cancel the above at this point

                  AtBeginDocument{New Firstpar}
                  AtBeginDocument{New Secondpar}
                  AtBeginDocument{New Thirdpar}


                  which produces:



                  enter image description here



                  Notes:




                  • Note that this requires that all external packages MUST be included before you issue AllowAtBeginDocumentToBeResetable.


                  Code:



                  documentclass{memoir}

                  makeatletter
                  let@OldAtBeginDocumentAtBeginDocument
                  newcommand{@ResetableBegindDoumentItems}{}%
                  newcommand{AllowAtBeginDocumentToBeResetable}{%
                  ifdefined@AtBeginDocumentIncludesResetableListelse
                  gdef@AtBeginDocumentIncludesResetableList{}% Don't execute this again
                  AtBeginDocument{@ResetableBegindDoumentItems}%
                  fi
                  renewcommand{AtBeginDocument}[1]{%
                  g@addto@macro@ResetableBegindDoumentItems{{##1}}%
                  }%
                  }
                  newcommand*{ClearResetableAtBeginDocumentList}{%
                  gdef@ResetableBegindDoumentItems{}%
                  }
                  makeatother


                  AllowAtBeginDocumentToBeResetable%% ALL EXTERNAL PACKAGES INCLUDED BEFORE HERE

                  AtBeginDocument{Firstpar}
                  AtBeginDocument{Secondpar}
                  AtBeginDocument{Thirdpar}

                  ClearResetableAtBeginDocumentList% I want to cancel the above at this point

                  AtBeginDocument{New Firstpar}
                  AtBeginDocument{New Secondpar}
                  AtBeginDocument{New Thirdpar}

                  begin{document}

                  end{document}





                  share|improve this answer












                  This is similar to Paul Stanley's answer but with slightly different UI. This
                  defines AllowAtBeginDocumentToBeResetable which makes all subsequent uses of AtBeginDocument resetable. To reset these you invoke ClearResetableAtBeginDocumentList.
                  Thus your example code would look like:



                  AllowAtBeginDocumentToBeResetable%% ALL EXTERNAL PACKAGES INCLUDED BEFORE HERE

                  AtBeginDocument{Firstpar}
                  AtBeginDocument{Secondpar}
                  AtBeginDocument{Thirdpar}

                  ClearResetableAtBeginDocumentList% I want to cancel the above at this point

                  AtBeginDocument{New Firstpar}
                  AtBeginDocument{New Secondpar}
                  AtBeginDocument{New Thirdpar}


                  which produces:



                  enter image description here



                  Notes:




                  • Note that this requires that all external packages MUST be included before you issue AllowAtBeginDocumentToBeResetable.


                  Code:



                  documentclass{memoir}

                  makeatletter
                  let@OldAtBeginDocumentAtBeginDocument
                  newcommand{@ResetableBegindDoumentItems}{}%
                  newcommand{AllowAtBeginDocumentToBeResetable}{%
                  ifdefined@AtBeginDocumentIncludesResetableListelse
                  gdef@AtBeginDocumentIncludesResetableList{}% Don't execute this again
                  AtBeginDocument{@ResetableBegindDoumentItems}%
                  fi
                  renewcommand{AtBeginDocument}[1]{%
                  g@addto@macro@ResetableBegindDoumentItems{{##1}}%
                  }%
                  }
                  newcommand*{ClearResetableAtBeginDocumentList}{%
                  gdef@ResetableBegindDoumentItems{}%
                  }
                  makeatother


                  AllowAtBeginDocumentToBeResetable%% ALL EXTERNAL PACKAGES INCLUDED BEFORE HERE

                  AtBeginDocument{Firstpar}
                  AtBeginDocument{Secondpar}
                  AtBeginDocument{Thirdpar}

                  ClearResetableAtBeginDocumentList% I want to cancel the above at this point

                  AtBeginDocument{New Firstpar}
                  AtBeginDocument{New Secondpar}
                  AtBeginDocument{New Thirdpar}

                  begin{document}

                  end{document}






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 13 at 18:11









                  Peter Grill

                  163k25433745




                  163k25433745






















                      up vote
                      4
                      down vote













                      I can offer a few routines for introducing sub-hooks within the AtBeginDocument-hook and maintaining them.



                      1) AtBeginDocumentSubHook



                      Syntax:



                      AtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%
                      {&langle;tokens&rangle;}%


                      Adds a new AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; to the AtBeginDocument-hook and defines that AtBeginDocument-sub-hook to deliver &langle;tokens&rangle;.



                      2) AddToContentOfAtBeginDocumentSubHook



                      Syntax:



                      AddToContentOfAtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%
                      {&langle;prepend-tokens&rangle;}%
                      {&langle;append-tokens&rangle;}%


                      Redefines the AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; to additionally deliver &langle;prepend-tokens&rangle; in front of the tokens that are already in this sub-hook and to additionally deliver &langle;append-tokens&rangle; behind the tokens that are already in this sub-hook.



                      3) ReplaceContentOfAtBeginDocumentSubHook



                      Syntax:



                      ReplaceContentOfAtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%
                      {&langle;tokens&rangle;}%


                      Redefines the AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; to deliver &langle;tokens&rangle;.



                      4) RemoveAtBeginDocumentSubHook



                      Syntax:



                      RemoveAtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%


                      Removes the AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; from the AtBeginDocument-hook.



                      documentclass{memoir}

                      makeatletter
                      %%----------------------------------------------------------------------
                      %% Paraphernalia
                      %%----------------------------------------------------------------------
                      newcommandUD@firstoftwo[2]{#1}%
                      newcommandUD@secondoftwo[2]{#2}%
                      newcommandUD@Exchange[2]{#2#1}%
                      newcommandUD@name{}longdefUD@name#1#{romannumeral0UD@innername{#1}}%
                      newcommandUD@innername[2]{%
                      expandafterUD@Exchangeexpandafter{csname#2endcsname}{ #1}%
                      }%
                      %%----------------------------------------------------------------------
                      %% Check whether argument is empty:
                      %%......................................................................
                      %% UD@CheckWhetherNull{<Argument which is to be checked>}%
                      %% {<Tokens to be delivered in case that argument
                      %% which is to be checked is empty>}%
                      %% {<Tokens to be delivered in case that argument
                      %% which is to be checked is not empty>}%
                      %%
                      %% The gist of this macro comes from Robert R. Schneck's ifempty-macro:
                      %% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
                      newcommandUD@CheckWhetherNull[1]{%
                      romannumeral0expandafterUD@secondoftwostring{expandafter
                      UD@secondoftwoexpandafter{expandafter{string#1}expandafter
                      UD@secondoftwostring}expandafterUD@firstoftwoexpandafter{expandafter
                      UD@secondoftwostring}expandafterexpandafterUD@firstoftwo{ }{}%
                      UD@secondoftwo}{expandafterexpandafterUD@firstoftwo{ }{}UD@firstoftwo}%
                      }%
                      %%----------------------------------------------------------------------
                      %% Remove AtBeginDocument-sub-hook from AtBeginDocument-hook:
                      %%......................................................................
                      %% RemoveAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}
                      %% removes the AtBeginDocument-sub-hook whose name is
                      %% <name of AtBeginDocument-sub-hook> from the AtBeginDocument-hook.
                      newcommand*RemoveAtBeginDocumentSubHook[1]{%
                      begingroup
                      UD@namelongdefkeep##1{@begindocumentsubhook@#1}{##1}%
                      UD@namelongdefremove##1{@begindocumentsubhook@#1}{}%
                      expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                      expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                      expandafter
                      UD@CheckWhetherNull
                      expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                      expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                      expandafter
                      {UD@nameexpandafterremove@begindocumenthook{@begindocumentsubhook@#1}}%
                      {%
                      endgroup
                      @latex@info{Removal of undefined stringAtBeginDocument-sub-hookMessageBreak`#1' obsolete}%
                      }{%
                      @latex@info{Removing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                      expandafterUD@Exchangeexpandafter{%
                      expandaftertoks@expandafter{thetoks@}%
                      }{%
                      expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                      endgroup
                      expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                      toks@
                      expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                      {expandafterexpandafterexpandafter@gobbleexpandafterkeepexpandafter.@begindocumenthook}%
                      xdef@begindocumenthook{thetoks@}%
                      }%
                      }%
                      UD@namegloballet{@begindocumentsubhook@#1}=UndEFineD
                      }%
                      %%----------------------------------------------------------------------
                      %% Introduce new AtBeginDocument-sub-hook:
                      %%......................................................................
                      %% AtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
                      %% {<tokens>}%
                      %% Adds a new AtBeginDocument-sub-hook whose name is
                      %% <name of AtBeginDocument-sub-hook> to the AtBeginDocument-hook
                      %% and defines that AtBeginDocument-sub-hook to deliver <tokens>.
                      newcommandAtBeginDocumentSubHook[2]{%
                      @ifundefined{@begindocumentsubhook@#1}{%
                      @latex@info{Introducing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                      UD@namenewcommand{@begindocumentsubhook@#1}{}%
                      expandafterUD@Exchangeexpandafter{%
                      expandaftertoks@expandafter{thetoks@}%
                      UD@nameAtBeginDocument{@begindocumentsubhook@#1}%
                      }{%
                      toks@{#2}%
                      UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
                      }%
                      }{%
                      @latex@error{Sub-Hook `#1' already definedon@line}{%
                      Use either stringRemoveAtBeginDocumentSubHookspace for removing the sub-hook,MessageBreak
                      or stringReplaceContentOfAtBeginDocumentSubHookspace for replacing the content of theMessageBreak
                      sub-hook,MessageBreak
                      or stringAddToContentOfAtBeginDocumentSubHookspace for adding tokens to the sub-hook.%
                      }%
                      }%
                      }%
                      %%----------------------------------------------------------------------
                      %% Replace all tokens of an AtBeginDocument-sub-hook:
                      %%......................................................................
                      %% ReplaceContentOfAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
                      %% {<tokens>}%
                      %% Redefines the AtBeginDocument-sub-hook whose name is
                      %% <name of AtBeginDocument-sub-hook> to deliver <tokens>.
                      newcommandReplaceContentOfAtBeginDocumentSubHook[2]{%
                      @ifundefined{@begindocumentsubhook@#1}{%
                      @latex@info{To be replaced stringAtBeginDocument-sub-hookMessageBreak`#1' does not exist}%
                      AtBeginDocumentSubHook{#1}{#2}%
                      }{%
                      @latex@info{Replacing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                      expandafterUD@Exchangeexpandafter{%
                      expandaftertoks@expandafter{thetoks@}%
                      }{%
                      toks@{#2}%
                      UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
                      }%
                      }%
                      }%
                      %%----------------------------------------------------------------------
                      %% Add tokens to an AtBeginDocument-sub-hook:
                      %%......................................................................
                      %% AddToContentOfAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
                      %% {<prepend-tokens>}%
                      %% {<append-tokens>}%
                      %% Redefines the AtBeginDocument-sub-hook whose name is
                      %% <name of AtBeginDocument-sub-hook> to additionally deliver <prepend-tokens>
                      %% in front of the tokens that are already in this hook and to additionally
                      %% deliver <append-tokens> behind the tokens that are already in this hook.
                      newcommandAddToContentOfAtBeginDocumentSubHook[3]{%
                      @ifundefined{@begindocumentsubhook@#1}{%
                      @latex@info{stringAtBeginDocument-sub-hook `#1' where tokensMessageBreak shall be added does not exist}%
                      AtBeginDocumentSubHook{#1}{#2#3}%
                      }{%
                      @latex@info{Adding tokens to stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                      expandafterUD@Exchangeexpandafter{%
                      expandaftertoks@expandafter{thetoks@}%
                      }{%
                      toks@{#2}%
                      toks@expandafterexpandafterexpandafter
                      expandafterexpandafterexpandafterexpandafter{%
                      UD@nametheexpandaftertoks@{@begindocumentsubhook@#1}#3%
                      }%
                      UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
                      }%
                      }%
                      }%
                      AtBeginDocument{%
                      renewcommandRemoveAtBeginDocumentSubHook[1]{%
                      @bsphack
                      UD@namegloballet{@begindocumentsubhook@#1}=UndEFineD
                      @latex@info{Removal of stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                      @esphack
                      }%
                      renewcommandAtBeginDocumentSubHook[2]{%
                      @bsphack
                      @latex@info{Introducing stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                      @esphack
                      #2%
                      }%
                      renewcommandReplaceContentOfAtBeginDocumentSubHook[2]{%
                      @bsphack
                      @latex@info{Replacing stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                      @esphack
                      #2%
                      }%
                      renewcommandAddToContentOfAtBeginDocumentSubHook[3]{%
                      @bsphack
                      @latex@info{Adding to stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                      @esphack
                      #2#3%
                      }%
                      }%
                      makeatother

                      %expandaftershowcsname @begindocumenthookendcsname

                      AtBeginDocumentSubHook{SomeSubHook}{Somepar}

                      AtBeginDocumentSubHook{FirstSubHook}{Firstpar}

                      AtBeginDocumentSubHook{SecondSubHook}{Secondpar}

                      AtBeginDocumentSubHook{ThirdSubHook}{Thirdpar}

                      AtBeginDocumentSubHook{FourthSubHook}{Fourthpar}

                      % Let's cancel/change some of the above at this point

                      RemoveAtBeginDocumentSubHook{SomeSubHook}

                      ReplaceContentOfAtBeginDocumentSubHook{FirstSubHook}{New Firstpar}

                      ReplaceContentOfAtBeginDocumentSubHook{SecondSubHook}{New Secondpar}

                      ReplaceContentOfAtBeginDocumentSubHook{ThirdSubHook}{Thi}

                      AddToContentOfAtBeginDocumentSubHook{ThirdSubHook}{New }{rdpar}

                      %expandaftershowcsname @begindocumenthookendcsname

                      begin{document}

                      end{document}


                      enter image description here






                      share|improve this answer



























                        up vote
                        4
                        down vote













                        I can offer a few routines for introducing sub-hooks within the AtBeginDocument-hook and maintaining them.



                        1) AtBeginDocumentSubHook



                        Syntax:



                        AtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%
                        {&langle;tokens&rangle;}%


                        Adds a new AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; to the AtBeginDocument-hook and defines that AtBeginDocument-sub-hook to deliver &langle;tokens&rangle;.



                        2) AddToContentOfAtBeginDocumentSubHook



                        Syntax:



                        AddToContentOfAtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%
                        {&langle;prepend-tokens&rangle;}%
                        {&langle;append-tokens&rangle;}%


                        Redefines the AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; to additionally deliver &langle;prepend-tokens&rangle; in front of the tokens that are already in this sub-hook and to additionally deliver &langle;append-tokens&rangle; behind the tokens that are already in this sub-hook.



                        3) ReplaceContentOfAtBeginDocumentSubHook



                        Syntax:



                        ReplaceContentOfAtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%
                        {&langle;tokens&rangle;}%


                        Redefines the AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; to deliver &langle;tokens&rangle;.



                        4) RemoveAtBeginDocumentSubHook



                        Syntax:



                        RemoveAtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%


                        Removes the AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; from the AtBeginDocument-hook.



                        documentclass{memoir}

                        makeatletter
                        %%----------------------------------------------------------------------
                        %% Paraphernalia
                        %%----------------------------------------------------------------------
                        newcommandUD@firstoftwo[2]{#1}%
                        newcommandUD@secondoftwo[2]{#2}%
                        newcommandUD@Exchange[2]{#2#1}%
                        newcommandUD@name{}longdefUD@name#1#{romannumeral0UD@innername{#1}}%
                        newcommandUD@innername[2]{%
                        expandafterUD@Exchangeexpandafter{csname#2endcsname}{ #1}%
                        }%
                        %%----------------------------------------------------------------------
                        %% Check whether argument is empty:
                        %%......................................................................
                        %% UD@CheckWhetherNull{<Argument which is to be checked>}%
                        %% {<Tokens to be delivered in case that argument
                        %% which is to be checked is empty>}%
                        %% {<Tokens to be delivered in case that argument
                        %% which is to be checked is not empty>}%
                        %%
                        %% The gist of this macro comes from Robert R. Schneck's ifempty-macro:
                        %% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
                        newcommandUD@CheckWhetherNull[1]{%
                        romannumeral0expandafterUD@secondoftwostring{expandafter
                        UD@secondoftwoexpandafter{expandafter{string#1}expandafter
                        UD@secondoftwostring}expandafterUD@firstoftwoexpandafter{expandafter
                        UD@secondoftwostring}expandafterexpandafterUD@firstoftwo{ }{}%
                        UD@secondoftwo}{expandafterexpandafterUD@firstoftwo{ }{}UD@firstoftwo}%
                        }%
                        %%----------------------------------------------------------------------
                        %% Remove AtBeginDocument-sub-hook from AtBeginDocument-hook:
                        %%......................................................................
                        %% RemoveAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}
                        %% removes the AtBeginDocument-sub-hook whose name is
                        %% <name of AtBeginDocument-sub-hook> from the AtBeginDocument-hook.
                        newcommand*RemoveAtBeginDocumentSubHook[1]{%
                        begingroup
                        UD@namelongdefkeep##1{@begindocumentsubhook@#1}{##1}%
                        UD@namelongdefremove##1{@begindocumentsubhook@#1}{}%
                        expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                        expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                        expandafter
                        UD@CheckWhetherNull
                        expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                        expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                        expandafter
                        {UD@nameexpandafterremove@begindocumenthook{@begindocumentsubhook@#1}}%
                        {%
                        endgroup
                        @latex@info{Removal of undefined stringAtBeginDocument-sub-hookMessageBreak`#1' obsolete}%
                        }{%
                        @latex@info{Removing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                        expandafterUD@Exchangeexpandafter{%
                        expandaftertoks@expandafter{thetoks@}%
                        }{%
                        expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                        endgroup
                        expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                        toks@
                        expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                        {expandafterexpandafterexpandafter@gobbleexpandafterkeepexpandafter.@begindocumenthook}%
                        xdef@begindocumenthook{thetoks@}%
                        }%
                        }%
                        UD@namegloballet{@begindocumentsubhook@#1}=UndEFineD
                        }%
                        %%----------------------------------------------------------------------
                        %% Introduce new AtBeginDocument-sub-hook:
                        %%......................................................................
                        %% AtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
                        %% {<tokens>}%
                        %% Adds a new AtBeginDocument-sub-hook whose name is
                        %% <name of AtBeginDocument-sub-hook> to the AtBeginDocument-hook
                        %% and defines that AtBeginDocument-sub-hook to deliver <tokens>.
                        newcommandAtBeginDocumentSubHook[2]{%
                        @ifundefined{@begindocumentsubhook@#1}{%
                        @latex@info{Introducing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                        UD@namenewcommand{@begindocumentsubhook@#1}{}%
                        expandafterUD@Exchangeexpandafter{%
                        expandaftertoks@expandafter{thetoks@}%
                        UD@nameAtBeginDocument{@begindocumentsubhook@#1}%
                        }{%
                        toks@{#2}%
                        UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
                        }%
                        }{%
                        @latex@error{Sub-Hook `#1' already definedon@line}{%
                        Use either stringRemoveAtBeginDocumentSubHookspace for removing the sub-hook,MessageBreak
                        or stringReplaceContentOfAtBeginDocumentSubHookspace for replacing the content of theMessageBreak
                        sub-hook,MessageBreak
                        or stringAddToContentOfAtBeginDocumentSubHookspace for adding tokens to the sub-hook.%
                        }%
                        }%
                        }%
                        %%----------------------------------------------------------------------
                        %% Replace all tokens of an AtBeginDocument-sub-hook:
                        %%......................................................................
                        %% ReplaceContentOfAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
                        %% {<tokens>}%
                        %% Redefines the AtBeginDocument-sub-hook whose name is
                        %% <name of AtBeginDocument-sub-hook> to deliver <tokens>.
                        newcommandReplaceContentOfAtBeginDocumentSubHook[2]{%
                        @ifundefined{@begindocumentsubhook@#1}{%
                        @latex@info{To be replaced stringAtBeginDocument-sub-hookMessageBreak`#1' does not exist}%
                        AtBeginDocumentSubHook{#1}{#2}%
                        }{%
                        @latex@info{Replacing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                        expandafterUD@Exchangeexpandafter{%
                        expandaftertoks@expandafter{thetoks@}%
                        }{%
                        toks@{#2}%
                        UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
                        }%
                        }%
                        }%
                        %%----------------------------------------------------------------------
                        %% Add tokens to an AtBeginDocument-sub-hook:
                        %%......................................................................
                        %% AddToContentOfAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
                        %% {<prepend-tokens>}%
                        %% {<append-tokens>}%
                        %% Redefines the AtBeginDocument-sub-hook whose name is
                        %% <name of AtBeginDocument-sub-hook> to additionally deliver <prepend-tokens>
                        %% in front of the tokens that are already in this hook and to additionally
                        %% deliver <append-tokens> behind the tokens that are already in this hook.
                        newcommandAddToContentOfAtBeginDocumentSubHook[3]{%
                        @ifundefined{@begindocumentsubhook@#1}{%
                        @latex@info{stringAtBeginDocument-sub-hook `#1' where tokensMessageBreak shall be added does not exist}%
                        AtBeginDocumentSubHook{#1}{#2#3}%
                        }{%
                        @latex@info{Adding tokens to stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                        expandafterUD@Exchangeexpandafter{%
                        expandaftertoks@expandafter{thetoks@}%
                        }{%
                        toks@{#2}%
                        toks@expandafterexpandafterexpandafter
                        expandafterexpandafterexpandafterexpandafter{%
                        UD@nametheexpandaftertoks@{@begindocumentsubhook@#1}#3%
                        }%
                        UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
                        }%
                        }%
                        }%
                        AtBeginDocument{%
                        renewcommandRemoveAtBeginDocumentSubHook[1]{%
                        @bsphack
                        UD@namegloballet{@begindocumentsubhook@#1}=UndEFineD
                        @latex@info{Removal of stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                        @esphack
                        }%
                        renewcommandAtBeginDocumentSubHook[2]{%
                        @bsphack
                        @latex@info{Introducing stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                        @esphack
                        #2%
                        }%
                        renewcommandReplaceContentOfAtBeginDocumentSubHook[2]{%
                        @bsphack
                        @latex@info{Replacing stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                        @esphack
                        #2%
                        }%
                        renewcommandAddToContentOfAtBeginDocumentSubHook[3]{%
                        @bsphack
                        @latex@info{Adding to stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                        @esphack
                        #2#3%
                        }%
                        }%
                        makeatother

                        %expandaftershowcsname @begindocumenthookendcsname

                        AtBeginDocumentSubHook{SomeSubHook}{Somepar}

                        AtBeginDocumentSubHook{FirstSubHook}{Firstpar}

                        AtBeginDocumentSubHook{SecondSubHook}{Secondpar}

                        AtBeginDocumentSubHook{ThirdSubHook}{Thirdpar}

                        AtBeginDocumentSubHook{FourthSubHook}{Fourthpar}

                        % Let's cancel/change some of the above at this point

                        RemoveAtBeginDocumentSubHook{SomeSubHook}

                        ReplaceContentOfAtBeginDocumentSubHook{FirstSubHook}{New Firstpar}

                        ReplaceContentOfAtBeginDocumentSubHook{SecondSubHook}{New Secondpar}

                        ReplaceContentOfAtBeginDocumentSubHook{ThirdSubHook}{Thi}

                        AddToContentOfAtBeginDocumentSubHook{ThirdSubHook}{New }{rdpar}

                        %expandaftershowcsname @begindocumenthookendcsname

                        begin{document}

                        end{document}


                        enter image description here






                        share|improve this answer

























                          up vote
                          4
                          down vote










                          up vote
                          4
                          down vote









                          I can offer a few routines for introducing sub-hooks within the AtBeginDocument-hook and maintaining them.



                          1) AtBeginDocumentSubHook



                          Syntax:



                          AtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%
                          {&langle;tokens&rangle;}%


                          Adds a new AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; to the AtBeginDocument-hook and defines that AtBeginDocument-sub-hook to deliver &langle;tokens&rangle;.



                          2) AddToContentOfAtBeginDocumentSubHook



                          Syntax:



                          AddToContentOfAtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%
                          {&langle;prepend-tokens&rangle;}%
                          {&langle;append-tokens&rangle;}%


                          Redefines the AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; to additionally deliver &langle;prepend-tokens&rangle; in front of the tokens that are already in this sub-hook and to additionally deliver &langle;append-tokens&rangle; behind the tokens that are already in this sub-hook.



                          3) ReplaceContentOfAtBeginDocumentSubHook



                          Syntax:



                          ReplaceContentOfAtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%
                          {&langle;tokens&rangle;}%


                          Redefines the AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; to deliver &langle;tokens&rangle;.



                          4) RemoveAtBeginDocumentSubHook



                          Syntax:



                          RemoveAtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%


                          Removes the AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; from the AtBeginDocument-hook.



                          documentclass{memoir}

                          makeatletter
                          %%----------------------------------------------------------------------
                          %% Paraphernalia
                          %%----------------------------------------------------------------------
                          newcommandUD@firstoftwo[2]{#1}%
                          newcommandUD@secondoftwo[2]{#2}%
                          newcommandUD@Exchange[2]{#2#1}%
                          newcommandUD@name{}longdefUD@name#1#{romannumeral0UD@innername{#1}}%
                          newcommandUD@innername[2]{%
                          expandafterUD@Exchangeexpandafter{csname#2endcsname}{ #1}%
                          }%
                          %%----------------------------------------------------------------------
                          %% Check whether argument is empty:
                          %%......................................................................
                          %% UD@CheckWhetherNull{<Argument which is to be checked>}%
                          %% {<Tokens to be delivered in case that argument
                          %% which is to be checked is empty>}%
                          %% {<Tokens to be delivered in case that argument
                          %% which is to be checked is not empty>}%
                          %%
                          %% The gist of this macro comes from Robert R. Schneck's ifempty-macro:
                          %% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
                          newcommandUD@CheckWhetherNull[1]{%
                          romannumeral0expandafterUD@secondoftwostring{expandafter
                          UD@secondoftwoexpandafter{expandafter{string#1}expandafter
                          UD@secondoftwostring}expandafterUD@firstoftwoexpandafter{expandafter
                          UD@secondoftwostring}expandafterexpandafterUD@firstoftwo{ }{}%
                          UD@secondoftwo}{expandafterexpandafterUD@firstoftwo{ }{}UD@firstoftwo}%
                          }%
                          %%----------------------------------------------------------------------
                          %% Remove AtBeginDocument-sub-hook from AtBeginDocument-hook:
                          %%......................................................................
                          %% RemoveAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}
                          %% removes the AtBeginDocument-sub-hook whose name is
                          %% <name of AtBeginDocument-sub-hook> from the AtBeginDocument-hook.
                          newcommand*RemoveAtBeginDocumentSubHook[1]{%
                          begingroup
                          UD@namelongdefkeep##1{@begindocumentsubhook@#1}{##1}%
                          UD@namelongdefremove##1{@begindocumentsubhook@#1}{}%
                          expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                          expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                          expandafter
                          UD@CheckWhetherNull
                          expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                          expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                          expandafter
                          {UD@nameexpandafterremove@begindocumenthook{@begindocumentsubhook@#1}}%
                          {%
                          endgroup
                          @latex@info{Removal of undefined stringAtBeginDocument-sub-hookMessageBreak`#1' obsolete}%
                          }{%
                          @latex@info{Removing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                          expandafterUD@Exchangeexpandafter{%
                          expandaftertoks@expandafter{thetoks@}%
                          }{%
                          expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                          endgroup
                          expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                          toks@
                          expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                          {expandafterexpandafterexpandafter@gobbleexpandafterkeepexpandafter.@begindocumenthook}%
                          xdef@begindocumenthook{thetoks@}%
                          }%
                          }%
                          UD@namegloballet{@begindocumentsubhook@#1}=UndEFineD
                          }%
                          %%----------------------------------------------------------------------
                          %% Introduce new AtBeginDocument-sub-hook:
                          %%......................................................................
                          %% AtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
                          %% {<tokens>}%
                          %% Adds a new AtBeginDocument-sub-hook whose name is
                          %% <name of AtBeginDocument-sub-hook> to the AtBeginDocument-hook
                          %% and defines that AtBeginDocument-sub-hook to deliver <tokens>.
                          newcommandAtBeginDocumentSubHook[2]{%
                          @ifundefined{@begindocumentsubhook@#1}{%
                          @latex@info{Introducing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                          UD@namenewcommand{@begindocumentsubhook@#1}{}%
                          expandafterUD@Exchangeexpandafter{%
                          expandaftertoks@expandafter{thetoks@}%
                          UD@nameAtBeginDocument{@begindocumentsubhook@#1}%
                          }{%
                          toks@{#2}%
                          UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
                          }%
                          }{%
                          @latex@error{Sub-Hook `#1' already definedon@line}{%
                          Use either stringRemoveAtBeginDocumentSubHookspace for removing the sub-hook,MessageBreak
                          or stringReplaceContentOfAtBeginDocumentSubHookspace for replacing the content of theMessageBreak
                          sub-hook,MessageBreak
                          or stringAddToContentOfAtBeginDocumentSubHookspace for adding tokens to the sub-hook.%
                          }%
                          }%
                          }%
                          %%----------------------------------------------------------------------
                          %% Replace all tokens of an AtBeginDocument-sub-hook:
                          %%......................................................................
                          %% ReplaceContentOfAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
                          %% {<tokens>}%
                          %% Redefines the AtBeginDocument-sub-hook whose name is
                          %% <name of AtBeginDocument-sub-hook> to deliver <tokens>.
                          newcommandReplaceContentOfAtBeginDocumentSubHook[2]{%
                          @ifundefined{@begindocumentsubhook@#1}{%
                          @latex@info{To be replaced stringAtBeginDocument-sub-hookMessageBreak`#1' does not exist}%
                          AtBeginDocumentSubHook{#1}{#2}%
                          }{%
                          @latex@info{Replacing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                          expandafterUD@Exchangeexpandafter{%
                          expandaftertoks@expandafter{thetoks@}%
                          }{%
                          toks@{#2}%
                          UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
                          }%
                          }%
                          }%
                          %%----------------------------------------------------------------------
                          %% Add tokens to an AtBeginDocument-sub-hook:
                          %%......................................................................
                          %% AddToContentOfAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
                          %% {<prepend-tokens>}%
                          %% {<append-tokens>}%
                          %% Redefines the AtBeginDocument-sub-hook whose name is
                          %% <name of AtBeginDocument-sub-hook> to additionally deliver <prepend-tokens>
                          %% in front of the tokens that are already in this hook and to additionally
                          %% deliver <append-tokens> behind the tokens that are already in this hook.
                          newcommandAddToContentOfAtBeginDocumentSubHook[3]{%
                          @ifundefined{@begindocumentsubhook@#1}{%
                          @latex@info{stringAtBeginDocument-sub-hook `#1' where tokensMessageBreak shall be added does not exist}%
                          AtBeginDocumentSubHook{#1}{#2#3}%
                          }{%
                          @latex@info{Adding tokens to stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                          expandafterUD@Exchangeexpandafter{%
                          expandaftertoks@expandafter{thetoks@}%
                          }{%
                          toks@{#2}%
                          toks@expandafterexpandafterexpandafter
                          expandafterexpandafterexpandafterexpandafter{%
                          UD@nametheexpandaftertoks@{@begindocumentsubhook@#1}#3%
                          }%
                          UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
                          }%
                          }%
                          }%
                          AtBeginDocument{%
                          renewcommandRemoveAtBeginDocumentSubHook[1]{%
                          @bsphack
                          UD@namegloballet{@begindocumentsubhook@#1}=UndEFineD
                          @latex@info{Removal of stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                          @esphack
                          }%
                          renewcommandAtBeginDocumentSubHook[2]{%
                          @bsphack
                          @latex@info{Introducing stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                          @esphack
                          #2%
                          }%
                          renewcommandReplaceContentOfAtBeginDocumentSubHook[2]{%
                          @bsphack
                          @latex@info{Replacing stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                          @esphack
                          #2%
                          }%
                          renewcommandAddToContentOfAtBeginDocumentSubHook[3]{%
                          @bsphack
                          @latex@info{Adding to stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                          @esphack
                          #2#3%
                          }%
                          }%
                          makeatother

                          %expandaftershowcsname @begindocumenthookendcsname

                          AtBeginDocumentSubHook{SomeSubHook}{Somepar}

                          AtBeginDocumentSubHook{FirstSubHook}{Firstpar}

                          AtBeginDocumentSubHook{SecondSubHook}{Secondpar}

                          AtBeginDocumentSubHook{ThirdSubHook}{Thirdpar}

                          AtBeginDocumentSubHook{FourthSubHook}{Fourthpar}

                          % Let's cancel/change some of the above at this point

                          RemoveAtBeginDocumentSubHook{SomeSubHook}

                          ReplaceContentOfAtBeginDocumentSubHook{FirstSubHook}{New Firstpar}

                          ReplaceContentOfAtBeginDocumentSubHook{SecondSubHook}{New Secondpar}

                          ReplaceContentOfAtBeginDocumentSubHook{ThirdSubHook}{Thi}

                          AddToContentOfAtBeginDocumentSubHook{ThirdSubHook}{New }{rdpar}

                          %expandaftershowcsname @begindocumenthookendcsname

                          begin{document}

                          end{document}


                          enter image description here






                          share|improve this answer














                          I can offer a few routines for introducing sub-hooks within the AtBeginDocument-hook and maintaining them.



                          1) AtBeginDocumentSubHook



                          Syntax:



                          AtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%
                          {&langle;tokens&rangle;}%


                          Adds a new AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; to the AtBeginDocument-hook and defines that AtBeginDocument-sub-hook to deliver &langle;tokens&rangle;.



                          2) AddToContentOfAtBeginDocumentSubHook



                          Syntax:



                          AddToContentOfAtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%
                          {&langle;prepend-tokens&rangle;}%
                          {&langle;append-tokens&rangle;}%


                          Redefines the AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; to additionally deliver &langle;prepend-tokens&rangle; in front of the tokens that are already in this sub-hook and to additionally deliver &langle;append-tokens&rangle; behind the tokens that are already in this sub-hook.



                          3) ReplaceContentOfAtBeginDocumentSubHook



                          Syntax:



                          ReplaceContentOfAtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%
                          {&langle;tokens&rangle;}%


                          Redefines the AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; to deliver &langle;tokens&rangle;.



                          4) RemoveAtBeginDocumentSubHook



                          Syntax:



                          RemoveAtBeginDocumentSubHook{&langle;name of AtBeginDocument-sub-hook&rangle;}%


                          Removes the AtBeginDocument-sub-hook whose name is &langle;name of AtBeginDocument-sub-hook&rangle; from the AtBeginDocument-hook.



                          documentclass{memoir}

                          makeatletter
                          %%----------------------------------------------------------------------
                          %% Paraphernalia
                          %%----------------------------------------------------------------------
                          newcommandUD@firstoftwo[2]{#1}%
                          newcommandUD@secondoftwo[2]{#2}%
                          newcommandUD@Exchange[2]{#2#1}%
                          newcommandUD@name{}longdefUD@name#1#{romannumeral0UD@innername{#1}}%
                          newcommandUD@innername[2]{%
                          expandafterUD@Exchangeexpandafter{csname#2endcsname}{ #1}%
                          }%
                          %%----------------------------------------------------------------------
                          %% Check whether argument is empty:
                          %%......................................................................
                          %% UD@CheckWhetherNull{<Argument which is to be checked>}%
                          %% {<Tokens to be delivered in case that argument
                          %% which is to be checked is empty>}%
                          %% {<Tokens to be delivered in case that argument
                          %% which is to be checked is not empty>}%
                          %%
                          %% The gist of this macro comes from Robert R. Schneck's ifempty-macro:
                          %% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
                          newcommandUD@CheckWhetherNull[1]{%
                          romannumeral0expandafterUD@secondoftwostring{expandafter
                          UD@secondoftwoexpandafter{expandafter{string#1}expandafter
                          UD@secondoftwostring}expandafterUD@firstoftwoexpandafter{expandafter
                          UD@secondoftwostring}expandafterexpandafterUD@firstoftwo{ }{}%
                          UD@secondoftwo}{expandafterexpandafterUD@firstoftwo{ }{}UD@firstoftwo}%
                          }%
                          %%----------------------------------------------------------------------
                          %% Remove AtBeginDocument-sub-hook from AtBeginDocument-hook:
                          %%......................................................................
                          %% RemoveAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}
                          %% removes the AtBeginDocument-sub-hook whose name is
                          %% <name of AtBeginDocument-sub-hook> from the AtBeginDocument-hook.
                          newcommand*RemoveAtBeginDocumentSubHook[1]{%
                          begingroup
                          UD@namelongdefkeep##1{@begindocumentsubhook@#1}{##1}%
                          UD@namelongdefremove##1{@begindocumentsubhook@#1}{}%
                          expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                          expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                          expandafter
                          UD@CheckWhetherNull
                          expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                          expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                          expandafter
                          {UD@nameexpandafterremove@begindocumenthook{@begindocumentsubhook@#1}}%
                          {%
                          endgroup
                          @latex@info{Removal of undefined stringAtBeginDocument-sub-hookMessageBreak`#1' obsolete}%
                          }{%
                          @latex@info{Removing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                          expandafterUD@Exchangeexpandafter{%
                          expandaftertoks@expandafter{thetoks@}%
                          }{%
                          expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                          endgroup
                          expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                          toks@
                          expandafterexpandafterexpandafterexpandafterexpandafterexpandafterexpandafter
                          {expandafterexpandafterexpandafter@gobbleexpandafterkeepexpandafter.@begindocumenthook}%
                          xdef@begindocumenthook{thetoks@}%
                          }%
                          }%
                          UD@namegloballet{@begindocumentsubhook@#1}=UndEFineD
                          }%
                          %%----------------------------------------------------------------------
                          %% Introduce new AtBeginDocument-sub-hook:
                          %%......................................................................
                          %% AtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
                          %% {<tokens>}%
                          %% Adds a new AtBeginDocument-sub-hook whose name is
                          %% <name of AtBeginDocument-sub-hook> to the AtBeginDocument-hook
                          %% and defines that AtBeginDocument-sub-hook to deliver <tokens>.
                          newcommandAtBeginDocumentSubHook[2]{%
                          @ifundefined{@begindocumentsubhook@#1}{%
                          @latex@info{Introducing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                          UD@namenewcommand{@begindocumentsubhook@#1}{}%
                          expandafterUD@Exchangeexpandafter{%
                          expandaftertoks@expandafter{thetoks@}%
                          UD@nameAtBeginDocument{@begindocumentsubhook@#1}%
                          }{%
                          toks@{#2}%
                          UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
                          }%
                          }{%
                          @latex@error{Sub-Hook `#1' already definedon@line}{%
                          Use either stringRemoveAtBeginDocumentSubHookspace for removing the sub-hook,MessageBreak
                          or stringReplaceContentOfAtBeginDocumentSubHookspace for replacing the content of theMessageBreak
                          sub-hook,MessageBreak
                          or stringAddToContentOfAtBeginDocumentSubHookspace for adding tokens to the sub-hook.%
                          }%
                          }%
                          }%
                          %%----------------------------------------------------------------------
                          %% Replace all tokens of an AtBeginDocument-sub-hook:
                          %%......................................................................
                          %% ReplaceContentOfAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
                          %% {<tokens>}%
                          %% Redefines the AtBeginDocument-sub-hook whose name is
                          %% <name of AtBeginDocument-sub-hook> to deliver <tokens>.
                          newcommandReplaceContentOfAtBeginDocumentSubHook[2]{%
                          @ifundefined{@begindocumentsubhook@#1}{%
                          @latex@info{To be replaced stringAtBeginDocument-sub-hookMessageBreak`#1' does not exist}%
                          AtBeginDocumentSubHook{#1}{#2}%
                          }{%
                          @latex@info{Replacing stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                          expandafterUD@Exchangeexpandafter{%
                          expandaftertoks@expandafter{thetoks@}%
                          }{%
                          toks@{#2}%
                          UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
                          }%
                          }%
                          }%
                          %%----------------------------------------------------------------------
                          %% Add tokens to an AtBeginDocument-sub-hook:
                          %%......................................................................
                          %% AddToContentOfAtBeginDocumentSubHook{<name of AtBeginDocument-sub-hook>}%
                          %% {<prepend-tokens>}%
                          %% {<append-tokens>}%
                          %% Redefines the AtBeginDocument-sub-hook whose name is
                          %% <name of AtBeginDocument-sub-hook> to additionally deliver <prepend-tokens>
                          %% in front of the tokens that are already in this hook and to additionally
                          %% deliver <append-tokens> behind the tokens that are already in this hook.
                          newcommandAddToContentOfAtBeginDocumentSubHook[3]{%
                          @ifundefined{@begindocumentsubhook@#1}{%
                          @latex@info{stringAtBeginDocument-sub-hook `#1' where tokensMessageBreak shall be added does not exist}%
                          AtBeginDocumentSubHook{#1}{#2#3}%
                          }{%
                          @latex@info{Adding tokens to stringAtBeginDocument-sub-hookMessageBreak`#1'}%
                          expandafterUD@Exchangeexpandafter{%
                          expandaftertoks@expandafter{thetoks@}%
                          }{%
                          toks@{#2}%
                          toks@expandafterexpandafterexpandafter
                          expandafterexpandafterexpandafterexpandafter{%
                          UD@nametheexpandaftertoks@{@begindocumentsubhook@#1}#3%
                          }%
                          UD@namexdef{@begindocumentsubhook@#1}{thetoks@}%
                          }%
                          }%
                          }%
                          AtBeginDocument{%
                          renewcommandRemoveAtBeginDocumentSubHook[1]{%
                          @bsphack
                          UD@namegloballet{@begindocumentsubhook@#1}=UndEFineD
                          @latex@info{Removal of stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                          @esphack
                          }%
                          renewcommandAtBeginDocumentSubHook[2]{%
                          @bsphack
                          @latex@info{Introducing stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                          @esphack
                          #2%
                          }%
                          renewcommandReplaceContentOfAtBeginDocumentSubHook[2]{%
                          @bsphack
                          @latex@info{Replacing stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                          @esphack
                          #2%
                          }%
                          renewcommandAddToContentOfAtBeginDocumentSubHook[3]{%
                          @bsphack
                          @latex@info{Adding to stringAtBeginDocument-sub-hook `#1'MessageBreak obsolete as not in preamble any more}%
                          @esphack
                          #2#3%
                          }%
                          }%
                          makeatother

                          %expandaftershowcsname @begindocumenthookendcsname

                          AtBeginDocumentSubHook{SomeSubHook}{Somepar}

                          AtBeginDocumentSubHook{FirstSubHook}{Firstpar}

                          AtBeginDocumentSubHook{SecondSubHook}{Secondpar}

                          AtBeginDocumentSubHook{ThirdSubHook}{Thirdpar}

                          AtBeginDocumentSubHook{FourthSubHook}{Fourthpar}

                          % Let's cancel/change some of the above at this point

                          RemoveAtBeginDocumentSubHook{SomeSubHook}

                          ReplaceContentOfAtBeginDocumentSubHook{FirstSubHook}{New Firstpar}

                          ReplaceContentOfAtBeginDocumentSubHook{SecondSubHook}{New Secondpar}

                          ReplaceContentOfAtBeginDocumentSubHook{ThirdSubHook}{Thi}

                          AddToContentOfAtBeginDocumentSubHook{ThirdSubHook}{New }{rdpar}

                          %expandaftershowcsname @begindocumenthookendcsname

                          begin{document}

                          end{document}


                          enter image description here







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 16 at 10:29

























                          answered Dec 16 at 0:40









                          Ulrich Diez

                          4,050615




                          4,050615






























                              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

                              數位音樂下載

                              格利澤436b

                              When can things happen in Etherscan, such as the picture below?