Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

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

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

share|improve this question

marked as duplicate by Martin Schröder, Andrew Swann, Peter Jansson, Svend Tveskæg, Claudio Fiandrino Mar 4 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.

3 Answers 3

up vote 12 down vote accepted

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.

share|improve this answer
    
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 at 16:26
    
Yes, that's the idea. –  egreg Mar 3 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 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 3 at 20:07

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}}%
 }
share|improve this answer
    
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 at 17:17
7  
@SeanAllred package interface design would be easier if there were no users. –  David Carlisle Mar 3 at 17:19
    
Comment of the year. –  Sean Allred Mar 3 at 17:20
\RequirePackage{listings}
\lstset{language=Python}

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

share|improve this answer

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