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?

0

3 Answers 3

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.

4
  • 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…) Commented Mar 3, 2014 at 16:26
  • Yes, that's the idea.
    – egreg
    Commented Mar 3, 2014 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 :) Commented Mar 3, 2014 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
    Commented Mar 3, 2014 at 20:07
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}}%
 }
3
  • 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. Commented Mar 3, 2014 at 17:17
  • 9
    @SeanAllred package interface design would be easier if there were no users. Commented Mar 3, 2014 at 17:19
  • Comment of the year. Commented Mar 3, 2014 at 17:20
11
\RequirePackage{listings}
\lstset{language=Python}

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 .