I got an error with egreg's solution when loading hyperref and could produce the following MWE to demonstrate it:



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
? 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:



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.

  • 2
    hyperref can be last, but requires using \AtEndPreamble instead of AtBeginDocument (requires the etoolbox package). The reason for this is that hyerref redefines some macros at \AtBeginDocument the \bgroup needs to be after that. When you moved the \AtBeginDocument{\bgroup} to after hyperref your bgroup will be processed after the \AtBeginDocument{} issued by hyperref and hence that also works. Dec 18, 2014 at 18:52
  • 2
    Your last example is what you should use, since it gives hyperref time to redefine \AtBeginDocument after which you use it. It sounds more logical though to use \usepackage{hyperref} \AfterBeginDocument{\bgroup} \AtEndDocument{\egroup}.
    – Werner
    Dec 18, 2014 at 18:59
  • @PeterGrill thanks for the explanation, that makes sense. But why does it work if the aux-file is present? And why is the \bgroup a 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't hyperref use global definitions, if they should be valid even outside the current TeX-group? Dec 18, 2014 at 21:13
  • @Werner thanks for your explanation, too. I didn't understand everything in egreg's solution linked above. Do you think his example would be more logial with \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? Dec 18, 2014 at 21:21
  • 2
    @PatrickHäcker: The post Which packages should be loaded after hyperref instead of before? gives some indication of load order surrounding hyperref. 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, 2014 at 21:29

1 Answer 1


As can be read in Werner's comment, hyperref should be loaded before using the hooks. Alternatively, as Peter Grill mentioned, other hooks can be used.

There is a list of recommended package load order in another comment of Werner. Unfortunately, there does not seem to be a list of the order between hooks and packages. This seems to be another area where LaTeX is broken by design and where nearly no documentation is available. But at least for the concrete case of this question, the answer is now documented.

You must log in to answer this question.

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