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:
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
.
{\newfloat{listing}{h}{lol}[\minted@float@within]}
which uses a counter ofsection
orchapter
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