I have designed a class that needs the package. This package is unique in the way that it needs to be called last so it doesn't get overwritten. I usually define my packages in the class file using the "\RequirePackage". Is there a way to force a package to be called last where it is called in the .cls file with the possibility of other packages being called using the \usepackage in the .tex file?

  • You can use \AtBeginDocument{\RequirePackage{xepersian}}. – Phelype Oleinik Apr 10 '19 at 11:44
  • @PhelypeOleinik I see. Thanks. – Al_Fh Apr 10 '19 at 11:45
  • 2
    @campa \AfterEndPreamble is too late to load a package. You'll get a LaTeX Error: Can be used only in preamble.. The \AtBeginDocument is the latest a package can be loaded because right after that the preamble ends (i.e. preamble-only commands become error messages). – Phelype Oleinik Apr 10 '19 at 11:51
  • 4
    @PhelypeOleinik assuming that none of the packages you load later use the same trick to delay some package loading ... – Ulrike Fischer Apr 10 '19 at 11:55
  • @UlrikeFischer Ah, yes, there's that. If I recall correctly, there's an infamous Brazilian class file which does something like that and loading hyperref on top of it is a nightmare :-) – Phelype Oleinik Apr 10 '19 at 11:58

You can delay the package loading using \AtBeginDocument:


this will put the package loading after the .aux file is read but before the “proper end” of the preamble, where all the preamble-only commands are redefined to throw an error; from this point on any \usepackage (or \RequirePackage) will not work.

If the package, for some reason, needs to be loaded before the \begin{document} even starts, then you can load the etoolbox package and use:


this will execute the \RequirePackage before executing anything from the \begin{document}. The effect is the same as if you had put the \RequirePackage line right before \begin{document} in your .tex file.

| improve this answer | |
  • What do you mean by the "proper end" of the preamble? Can you clarify? – Al_Fh Apr 10 '19 at 12:12
  • @Al_Fh I just invented that name :P I clarified the answer. – Phelype Oleinik Apr 10 '19 at 12:17
  • Of course, you can always load xpersian in the preamble (before hyperref). It will not be loaded twice. – John Kormylo Apr 10 '19 at 16:49

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.