40

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}.

  • I tried to make the title of your question clearer by adding the word "text" - is that OK? – Hendrik Vogt Mar 24 '11 at 9:48
  • The latex-general tag (and also latex3) is for questions about LaTeX and the LaTeX project in general. A good example is Difference between LaTeX, LaTeX2e, LaTeX3?. Your question is about a specific task and therefore should not be tagged with it. – Martin Scharrer Mar 24 '11 at 9:56
55

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.

  • 2
    Thank You. That is a MUCH better solution that what I was attempting... – Peter Grill Mar 24 '11 at 3:39
  • 3
    Note that the reference given is not up to date, see this version. An update of december 1996 states that no typesetting can (should) be done with \AtBeginDocument. Check out this for more information on why you should not do this. – TonioElGringo Mar 3 '15 at 12:21
  • @TonioElGringo: Just saw this now. The first link you provided no longer works. The problem file provided at the second link seems to compile fine by adding \usepackage{tikz}, so not sure if that is a reason to not use \AtBeginDocument{}. No idea why it needs tikz, but if someone posts that question on this site, people will explain or fix the package if it is a bug. – Peter Grill Sep 24 '16 at 8:52
39

As already stated by Aaron, \AtBeginDocument{<code>} can be used to place code "at the begin of the document". This hook is executed after the old .aux is read in and the new one is opened, but before the preamble is officially closed. Therefore the code can still load packages and add already type-settable material which writes into the .aux file, like sectioning commands. So it is actually executed with one foot in the preamble and the other one in the document body.

The etoolbox package provides \AfterEndPreamble{<code>} (and other useful hooks) which is executed after the preamble is closed, so e.g. all preamble-only macros are disabled etc. Actually it is executed at the very end of \begin{document}, only followed by an \ignorespaces. I personally would prefer \AfterEndPreamble to \AtBeginDocument (already heavily used by packages) to add code at the very beginning of the document body.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.