Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change LaTeX Minted/Listings Numbering to include current Section

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 hereenter 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.

like image 663
Morrowind789 Avatar asked Jun 06 '14 16:06

Morrowind789


1 Answers

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.

like image 131
Werner Avatar answered Nov 12 '22 21:11

Werner