6

Let's say I have these two simple files

  • myclass.cls :

    \LoadClass{article}
    \AtBeginDocument{\typeout{IN CLASS}}
    
  • myfile.tex

    \AtBeginDocument{\typeout{BEFORE CLASS}}
    \documentclass{myclass}
    \AtBeginDocument{\typeout{AFTER CLASS}}
    \begin{document}
    empty
    \end{document}
    

When compiling myfile.tex I hoped to see messages in this order:

  1. BEFORE CLASS
  2. IN CLASS
  3. AFTER CLASS

I rather got this order:

  1. IN CLASS
  2. BEFORE CLASS
  3. AFTER CLASS

What did I miss?

Just for the sake of completeness, I get this result using pdfTeX 3.14159265-2.6-1.40.21 (TeX Live 2020/Debian) and kpathsea version 6.3.2.

1 Answers1

7

With the hook management system \AtBeginDocument is an alias fro \AddToHook{begindocument} and all entries with the same label (defaulting to toplevel or your class name in this case) are executed together so they can be ordered by hook rules.

You can use the new optional argument to put the two calls in separate labels

\AtBeginDocument[A]{\typeout{BEFORE CLASS}}
\documentclass{myclass}
\AtBeginDocument[B]{\typeout{AFTER CLASS}}
\begin{document}
empty
\end{document}

produces

BEFORE CLASS
IN CLASS
AFTER CLASS
David Carlisle
  • 757,742
  • I just read lthooks-doc thank's to your link to ltnews32.

    I do not get why in this answer you use different labels?

    Using the same one in every \AtBeginDocument occurence in my two files (or using the myclass label only in the .tex file ones), gives the expected results.

    If I get it right, it is a way to enforce not having the \AtBeginDocument in the class file to be treated separately from the others.

    Am I right?

    Is using the myclass label considered bad usage of hooks?

    – Bruno BEAUFILS Jun 25 '21 at 22:05
  • I re-read section 2.1.4 (The top-level label) and 2.3 (On the order of hook code execution) of lthooks-doc.pdf. I think I get the point. With the hook system all we need to do to respect order of appearance is to remove them from the top-level label which is always executed last. Any other one, even different is sufficient . – Bruno BEAUFILS Jun 25 '21 at 22:21
  • if you put them both in A then A will be done first then myclass so you will get BEFORE CLASS, AFTER CLASS, IN CLASS @BrunoBEAUFILS – David Carlisle Jun 26 '21 at 09:32
  • Thank's for the clarification. Order is using alphabectic order? Is using myclass as label outside the class file itself a good practice in such case? – Bruno BEAUFILS Jun 28 '21 at 09:57