8

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

The following used to work:

\documentclass{article}

\newenvironment{mydoc}{%
  \document%
  }{%
  \enddocument%
}%


\begin{mydoc}

This is a test document.

\end{mydoc}

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.

6
  • 1
    \AddToHook{env/mydoc/before}{\begin{document}} \AddToHook{env/mydoc/after}{\end{document}} works, depending on the timing you need – Phelype Oleinik Oct 14 '20 at 15:24
  • alternatively \let\mydoc\document \let\endmydoc\enddocument would work in old or new formats – David Carlisle Oct 14 '20 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? – Phelype Oleinik Oct 14 '20 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). – Thomas F. Sturm Oct 14 '20 at 15:31
  • @DavidCarlisle That would not work for me, because in my application mydoc contains some more code. – Thomas F. Sturm Oct 14 '20 at 15:33
9

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

\begin{mydoc}
  ...
\end{mydoc}

to be a shortcut for

\begin{document}
  \begin{mydoc}
    ...
  \end{mydoc}
\end{document}

then you can do

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

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:

\BeforeBeginEnvironment{mydoc}{\begin{document}}
\AfterEndEnvironment{mydoc}{\end{document}}

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

1
  • Thank you for your really helpful answer (and linking to your timing answer). – Thomas F. Sturm Oct 15 '20 at 9:40

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.