[Tex/LaTex] How to automatically add text immediately after \begin{document}

environmentshooks

I attempted to redefine the document environment as follows, but get a complaint that:

LaTeX Error: \begin{MYdocument} on input line 13 ended by \end{document}.

\documentclass[12pt]{article}
\newtoks{\SpecialText} 
\SpecialText={Text to be printed after begin document}

\newenvironment{MYdocument}{%
  \begin{document}{}
  Note: \the\SpecialText
}{%
  \end{document}
} 

\begin{MYdocument}
 ... file contents ...
\end{MYdocument}

I want this to produce the following output

Note: Text to be printed after begin document

… file contents …

If I could do this by just slightly modifying the environment that would be preferable so that I could just use \begin{document} ... \end{document}.

Best Answer

You can use the \AtBeginDocument command before the \begin{document} line to queue up code (even multiple pieces of code) so that it it executed when that line is hit. Here's a reference.

Related Question