TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It seems as if the listings package can not be loaded after all other packages using \AtBeginDocument

\documentclass[]{scrbook} % 

This raises the error

! Undefined control sequence.

<argument> ... \iffalse }{}\lst@ifnumberbychapter

In the code sequence

           {\ifnum \c@chapter>\z@ \thechapter.\fi \@arabic\c@lstlisting}

Is this a bug or am I misusing \AtBeginDocument ?

share|improve this question
up vote 6 down vote accepted

You probably are misusing \AtBeginDocument :-)

I had no idea packages can be loaded then; but it seems they can be. listings, however, uses \AtBeginDocument itself. I'd say that's why your code doesn't work.

Actually, I have checked: yes, listings \lets \lst@ifnumberbychapter in \AtBeginDocument.

share|improve this answer
I had problems with options clashes which could most easily be resolved by loaded the code of a whole package using \AtBeginDocument. since listings does not really depend on other packages and itself has no options clash problem I can load it directly at the loading of my package. – Matthias Pospiech Jan 29 '13 at 16:17
@MatthiasPospiech with etoolbox you have AtEndPreamble. – jfbu Jan 29 '13 at 20:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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