0

I need to add a lot of listing environments to a report, and I want to have the numbering of the listing captions to follow the same structure as the captions of figures or tables (eg Listing 1.1, Listing 1.2, Listing 2.1) rather than the current structure, which just increases the number by one for each added listing.

I'm not sure how necessary a MWE is for a (seemingly) simple question like this, but below there is one:

\documentclass[]{article}
\usepackage{listings}
\begin{document}

\section{Section 1}

\begin{lstlisting}[caption = CAPTION., captionpos = b] CODE \end{lstlisting} % I want this to say Listing 1.1, says Listing 1

\begin{lstlisting}[caption = CAPTION., captionpos = b] CODE \end{lstlisting} % I want this to say Listing 1.2, says Listing 2

\section{Section 2}

\begin{lstlisting}[caption = CAPTION., captionpos = b] CODE \end{lstlisting} % I want this to say Listing 2.1, says Listing 3

\end{document}

Below is how the MWE compiles, for clarity: enter image description here

sg37
  • 13
  • Welcome. // Your MWE looks fine to me. You could only improve it by posting a screenshot of the text it creates (then may be your question is even easier to grasp). // You are on a good track. – MS-SPO Jun 06 '23 at 12:50

1 Answers1

0

This is a simple solution. For a more comprehensive see Listings numbering

a

\documentclass[]{article}
\usepackage{listings}

\AtBeginDocument{%added <<<<<<<<<<<<<< \counterwithin*{lstlisting}{section} \renewcommand{\thelstlisting}{\thesection.\arabic{lstlisting}} }

\begin{document}

\section{Section 1}

\begin{lstlisting}[caption = CAPTION., captionpos = b]
    CODE
\end{lstlisting} % I want this to say Listing 1.1, says Listing 1

\begin{lstlisting}[caption = CAPTION., captionpos = b]
    CODE
\end{lstlisting} % I want this to say Listing 1.2, says Listing 2


\section{Section 2}

\begin{lstlisting}[caption = CAPTION., captionpos = b]
    CODE
\end{lstlisting} % I want this to say Listing 2.1, says Listing 3

\end{document}

Simon Dispa
  • 39,141