1

I Want to recreate the nested Listing captions like in the image below, I tried doing this:

\documentclass{article}
\usepackage{listings}
\usepackage{subcaption}

\begin{document}

\lstlistoflistings

\section{Example Python programs}

Here are two example Python programs that print "Hello, World!":

\begin{figure}[htbp]
  \centering
  \begin{subfigure}[b]{0.45\textwidth}
    \begin{lstlisting}[language=Python,caption={[First program] Python program to print "Hello, World!"},label={lst:hello1}]
      print("Hello, World!")
    \end{lstlisting}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.45\textwidth}
    \begin{lstlisting}[language=Python,caption={[Second program] Python program to print "Hello, World!"},label={lst:hello2}]
      print("Hello, World!")
    \end{lstlisting}
  \end{subfigure}
  \caption{Example Python programs.}
  \label{fig:example}
\end{figure}

We can reference the first program using Listing \ref{lst:hello1}, and the second program using Listing \ref{lst:hello2}.

\end{document}

However, this creates two listing side by side. Any help is appreciated.

enter image description here

2
  • 1
    Your image looks pretty much like lstlistings without any figure or subfigure environment, and with \counterwithin{lstlisting}{section} at the beginning of your document, see i.stack.imgur.com/TfXqf.png Mar 14, 2023 at 19:38
  • If you really want to have listings and sublistings, that can be done. One would have to create a new float type (not lstlisting) and a new subcaption to match. Mar 14, 2023 at 19:50

1 Answer 1

1

This uses the lstlisting custom caption.

\documentclass{article}
\usepackage{listings}
\usepackage{newfloat}

%\renewcommand{\lstlistingname}{Sublisting}
\DeclareFloatingEnvironment[fileext=lol,listname=Listings,name=Listing,placement=htp]{listing}

\usepackage{caption}
\captionsetup[listing]{position=above}
\AtBeginDocument{\counterwithin{lstlisting}{listing}}% not defined earlier

\begin{document}

\listoflistings

\section{Example Python programs}

Here are two example Python programs that print "Hello, World!":

\begin{listing}
  \caption{Example Python programs.}% must be above
  \label{fig:example}
  
    \begin{lstlisting}[language=Python,caption={[First program] Python program to print "Hello, World!"},label={lst:hello1}]
      print("Hello, World!")
    \end{lstlisting}
    
    \begin{lstlisting}[language=Python,caption={[Second program] Python program to print "Hello, World!"},label={lst:hello2}]
      print("Hello, World!")
    \end{lstlisting}
\end{listing}

We can reference the first program using Listing \ref{lst:hello1}, and the second program using Listing \ref{lst:hello2}.

\end{document}

This uses \subcaption. Note that the lstlisting counter is not incremented.

\documentclass{article}
\usepackage{listings}
\usepackage{newfloat}

\renewcommand{\lstlistingname}{Sublisting}

\DeclareFloatingEnvironment[fileext=lol,listname=Listings,name=Listing,placement=htp]{listing}

\usepackage{subcaption}
\captionsetup[listing]{position=above}
\DeclareCaptionSubType{listing}
\renewcommand{\thesublisting}{\thelisting.\arabic{sublisting}}
\captionsetup[sublisting]{singlelinecheck=off,labelformat=simple,labelsep=colon,skip=0pt,list=true}
\def\sublistingname{Listing}

\begin{document}

\listoflistings

\section{Example Python programs}

Here are two example Python programs that print ``Hello, World!'':

\begin{listing}
  \caption{Example Python programs.}% must to above
  \label{fig:example}
    \subcaption[First program]{Python program to print ``Hello, World!''}\label{lst:hello1}
    \begin{lstlisting}[language=Python]
      print("Hello, World!")
    \end{lstlisting}
    
    \subcaption[Second program]{Python program to print "Hello, World!"}\label{lst:hello2}
      
    \begin{lstlisting}[language=Python]
      print("Hello, World!")
    \end{lstlisting}
\end{listing}

We can reference the first program using Listing \ref{lst:hello1}, and the second program using Listing \ref{lst:hello2}.

\end{document}

You must log in to answer this question.

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