There was a need to create an environment that uses a different environment inside. I can implement this, for example, like this:

\newenvironment{new}{ \begin{old} }{ \end{old} }

And everything works fine until old becomes document. In this case, the code stops working:

! LaTeX Error: \begin{Document} on input line 2 ended by \end{document}

Why does the above code work fine in the case of non-document, but not in the case of document?

P.S. I will also give a solution that was found on the vastness of this site:

\AddToHook{env/Document/before}{ \begin{document} }
\AddToHook{env/Document/after}{ \end{document} }

1 Answer 1


There is a very good reason for this approach not be working.

Every environment makes a group, with one exception, namely document. Why? Because typesetting the whole document inside a group would quite easily lead to filling the memory, for TeX would need to retain every assignment in order to restore it at the end of the group (and for no purpose, since the document has ended).

Besides the approach not being workable, I can't see any good reason for hiding document inside another environment, because this would lead to exactly the same problem the LaTeX kernel wants to avoid.

  • Moving away from MWE, in my case, the need to define the environment in this way was that a friend is writing a book and wants to be able to collect both the whole book and individual chapters. The main goal was to ignore nested document environments. This is the answer to the question why it was necessary to create a synonym for document. Mar 12, 2022 at 0:02
  • @AlexeyIsmagilov Not really the best approach, in my opinion.
    – egreg
    Mar 12, 2022 at 0:03
  • How then is it better to implement it? Mar 12, 2022 at 0:23
  • I understand and fully agree that the behavior of document should be special. But what exactly is the reason why it doesn't work? For example, what prevents the engine from placing the document inside the group. Yes, it is more expensive, but the rules of the game are not violated. Mar 12, 2022 at 0:24
  • @AlexeyIsmagilov there are several packages on CTAN that does what you are looking for. ctan.org/pkg/childdoc?lang=en ctan.org/pkg/subdocs ctan.org/pkg/subfiles just to list a few. Mar 12, 2022 at 3:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .