2

I use this excellent approach for inserting external Python code in my document. How to highlight Python syntax in LaTeX Listings \lstinputlistings command

\section{TEST}
\pythonexternal[caption=test, label=lst.test.py]{test.py}

But how can I number the listing in my document?

I have tried:

\uespackage{chngcntr}
\counterwithin{lstlisting}{section}

The result: "! Package chngcntr Error: lstlisting is not a counter."

1
  • lstlisting - I have tried lstlistings, listing... nothing solved this problem...
    – GenXGer
    Commented Jul 14, 2018 at 13:35

1 Answer 1

2

The listings package delays the definition of the lstlisting counter until the AtBeginDocument phase. (See lines 1567 to 1576 in the file listings.sty for the complete details.) Hence, the \counterwithin{lstlisting}{section} instruction has to be wrapped in an \AtBeginDocument instruction in order to be successful.

enter image description here

\documentclass{article}
\usepackage{listings,chngcntr}
\AtBeginDocument{\counterwithin{lstlisting}{section}}
\begin{document}
\setcounter{section}{3} % just for this example
\begin{lstlisting}[caption={Hello World}]
\end{lstlisting}
\end{document}
1
  • Thank you very much! :) \AtBeginDocument{\counterwithin{lstlisting}{section}} solved my problem - thank you :)
    – GenXGer
    Commented Jul 14, 2018 at 14:03

You must log in to answer this question.

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