我正在使用minted和listings LaTeX软件包来设置代码片段的样式。
我正在尝试将代码段的编号系统更改为section.numberinsection
。因此,下面示例中的两个代码段将是1.1
和1.2
。如果第2部分中有第三个代码段,则会将其编号为2.1
。
我这样做是为了list of code snippets
与list of tables
和list 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.1
和1.1.2
。< / p>
我仍然需要一种方法来更改编号行为以针对section
命名,但忽略subsection
。
答案 0 :(得分: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
在你的序言中。后者\@addtoreset
会为每个新listing
重置\section
个计数器。如果您使用chngcntr
package:
\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
。但是,我认为它在某种程度上是相似的。