Consider this script:


I get an error


! LaTeX Error: No counter 'chapter' defined.

if I uncomment the line


What is the problem and how to solve it?

  • 3
    latvian defines \thechapter and this confuses listings. I would say it is a babel-latvian bug. Notify the author. Dec 6, 2018 at 19:31

1 Answer 1


It looks like a bug in Latvian babel (as Ulrike Fischer said). The file latvian.ldf (loaded by babel) defines \thechapter:


independently if you are using a class with or without \chapters. Later on, \AtBeginDocument listings defines the lstlisting counter conditionally if \thechapter is defined:


or if not:


When \thechapter exist (it does, when latvian.ldf is loaded), listings tries to hook the lstlisting counter to the chapter counter, which does not exist and you get the error.

You can work around it with this:



  • 2
    :-/ Not that latvian.ldf should not be fixed (there is no "local" maintainer, and therefore I'm maintaining it in the sense I'm fixings bugs), but there is clearly a bug in the listings side, too: if the counter chapter is used, this is exactly what must be tested, and not another macro. Dec 8, 2018 at 7:51
  • @JavierBezos You're right, it really doesn't make much sense :/ Dec 9, 2018 at 20:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .