11

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

\AtEndOfOtherPackage{listings}{%
  \lstset{language=Python}}

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

lockstep
  • 250,273
Sean Allred
  • 27,421

3 Answers3

12

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

\RequirePackage{listings}
\lstset{language=Python}

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

\AtBeginDocument{\@ifpackageloaded{listings}%
   {\typeout{yes}}%
   {\typeout{no}}%
 }
karlkoeller
  • 124,410
David Carlisle
  • 757,742
12

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

\RequirePackage{scrlfile}

\AfterPackage{listings}{\lstset{language=Python}}

should be what you want.

egreg
  • 1,121,712
  • 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 03 '14 at 16:26
  • Yes, that's the idea. – egreg Mar 03 '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 03 '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) – cgnieder Mar 03 '14 at 20:07
11
\RequirePackage{listings}
\lstset{language=Python}

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