Change LaTeX Minted/Listings Numbering to include current Section
Asked Answered
A

1

7

I am using minted along with listings LaTeX packages to style code snippets in my paper.

I am trying to change the numbering system of the code snippets to be section.numberinsection. So the two code snippets in my example below would be 1.1 and 1.2. If there was a third snippet in section 2 it would be numbered 2.1.

I am doing this so the list of code snippets has a similar numbering scheme to the list of tables and list of figures that are already in my paper and have the behavior I am seeking by default.

Currently the counter for the snippets just increments from 1 for each snippet in the paper.

I am trying to alter the output using this particular line in the code below: \AtBeginDocument{\renewcommand*{\thelstlisting}{\thesection.\arabic{lstlisting}}} but it doesn't seem to be working.

Thoughts?

Example LaTeX:

\documentclass{thesis}

% Imports
\usepackage{listings}
\usepackage{minted}

% Style Minted
\usemintedstyle{default}
\definecolor{codebg}{rgb}{0.96,0.96,0.96}
\newminted{python}{bgcolor=codebg,
                    linenos=true,
                    frame=lines,
                    numbersep=5pt,
                    fontsize=\footnotesize}
\renewcommand\listoflistingscaption{LIST OF CODE SNIPPETS}
\renewcommand\listingscaption{Code Snippet}

% Style Listings
\AtBeginDocument{\renewcommand*{\thelstlisting}{\thesection.\arabic{lstlisting}}}


\begin{document}
\listoflistings

\chapter{A Chapter}
\section{A Section}
\subsection{A Sub-Section}

Nam pulvinar euismod facilisis. Quisque vel sagittis diam. In ut egestas sem. Cras sit amet purus elementum, tempor nisi at, imperdiet diam. Mauris rhoncus vitae erat vel laoreet. Suspendisse interdum aliquet bibendum. Quisque venenatis leo eget neque blandit ullamcorper.

\begin{listing}[H]
\begin{pythoncode}
def get_path_leaf(path):
    """ return the leaf of a path. """
    if not isinstance(path, str):
        path = str(path)
    head, tail = ntpath.split(path)
    return tail or ntpath.basename(head)
\end{pythoncode}
\caption{SPARQL Endpoint}
\label{lst:SPARQL Endpoint}
\end{listing}

Aenean pharetra at mauris at posuere. Vivamus ante libero, posuere et luctus in, condimentum in lacus. Nam rhoncus mi nunc, consequat rhoncus sapien lobortis eu. Fusce feugiat orci nec sollicitudin cursus. Nunc at ligula mi. Vestibulum nec pharetra lacus. Suspendisse a ultrices massa. Nulla mauris purus, tempus quis convallis id, pellentesque vel nibh.

\begin{listing}[H]
\begin{pythoncode}
def get_path_leaf(path):
    """ return the leaf of a path. """
    if not isinstance(path, str):
        path = str(path)
    head, tail = ntpath.split(path)
    return tail or ntpath.basename(head)
\end{pythoncode}
\caption{SPARQL Endpoint}
\label{lst:SPARQL Endpoint}
\end{listing}

\end{document}

Output:

enter image description here enter image description here

Edit:

It appears minted has support for what I need. minted already uses the listings package to do what it does so I needed to stop importing listings explicitly, and changed the \usepackage{minted} to \usepackage[chapter]{minted}. If I used section it would number listings against the section, but it would also include the subsection (e.g. 1.1.1 and 1.1.2 from the above example.

I still need a way to change the numbering behavior to name against the section, but ignore the subsection.

Abessive answered 6/6, 2014 at 16:14 Comment(0)
A
8

First off, your sections are numbered \thechapter.\arabic{section}, so I assume numbering of \thesection.<listing> would be more like 1.1.1, 1.1.2, ...

When using minted, the counter at play is not lstlisting, but rather listing. So, you need

\makeatletter
\renewcommand*{\thelisting}{\thesection.\arabic{listing}}
\@addtoreset{listing}{section}
\makeatother

in your preamble. The latter \@addtoreset resets the listing counter with every new \section. You could also do this in one step if you use the chngcntr package:

\usepackage{chngcntr}% http://ctan.org/pkg/chngcntr
\counterwithin{listing}{section}

The \counterwithin does both of the aforementioned operations (representation of \thelisting as well as the resetting).

enter image description here

\documentclass{report}

% Imports
%\usepackage{listings}
\usepackage{minted,xcolor,chngcntr}

% Style Minted
\usemintedstyle{default}
\definecolor{codebg}{rgb}{0.96,0.96,0.96}
\newminted{python}{bgcolor=codebg,
                    linenos=true,
                    frame=lines,
                    numbersep=5pt,
                    fontsize=\footnotesize}
\renewcommand\listoflistingscaption{LIST OF CODE SNIPPETS}
\renewcommand\listingscaption{Code Snippet}

% Style Listings
\counterwithin{listing}{section}

\begin{document}
\listoflistings

\chapter{A Chapter}
\section{A Section}
\subsection{A Sub-Section}

Nam pulvinar euismod facilisis. Quisque vel sagittis diam. In ut egestas sem. Cras sit amet purus elementum, tempor nisi at, imperdiet diam. Mauris rhoncus vitae erat vel laoreet. Suspendisse interdum aliquet bibendum. Quisque venenatis leo eget neque blandit ullamcorper.

\begin{listing}[H]
\begin{pythoncode}
def get_path_leaf(path):
    """ return the leaf of a path. """
    if not isinstance(path, str):
        path = str(path)
    head, tail = ntpath.split(path)
    return tail or ntpath.basename(head)
\end{pythoncode}
\caption{SPARQL Endpoint}
%\label{lst:SPARQL Endpoint}
\end{listing}

Aenean pharetra at mauris at posuere. Vivamus ante libero, posuere et luctus in, condimentum in lacus. Nam rhoncus mi nunc, consequat rhoncus sapien lobortis eu. Fusce feugiat orci nec sollicitudin cursus. Nunc at ligula mi. Vestibulum nec pharetra lacus. Suspendisse a ultrices massa. Nulla mauris purus, tempus quis convallis id, pellentesque vel nibh.

\begin{listing}[H]
\begin{pythoncode}
def get_path_leaf(path):
    """ return the leaf of a path. """
    if not isinstance(path, str):
        path = str(path)
    head, tail = ntpath.split(path)
    return tail or ntpath.basename(head)
\end{pythoncode}
\caption{SPARQL Endpoint}
%\label{lst:SPARQL Endpoint}
\end{listing}

\section{Another section}

Aenean pharetra at mauris at posuere. Vivamus ante libero, posuere et luctus in, condimentum in lacus. Nam rhoncus mi nunc, consequat rhoncus sapien lobortis eu. Fusce feugiat orci nec sollicitudin cursus. Nunc at ligula mi. Vestibulum nec pharetra lacus. Suspendisse a ultrices massa. Nulla mauris purus, tempus quis convallis id, pellentesque vel nibh.

\begin{listing}[H]
\begin{pythoncode}
def get_path_leaf(path):
    """ return the leaf of a path. """
    if not isinstance(path, str):
        path = str(path)
    head, tail = ntpath.split(path)
    return tail or ntpath.basename(head)
\end{pythoncode}
\caption{SPARQL Endpoint}
%\label{lst:SPARQL Endpoint}
\end{listing}

\end{document} 

You'll notice I used the report document class, since I don't have thesis. However, I assume it would have been similar to some extent.

Acarpous answered 7/6, 2014 at 2:8 Comment(1)
Reading through minted.sty was also helpful. floated listing are created with: {\newfloat{listing}{h}{lol}[\minted@float@within]} which uses a counter of section or chapter in the variable: \minted@float@within]. Understanding what is going on with the counters and your answer helped me get where I want to be.Abessive

© 2022 - 2024 — McMap. All rights reserved.