gpt4 book ai didi

latex - 更改 LaTeX Minted/Listings 编号以包括当前部分

转载 作者:行者123 更新时间:2023-12-02 19:28:10 25 4
gpt4 key购买 nike

我正在使用minted以及listings LaTeX 包用于在我的论文中设置代码片段的样式。

我正在尝试将代码片段的编号系统更改为section.numberinsection。因此,下面示例中的两个代码片段将是 1.11.2。如果第 2 部分中有第三个片段,则其编号为 2.1

我这样做是为了让代码片段列表具有与表格列表图表列表类似的编号方案,它们是已经在我的论文中并且具有我默认寻求的行为。

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

我试图使用下面代码中的这一特定行来更改输出: \AtBeginDocument{\renewcommand*{\thelSTListing}{\thesection.\arabic{lSTListing}}} 但它没有似乎不起作用。

想法?

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}

输出:

enter image description here enter image description here

编辑:

看起来minted支持我需要的东西。 minted 已经使用 listings 包来执行它的操作,因此我需要停止显式导入 listings,并更改了 \usepackage{ minted}\usepackage[chapter]{minted}。如果我使用 section ,它会根据 section 对列表进行编号,但它也会包含 subsection (例如 1.1.1上面示例中的 code> 和 1.1.2

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

最佳答案

首先,您的部分编号为 \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 的表示以及重置)。

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}

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

关于latex - 更改 LaTeX Minted/Listings 编号以包括当前部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24086366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com