How do I reset the current AtBeginDocument?











up vote
9
down vote

favorite












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



documentclass{memoir}

AtBeginDocument{First}

AtBeginDocument{Second}

AtBeginDocument{Third}

% I want to cancel the above at this point

AtBeginDocument{New First}

AtBeginDocument{New Second}

AtBeginDocument{New Third}

begin{document}

end{document}


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



How do I do this?










share|improve this question


























    up vote
    9
    down vote

    favorite












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



    documentclass{memoir}

    AtBeginDocument{First}

    AtBeginDocument{Second}

    AtBeginDocument{Third}

    % I want to cancel the above at this point

    AtBeginDocument{New First}

    AtBeginDocument{New Second}

    AtBeginDocument{New Third}

    begin{document}

    end{document}


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



    How do I do this?










    share|improve this question
























      up vote
      9
      down vote

      favorite









      up vote
      9
      down vote

      favorite











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



      documentclass{memoir}

      AtBeginDocument{First}

      AtBeginDocument{Second}

      AtBeginDocument{Third}

      % I want to cancel the above at this point

      AtBeginDocument{New First}

      AtBeginDocument{New Second}

      AtBeginDocument{New Third}

      begin{document}

      end{document}


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



      How do I do this?










      share|improve this question













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



      documentclass{memoir}

      AtBeginDocument{First}

      AtBeginDocument{Second}

      AtBeginDocument{Third}

      % I want to cancel the above at this point

      AtBeginDocument{New First}

      AtBeginDocument{New Second}

      AtBeginDocument{New Third}

      begin{document}

      end{document}


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



      How do I do this?







      macros






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 11 hours ago









      Masroor

      11k64685




      11k64685






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          10
          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
            5 hours ago




















          up vote
          5
          down vote













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



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



          Something like:



          documentclass{memoir}

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

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

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


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






          share|improve this answer




























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





















              Your Answer








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

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

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















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

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              10
              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
                5 hours ago

















              up vote
              10
              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
                5 hours ago















              up vote
              10
              down vote










              up vote
              10
              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 10 hours ago

























              answered 11 hours ago









              moewe

              84.7k9108327




              84.7k9108327












              • 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
                5 hours ago




















              • 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
                5 hours ago


















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






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












              up vote
              5
              down vote













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



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



              Something like:



              documentclass{memoir}

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

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

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


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






              share|improve this answer

























                up vote
                5
                down vote













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



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



                Something like:



                documentclass{memoir}

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

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

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


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






                share|improve this answer























                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









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



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



                  Something like:



                  documentclass{memoir}

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

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

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


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






                  share|improve this answer












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



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



                  Something like:



                  documentclass{memoir}

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

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

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


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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 9 hours ago









                  Paul Stanley

                  13.8k42745




                  13.8k42745






















                      up vote
                      2
                      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
                        2
                        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
                          2
                          down vote










                          up vote
                          2
                          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 5 hours ago









                          Peter Grill

                          163k25432744




                          163k25432744






























                              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

                              Morgemoulin

                              Scott Moir

                              Souastre