At the end of 2020, LaTeX added support for a general hook management system. The system allows me to execute \maketitle at the beginning of a document if I am in the preamble, or immediately if I am in the document:
\AddToHook{begindocument/end}{\maketitle}
LaTeX hooks are only available in TeX Live 2021 and later. However, I need my code to work at least in unupdated TeX Live 2020 (to support Overleaf) and ideally in TeX Live ≥ 2018. Pragmatically, I could rewrite my code as follows:
\ifx\@onlypreamble\@notprerr
% We are in the document
\maketitle
\else
% We are in the preamble
\RequirePackage{etoolbox}
\AfterEndPreamble{\maketitle}
\fi
However, I would like to gradually convert my code to use LaTeX hooks and using compatibility code will only accumulate technical debt, which I would like to avoid.
Does there exist a polyfill package that adds (limited) support for LaTeX hooks and that would allow me to use LaTeX hooks in historical TeX Live versions? Like this:
\RequirePackage{lthooks-polyfill}
\AddToHook{begindocument/end}{\maketitle}
If not, what is the suggested migration path from code that does not use hooks to code that uses hooks and stays compatible with historical releases of TeX Live?
\AfterEndPreamble, with a new latex this is an alias to the new hook, but it works also in older systems. – Ulrike Fischer Sep 11 '21 at 10:52etoolboxand I am not convinced that\AfterEndPreambledoes the same thing before and after LaTeX 2020-10-01. Before LaTeX 2020-10-01, using\AfterEndPreambleafter the end of the preamble issues an error (\protected\def\AfterEndPreamble{\@notprerr\@gobble}). After 2020-10-01,\AfterEndPreambleexpands to\AddToHook{begindocument/end}, which behaves differently: "[...] after it is executed, all further attempts to add code to it will execute such code immediately". – Witiko Sep 11 '21 at 11:35\AfterEndPreambleafter the preamble is undefined byetoolbox, but it is important in my use case. Specifically, in order to use\AfterEndPreamble, I would need it to behave the same way as\AddToHook{begindocument/end}before LaTeX 2020-10-01. – Witiko Sep 11 '21 at 11:36\AfterEndPreambleis not an answer to the question as stated. Thank you for the heads-up, but I am hoping that there exists a polyfill that would allow me to not pollute the package with compatibility code. – Witiko Sep 11 '21 at 11:41\ifx\@nodocument\relax \maketitle\else \AfterEndPreamble{\maketitle}\fithat will work with old and new formats (needing etoolbox) – David Carlisle Sep 11 '21 at 11:47\AtBeginDocumentand\AfterEndPreambleis well-taken. I have updated the original question. – Witiko Sep 11 '21 at 11:49