I got an error with egreg's solution when loading hyperref and could produce the following MWE to demonstrate it:
\documentclass{article}
\AtBeginDocument{\bgroup}
\AtEndDocument{\egroup}
\usepackage{hyperref}
\begin{document}a\end{document}
When loading hyperref I tried to act according to the hyperref documentation, so it comes last of your loaded packages, which I read as load as late as possible. Nevertheless, I get the following error if no aux-file is present:
! LaTeX Error: Can be used only in preamble.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.4 \HyperFirstAtBeginDocument
{\ifx\hyper@anchor\@undefined
? H
Your command was ignored.
Type I <command> <return> to replace it with another command,
or <return> to continue without it.
After continuing, the error is gone. If the aux file is deleted it reappears.
However, loading hyperref early avoids the error:
\documentclass{article}
\usepackage{hyperref}
\AtBeginDocument{
\bgroup
}
\AtEndDocument{
\egroup
}
\begin{document}
a
\end{document}
So what is the correct order? Is this expected behaviour? Did I do anything wrong?
The most recent hyperref version (6.83m) is used. Article uses version 1.4h. I can provide the full output of \listfiles if that is helpful.
hyperrefcan be last, but requires using\AtEndPreambleinstead ofAtBeginDocument(requires theetoolboxpackage). The reason for this is thathyerrefredefines some macros at\AtBeginDocumentthe\bgroupneeds to be after that. When you moved the\AtBeginDocument{\bgroup}to afterhyperrefyourbgroupwill be processed after the\AtBeginDocument{}issued byhyperrefand hence that also works. – Peter Grill Dec 18 '14 at 18:52hyperreftime to redefine\AtBeginDocumentafter which you use it. It sounds more logical though to use\usepackage{hyperref} \AfterBeginDocument{\bgroup} \AtEndDocument{\egroup}. – Werner Dec 18 '14 at 18:59\bgroupa problem? After all, the group is not left during the whole document processing. Or is there a problem after\AtEndDocument? But if that is the problem, why doesn'thyperrefuse global definitions, if they should be valid even outside the current TeX-group? – Patrick Häcker Dec 18 '14 at 21:13\AfterBeginDocument, too? If so, you should probably edit his solution to avoid that others find the same trap that I found. The general problem is that I do not know the ordering between packages and the various document hooks. I know that there are attempts to document the various package loading orders. Do you know of any documentation or recommendation about ordering between packages and document hooks? – Patrick Häcker Dec 18 '14 at 21:21hyperrefinstead of before? gives some indication of load order surroundinghyperref. However, documents hooks are package-specific and it would be difficult to make a complete list the covers everything. Especially since some packages look for hooks and adapt, while others just use existing hooks as-is... it's almost dealt with on a case-by-case method. By the way, tying into hooks like you do doesn't really have anything to do with package load order. – Werner Dec 18 '14 at 21:29hyperrefif there is a general problem, which IMHO is the case. The other intention was that I learn something about the problem. Noone seems to be interested in the former one and the latter one was successful (although there are some open questions left, but they have not been part of the official question). So I think the best thing would be if one of you would summerize the comments in an answer so that I can close this quesiton. – Patrick Häcker Dec 19 '14 at 10:29