Since the last LaTeX Update, wrapping \document into another environment fails, possibly because of the new hook management system.

The following used to work:




This is a test document.


I use this mechanism since nearly 20 years to create math exercises for students which can be compiled standalone or included into other documents (varying the mydoc environment). Since updating today, I get

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

Since I have about 1000 such documents, I would really appreciate a (new?) way to wrap the document environment into a private one.

  • 1
    \AddToHook{env/mydoc/before}{\begin{document}} \AddToHook{env/mydoc/after}{\end{document}} works, depending on the timing you need Oct 14, 2020 at 15:24
  • alternatively \let\mydoc\document \let\endmydoc\enddocument would work in old or new formats Oct 14, 2020 at 15:29
  • Also \newenvironment{mydoc}{\endgroup\document}{\enddocument} seems to work. But again, depends on what exactly you do to \document in your custom environment. Could you clarify, please? Oct 14, 2020 at 15:30
  • @PhelypeOleinik Just tested it (your first code) for my applications and it works smoothly. You saved my day! Please provide this as an answer. I should familiarize myself with the new hook system (read about it today for the first time). Oct 14, 2020 at 15:31
  • @DavidCarlisle That would not work for me, because in my application mydoc contains some more code. Oct 14, 2020 at 15:33

1 Answer 1


It depends what exactly your mydoc environment does with \document and when. If you just want for


to be a shortcut for


then you can do


and it will execute \begin{document} right before \begin{mydoc}, and \end{document} right after \end{mydoc}. See my answer on the timing of the environment hooks.

You can find the documentation for lthooks here (or with texdoc lthooks).

This is equivalent to the interface previously provided by etoolbox:


except that now it uses lthooks rather than patching \begin and \end (which would often lead to

Package etoolbox Warning: Patching '\begin' failed!
(etoolbox)                '\BeforeBeginEnvironment' will not work.

if any slightly incompatible package were loaded).

  • Thank you for your really helpful answer (and linking to your timing answer). Oct 15, 2020 at 9:40

You must log in to answer this question.

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