[Tex/LaTex] Insert something “Before the end of document”

environmentsmacros

I insert the name and licence with the command \nameofcompany at the end of my document like this:

\nameofcompany
\end{document}

Now, I want to generalize it to some documents, for that I found \AtEndDocument{\nameofcompany} but it doesn't have exactly the same result than \nameofcompany \end{document}. It seems that with \AtEndDocument, the command is only executed after \end{document}, while I want it to be executed before.

I also try to redefine the document environment like this

\renewenvironment{document}%
  {\begin{document}}
  {%
  \nameofcompany
%
  \end{document}%
  }

but it doesn't work either.

Is there a command like \beforTheEndDocument?

Best Answer

\AtEndDocument{<stuff>}

is a shorthand equivalent to

<stuff>
\end{document}

Why? Look at the definition of \enddocument (from latex.ltx):

\def\enddocument{%
   \let\AtEndDocument\@firstofone
   \@enddocumenthook
   \@checkend{document}%
   \clearpage
   \begingroup
     \if@filesw
       \immediate\closeout\@mainaux
       \let\@setckpt\@gobbletwo
       \let\@newl@bel\@testdef
       \@tempswafalse
       \makeatletter \@@input\jobname.aux
     \fi
     \@dofilelist
     \ifdim \font@submax >\fontsubfuzz\relax
       \@font@warning{Size substitutions with differences\MessageBreak
                  up to \font@submax\space have occurred.\@gobbletwo}%
     \fi
     \@defaultsubs
     \@refundefined
     \if@filesw
       \ifx \@multiplelabels \relax
         \if@tempswa
           \@latex@warning@no@line{Label(s) may have changed.
               Rerun to get cross-references right}%
         \fi
       \else
         \@multiplelabels
       \fi
     \fi
   \endgroup
   \deadcycles\z@\@@end}

\@enddocumenthook is the hook that is updated by \AtEndDocument:

\def\AtEndDocument{\g@addto@macro\@enddocumenthook}

You may consider alternative end-of-document hooks by looking into the atveryend package. It provides the following hooks, all of which are executed after the \AtEndDocument hook, except the first:

BeforeClearDocument{<code>}

The code is called before the final \clearpage in \enddocument. However it is unknown, whether the last page is already shipped out or if this will be triggered by the final \clearpage.

\AfterLastShipout{<code>}

The code is called after the final \clearpage of \enddocument before the main .aux file is closed. This is the right place to remember the last page in the .aux file, for instance.

\AtVeryEndDocument{<code>}

The code is called after the .aux file is closed and read in again. It is the place for final checks, rerun hints, final messages.

\AtEndAfterFileList{<code>}

After the .aux file closing and reading LaTeX prints the file list if requested by \listfiles. Then this hook is executed.

\AtVeryVeryEnd{<code>}

This code is called right before the final \@@end.

Refer to the definition of \enddocument in order to see an approximate location where the above hooks are placed.