Modify LaTeX Minted / Numbers Numbering to include the current section

I use minted along with listings LaTeX packages to create code snippets in my article.

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

I do this, so the list of code snippets has a similar numbering scheme with list of tables and list of figures , which are already in my article and have the behavior I'm looking for by default.

Currently, the counter for fragments simply increases from 1 for each fragment in the document.

I am trying to change the output using this particular line in the code below: \AtBeginDocument{\renewcommand*{\thelstlisting}{\thesection.\arabic{lstlisting}}} , but it does not seem to work.

Thoughts?

LaTeX example:

 \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:

minted support what I need. minted already uses the listings package to do what it does, so I had to explicitly ban the import of listings and change \usepackage{minted} to \usepackage[chapter]{minted} . If I used section , it would assign lists against section , but it would also include subsection (e.g. 1.1.1 and 1.1.2 from the example above.

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

+6
source share
1 answer

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

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

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

in the preamble. The last \@addtoreset resets the listing counter with each new \section . You can also do this in one step if you use the chngcntr package:

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

\counterwithin performs both of the above operations (view \thelisting as well as reset).

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 will notice that I used the report document class, since I don't have thesis . However, I suppose that would be like some degree.

+6
source

Source: https://habr.com/ru/post/970468/


All Articles