6

我在我的论文中使用minted列表LaTeX 包来设置代码片段的样式。

我正在尝试将代码片段的编号系统更改为section.numberinsection. 所以下面我的示例中的两个代码片段将是1.11.2。如果第 2 节中有第三个片段,它将被编号2.1

我这样做是为了与我的论文中已经存在的和list of code snippets具有相似的编号方案,并且默认情况下具有我正在寻求的行为。list of tableslist of figures

目前,对于论文中的每个片段,片段的计数器仅从 1 递增。

我正在尝试使用下面代码中的这一特定行来更改输出:\AtBeginDocument{\renewcommand*{\thelstlisting}{\thesection.\arabic{lstlisting}}}但它似乎不起作用。

想法?

示例乳胶:

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

输出:

在此处输入图像描述 在此处输入图像描述

编辑:

它似乎minted支持我需要的东西。minted已经使用该listings包来完成它的工作,因此我需要listings明确停止导入,并将其更改\usepackage{minted}\usepackage[chapter]{minted}. 如果我使用section它,它将对列表进行编号section,但它也将包括subsection(例如1.1.11.1.2来自上面的示例。

我仍然需要一种方法来更改编号行为以针对 命名section,但忽略subsection.

4

1 回答 1

6

首先,您的部分已编号\thechapter.\arabic{section},因此我认为编号\thesection.<listing>会更像1.1.1, 1.1.2, ...

使用minted时,计数器在起作用lstlisting,而不是listing。所以,你需要

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

在你的序言中。后者用每个 new\@addtoreset重置计数器。如果您使用包,您也可以一步完成:listing\sectionchngcntr

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

执行上述\counterwithin两个操作(表示\thelisting以及重置)。

在此处输入图像描述

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

你会注意到我使用了report文档类,因为我没有thesis. 但是,我认为它在某种程度上是相似的。

于 2014-06-07T02:08:10.880 回答