This question already has an answer here:

I'm writing a thesis class file and I'd like to delay the running of code to after a specific package is loaded:


Is there a command that already exists for the task? If there isn't, how could it be effected?

marked as duplicate by Martin Schröder, Andrew Swann, Peter Jansson, Svend Tveskæg, Claudio Fiandrino Mar 4 '14 at 13:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


With scrlfile you get \AfterPackage, that's executed only if the package is loaded, and at the end of it. So



should be what you want.

  • Just to be clear, #2 here is executed immediately after the package is loaded? (I tried looking at the source of scrlfile.sty, but it is absolutely foreign to me…) – Sean Allred Mar 3 '14 at 16:26
  • Yes, that's the idea. – egreg Mar 3 '14 at 16:27
  • This is definitely what I need then :) Thanks! It should be noted that this doesn't seem to work inside an l3keys spec, but that's another question entirely :) – Sean Allred Mar 3 '14 at 16:48
  • @SeanAllred there are also variants \AfterPackage* (use code immediately if package is already loaded), \AfterPackage+ (like star variant but if the package isn't completely loaded, yet, the code will not be used immediately but at the end of the package before \AtEndOfPackage hook of the package) and \AfterPackage! (like + variant but code will be used after \AtEndOfPackage hook of the package) – clemens Mar 3 '14 at 20:07

If you need the package to be loaded, do as Herbert showed,


If you want to conditionally run stuff after the package if the user loads that in the preamble then there is not (by default) an end of package hook usable for this (The hook used by \AtEndOfPackage is cleared as the package is loaded) So the normal thing is to use the begin document hook

  • This is a great option, however it might confuse the user if they specifically \lstset something in the preamble and it is then overridden by the hook. – Sean Allred Mar 3 '14 at 17:17
  • 9
    @SeanAllred package interface design would be easier if there were no users. – David Carlisle Mar 3 '14 at 17:19
  • Comment of the year. – Sean Allred Mar 3 '14 at 17:20

If it is already loaded before \RequirePackage nothing happens and \lstset will be used as usual.

Not the answer you're looking for? Browse other questions tagged or ask your own question.