4

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.

  • 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. – Peter Grill Dec 18 '14 at 18:52
  • 1
    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 '14 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? – Patrick Häcker Dec 18 '14 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? – Patrick Häcker Dec 18 '14 at 21:21
  • 1
    @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 '14 at 21:29
2

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.

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.