\thelstlisting undefined for nostarch

I'm trying to get nostarch working and it won't even get out of the gate. I'm just a lowly user so these kinds of errors leave me quite clueless.

The sample doc in the distribution nssample.tex gives me the same error as this simple example does:

\documentclass[nocfonts]{nostarch}
\begin{document}
\end{document}


And I get the error:

! LaTeX Error: \thelstlisting undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...

l.580 \renewcommand \thelstlisting


Clearly nostarch is pulling in listings (or is it listing?), or maybe more correctly, it needs it but it isn't pulling it in? I'm using a fully updated TeXLive 2011 on OS X Lion.

-

Version 1.4 of listings.sty includes the following code snippet:

\AtBeginDocument{
\@ifundefined{thechapter}{\let\lst@ifnumberbychapter\iffalse}{}
\lst@ifnumberbychapter
\newcounter{lstlisting}[chapter]
\gdef\thelstlisting%
{\ifnum \c@chapter>\z@ \thechapter.\fi \@arabic\c@lstlisting}
\else
\newcounter{lstlisting}
\gdef\thelstlisting{\@arabic\c@lstlisting}
\fi}


Version 1.3 of nostarch.cls contains the following code snippet:

\renewcommand \thelstlisting
{\ifnum \c@chapter>\z@ \thechapter-\fi \@arabic\c@lstlisting}


In other words, nostarch tries to redefine a counter (lstlisting) in the preamble which will only be defined by listings at the begin of the document body. This results in the error message you observed.

Solution: Copy nostarch.cls to your working directory and rename the copy to, say, mynostarch.cls. In the renamed copy, replace the above code lines with

\AtBeginDocument{%
\renewcommand \thelstlisting
{\ifnum \c@chapter>\z@ \thechapter-\fi \@arabic\c@lstlisting}%
}


Then, replacing nostarch with mynostarch in your example (and actually typesetting something in the document body) should compile without errors.

EDIT: As Peter Grill pointed out in a (now deleted) comment, adding \def\thelstlisting{} before \documentclass will remove the error message in your original example. However, nostarchs attempted redefinition of \thelstlisting (replacing the dot with a dash) won't be applied. Here's an alternative solution that doesn't involve modifying a custom copy of nostarch.sty:

\def\thelstlisting{}

\documentclass[nocfonts]{nostarch}

\makeatletter
\AtBeginDocument{%
\renewcommand \thelstlisting
{\ifnum \c@chapter>\z@ \thechapter-\fi \@arabic\c@lstlisting}%
}
\makeatother

\begin{document}

\chapter{First}

\begin{lstlisting}[caption={A listing}]
foo
\end{lstlisting}

\end{document}

-
Wow... I'm glad I asked :) Googling got me nowhere and this was way beyond my meager LaTeX-ness. Thanks very much! – Derek Wyatt Mar 26 '12 at 0:26