# Executing a command after a package has been loaded

In my previous question I ended up with

\DeclareMathSymbol{:}{\mathpunct}{operators}{"3A}


But now I'm using the fontspec package which redefines : with \AtBeginDocument itself. So as my own definition comes before fontspec is loaded, even putting it in \AtBeginDocument won't help.

Is there a way to say “Execute this after fontspec has loaded!” or “Execute this at the very end of \AtBeginDocument!”?

-

According to the documentation of afterpackage this should work

\usepackage{afterpackage}
\AfterPackage{fontspec}{%
\AtBeginDocument{\DeclareMathSymbol{:}{\mathpunct}{operators}{"3A}}%
}

-
Great, thank you! –  canaaerus Jul 24 '12 at 9:53

There's also the scrlfile package from KOMA-script:

\usepackage{scrlfile}
\AfterPackage{fontspec}{%
\AtBeginDocument{\DeclareMathSymbol{:}{\mathpunct}{operators}{"3A}}%
}


It has a bunch of other useful commands.

-
Actually I do use scrartcl. So that clears up why I got “\AfterPackage already defined” on \usepackage{afterpackage}. –  canaaerus Jul 24 '12 at 11:58

Put it in \AtBeginDocument after you load fontspec any commands saved by that command are executed in order, so if you do it that way your saved commands will be executed after the packages.

-
Well, that is exactly what I didn't want to do. Actually I'm issuing \DeclareMathSymbol in my document class, which I then \dump with the rest of the preamble to speed up compilation. fontspec can not be dumped, so I put it right before \begin{document}. –  canaaerus Jul 24 '12 at 9:39
ah well an alternative is to patch \document to add your hook stuff just after it runs \@begindocumenthook –  David Carlisle Jul 24 '12 at 9:41