如何让 autoref 在不同的列表环境中使用不同的计数器?

如何让 autoref 在不同的列表环境中使用不同的计数器?

在我的文档中,我想要几个不同的自定义列表环境。解决方案弗拉克塔莱克效果很好,除了一件事:\autoref对我不起作用。我尝试了以下方法:

\newcounter{algorithm}
\lstnewenvironment{algorithm}[1][]{
        \renewcommand\lstlistingname{Algorithm}
        \setcounter{lstlisting}{\value{algorithm}}
        \lstset{#1}
} {\addtocounter{algorithm}{1}}

\newcounter{program}
\lstnewenvironment{program}[1][]{
        \renewcommand\lstlistingname{Program}
        \setcounter{lstlisting}{\value{program}}
        \lstset{#1}
} {\addtocounter{program}{1}}

\def\algorithmautorefname{Algorithm}
\def\programautorefname{Program}

但在我的文档中,

\autoref{alg:firstalgorithm}, \autoref{prg:firstprogram}, \autoref{alg:secondalgorithm}

返回

清单 1.1、清单 1.1、清单 1.2。

单独的编号是正确的,但 autoref 无法识别单独的计数器。但是,如果我使用

\def\lstlistingautorefname{Code fragment}

相反,结果变为

代码片段 1.1、代码片段 1.1、代码片段 1.2。

我曾尝试将此命令放入\lstnewenvirontment命令中,但这又给出了

清单 1.1、清单 1.1、清单 1.2

结果。显然,我希望结果是

算法 1.1,程序 1.1,算法 1.2。

对于如何实现这一目标您有什么想法吗?

答案1

我在这里提出的解决方案适用于相对较小的文档,但不适用于用户想要生成类似\listofalgorithms或 的更复杂的文档\listofprograms。由于原始问题中没有指定这一点,因此这里不包括它。但是,我相信使用float包裹, 例如。

解决方案的一些背景...

一个问题是lstlistinglistings包裹是特殊的。它的内容必须以一种除了 之外不展开的方式进行解析\end{lstlisting}。这就是为什么它不能被分解的原因:

\newenvironment{myenvironment}[1][]{%
  \begin{lstlisting}[#1]% Begin listing
}{\end{lstlisting}}% End listing

在编译过程中会出错。作为安慰,该 listings包提供了一种替代方案,形式为

\lstnewenvironment{<name>}[<number>][<opt. default arg.>]{<start code>}{<end code>}

类似于 LaTeX 的 \newenvironment{nam}[args][opt]{begdef}{enddef} 定义。然而,仅仅在 针对不同上市环境提供不同柜台的解决方案 原因hyperref以以下方式投诉

! pdfTeX warning (ext4): destination with the same identifier
(name{page.1}) has been already used, duplicate ignored

这是因为在新定义的列表环境中使用了相同的计数器lstlisting。事实上,它们是相同的环境,只是名称不同(由于命令 \renewcommand\lstlistingname{<name>})。这个警告​​说明了为什么使用

\def\algorithmautorefname{Algorithm}
\def\programautorefname{Program}

不会影响\autoref{...}使用正确的参考标题,因为\autoref{...}仍然将每个新定义的环境(algorithmprogram)视为lstlisting

一个建议的解决方案是添加一些宏,这些宏既可以打印正确的标题(尽管是手动的),也可以正确地超链接到相应的lstlisting。这是通过为每个新内容引入“预挂钩”\lstnewenvironment并修改传递给它的参数的方式来实现的。通过listings'caption={...}和提供的标题和标签支持label=...被删除,以代替手动替代方案。这样,lstlisting环境在后台总是使用增量不同的计数器,从而避免hyperref重复目标警告。

\documentclass{article}
\usepackage{listings}
\usepackage{hyperref}

\newcommand{\listingcaption}[2]{%
  \parbox{0.95\textwidth}{% Width of caption is 95% of \textwidth
    \leftskip=0pt plus.5fil% These 3 lines allow for a
    \rightskip=0pt plus-.5fil% justification=centerlast option similar
    \parfillskip=0pt plus1fil% to that offered by the `caption` package
    \small \textbf{#1~\thealgorithm}.\ #2% Caption formatting
  }%
}
% ================== ALGORITHM ==================
\newcounter{algorithm}
%\renewcommand{\thealgorithm}{\thesection.\arabic{algorithm}}% Algorithm counter definition
\newcommand{\algorithmprehook}[2]{%
  \refstepcounter{algorithm}% Increment counter for correct reference
  \listingcaption{Algorithm}{#1}% Algorithm caption
  \label{#2}% Label algorithm
}
\lstnewenvironment{algorithm}[3][]{% \begin{algorithm}[<listings options>]{<caption>}{<label>}...
  \algorithmprehook{#2}{#3}% Algorithm pre-hook
  \lstset{#1}% Set listings options
} {}% ...\end{algorithm}
\def\algorithmautorefname{Algorithm}% Autoref caption

% ================== PROGRAM ==================
\newcounter{program}
%\renewcommand{\theprogram}{\thesection.\arabic{program}}% Program counter definition
\newcommand{\programprehook}[2]{%
  \refstepcounter{program}% Increment counter for correct reference
  \listingcaption{Program}{#1}% Program caption
  \label{#2}% Label program
}
\lstnewenvironment{program}[3][]{% \begin{program}[<listings options>]{<caption>}{<label>}...
  \programprehook{#2}{#3}% Program pre-hook
  \lstset{#1}% Set listings options
} {}% ...\end{program}    
\def\programautorefname{Program}% Autoref caption

\begin{document}
\pagestyle{empty}

\begin{algorithm}[]{My first algorithm. This is an extremely long caption, giving a detailed %
description of the context and code. Justification is ``centerlast''}{alg:firstalgorithm}
  Here is some algorithm code;
  Then some more code;
  And it ends here.
\end{algorithm}

\begin{program}[]{My first program}{prg:firstprogram}
  Here is some program code;
  Which is a little shorter.
\end{program}

\begin{algorithm}[]{My second algorithm}{alg:secondalgorithm}
  The final algorithm code is very short.
\end{algorithm}

\autoref{alg:firstalgorithm}, \autoref{prg:firstprogram}, \autoref{alg:secondalgorithm}

\end{document}

具有正确标题、计数器和超链接的替代列表环境(算法+程序)

\listingcaption{<caption label>}{<caption>}提供手动标题支持的宏需要 2 个强制参数。<caption label>是标签的类型(本例中为算法或程序),<caption>是实际标题。标题的格式类似于规范

\captionsetup[<float type>]{%
  font=small,%
  format=plain,%
  labelsep=period,%
  labelfont=bf,%
  justification=centerlast%
}

支持caption包裹。 选项justification=centerlast来自这篇最近的博客文章,最初由 Victor Eijkhout 建议TeX 按主题分类

当使用该float包管理标题、计数器和\listof...条目时,建议也放弃使用和listings选项。caption={...}label=...

答案2

沃纳解决方案之后,我发现自己无法再让新环境漂浮起来。

经过进一步的研究,我现在可以提出一种解决原始问题的新方法。无需使用以下方法创建新的浮点数:\lstnewenvironment{algorithm},您还可以创建一个浮动容器并在其中放置一个非浮动的 lstlisting 环境。沃纳建议使用漂浮包。由于我已经采用了浮行对于我文档中的其他浮点数,我也在这里使用了该包。浮行扩展了浮动布局的可能性,并且与标题

我们首先加载必要的包

\usepackage[font=small,format=plain,labelsep=period,
    labelfont=bf, justification=centerlast]{caption}    
\usepackage{floatrow}
\usepackage{listings}

接下来,我们使用浮行

\DeclareNewFloatType{algorithm}{name=Algorithm, placement=htbp, within=section}
\DeclareNewFloatType{codefragment}{name=Code fragment, placement=htbp,within=section}

该名称将用于标题和参考,指定新的浮点数将在各节内编号。

我们希望将标题放在我们新创建的浮动元素上方。由于浮行处理标题位置,并默认将其放在浮动对象下方(无论浮动对象内部的位置如何\caption),我们必须通过以下方式指定

\floatsetup[algorithm]{style=plaintop}
\floatsetup[codefragment]{style=plaintop}

并且可能还

\floatsetup[table]{style=plaintop}

现在,在我们的文档中,我们可以使用以下代码片段创建算法或代码片段:

\begin{algorithm}
\caption{Hello world!}
\label{alg:myfirstalgorithm}
\begin{lstlisting}
Hello world!
How are you today?
\end{lstlisting}
\end{algorithm}

\begin{codefragment}
\caption{Hello world!}
\label{alg:myfirstalgorithm}
\begin{lstlisting}
Hello world!
How are you today?
\end{lstlisting}
\end{codefragment}

此解决方案允许列表与所需标题一起浮动。创建新浮动类型的列表也很容易:

\listof{algorithm}{List of Algorithms}
\listof{codefragment}{List of Code fragments}

通过使用 floatrow 包创建适当的浮点框,还可以轻松地并排比较代码片段。下面是一个更详细的例子,伪代码和 C++/matlab 代码的格式是不同的。

\documentclass{article}

% math  
\usepackage{amsmath}                    % ams math
\usepackage{amssymb}                    % ams symbols

% fonts & typesetting
\usepackage{mathpazo}               % Palatino & Pazo math
\usepackage[scaled=0.85]{berasans}  % bera sans and mono scaled to fit with Palatino
\usepackage[scaled=0.85]{beramono}
\usepackage{microtype}                  % better typesetting
\usepackage[T1]{fontenc}                % font encoding
\usepackage[utf8x]{inputenc}            % input encoding
\usepackage{textcomp}                   % required for the upquote option of the listings package

\usepackage[colorlinks=true]{hyperref}
\usepackage[font=small,format=plain,labelsep=period,
    labelfont=bf, justification=centerlast]{caption}    % nice captions
\usepackage{floatrow}
\usepackage{listings}

% create new numbered floats for different types of listings
\DeclareNewFloatType{algorithm}{name=Algorithm, placement=htbp, within=section}
\DeclareNewFloatType{codefragment}{name=Code fragment, placement=htbp,within=section}

% captions above code floats
\floatsetup[algorithm]{style=plaintop}
\floatsetup[codefragment]{style=plaintop}


% pseudo language definition
\lstdefinelanguage{pseudo}{
morekeywords={for, for all, while, do, end, if, then, else, return, divides, min, max, require, ensure, print},
sensitive=false,
mathescape=true,
escapechar=`}

% pseudo style definition
\lstdefinestyle{pseudo}{
language=pseudo,
basicstyle=\rmfamily,
keywordstyle=\bfseries,
columns=[l]fullflexible,    
showstringspaces=false,
numbers=left,       
numberstyle=\footnotesize,
tabsize=2,
aboveskip={0pt},
belowskip={0pt},
frame=lines}

% normal code style definition
\lstdefinestyle{realcode}{
    basicstyle=\ttfamily,
    keywordstyle=\bfseries,
    commentstyle=\textit,
    numberstyle=\footnotesize,
    showstringspaces=false,
    numbers=left,
    tabsize=2,
    breaklines=true,
    breakatwhitespace=false,
    aboveskip={0pt},
    belowskip={0pt},
    upquote=true,
    extendedchars=true,
    frame=lines,
    columns=[c]fixed}

% Matlab and C++style definition
\lstdefinestyle{matlab}{language=matlab,style=realcode}     
\lstdefinestyle{C++}{language=C++,style=realcode}       


\begin{document}
\pagestyle{empty}

\listof{algorithm}{List of Algorithms}
\listof{codefragment}{List of Code fragments}

\section{Computing $N!$}

Check out the pseudo code in \autoref{alg:myfirstalgorithm}, and then the Matlab and C++ equivalents in  \autoref{alg:myfirstprogram} and \autoref{alg:mysecondprogram}.

\begin{algorithm}
\caption{Compute $N!$.}
\label{alg:myfirstalgorithm}
\begin{lstlisting}[style=pseudo]
$z \gets 1$ 
for $k\in[1,\dotsc,N]$ do
    $z \gets z\cdot k$
return $z$
\end{lstlisting}
\end{algorithm}

\begin{codefragment}
\caption{Compute $N!$, Matlab implementation.}
\label{alg:myfirstprogram}
\begin{lstlisting}[style=matlab]
function z = computefactorial(N)
    z = 1;
    for k = 1 : N
        z = z * k;
    end
end
\end{lstlisting}
\end{codefragment}

\begin{codefragment}
\caption{Compute $N!$, C++ implementation.}
\label{alg:mysecondprogram}
\begin{lstlisting}[style=C++]
int z = computefactorial(int N)  { 
    z = 1;
    for (int k = 1; k <= N; k++) { 
        z = z * k;
    }
    return z;
}
\end{lstlisting}
\end{codefragment}



\end{document}

输出结果为

代码输出

这种方法的一个缺点是,在文档中,你必须嵌套两个环境。这比沃纳解决方案

理想情况下,\lstnewenvironment应该修改操作以便为新生成的环境创建单独的计数器。

仍然非常欢迎提出建议和意见!这是我第一次深入研究细节,但我很享受!

答案3

几天来我一直在为同样的问题而苦苦挣扎。就我而言,我无法将列表包装在浮动中,因为我希望它们能够跨多个页面。而且我并不希望将标题和标签拆分成单独的参数,例如沃纳解决方案,因为我希望能够使用列表环境的内置标题支持,以便轻松地使用重新设置每个列表环境的样式\captionsetup

我不是 TeXpert,但我设法拼凑了以下似乎可行的解决方案。它基于与讨论过的类似想法别处TeX.SX 上的实现。虽然实现过程有些粗糙,但它基于修补的想法,\lst@MakeCaption以替换所有对\c@lstlisting计数器的硬编码引用。此外,它集成了已经提出的修复这里为每个列表环境提供单独的“...列表”。

我最终得到的是一个\newcustomlstenvironment包装的宏\lstnewenvironment,但将与新列表环境关联的 caption/autoref 名称和用于生成列表列表的辅助文件扩展名作为附加参数。

我尝试解决的一个问题是(虽然不是很优雅),包hyperrefcaption包都会包装\lst@MakeCaption宏。并且根据这两个包的加载顺序,一个包可能会先包装宏,另一个包则不会。因此,识别应该修补的实际重命名宏需要一点工作量\lst@MakeCaption

最后,由于 listings 包没有提供用于\lstinputlisting不同列表环境的方法,因此我还将该宏包装在\lstinput<name of environment>特定于环境的宏中。

因此这里有一个 MWE(尽管它不是那么“最小”):

\documentclass{book}

% Packages needed by our "hack"
\usepackage{xpatch}
\usepackage[hang,bf,small,hypcap]{caption}
\usepackage{listings}
\usepackage{hyperref}

%%%%%%%%%%%%%%%%%%%%%%%% THE HACK %%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter

\AtBeginDocument{
  \global\@namedef{cmdname@lst@MakeCaption}{lst@MakeCaption}

  \@ifpackageloaded{caption}{
    % If hyperref is loaded before caption, then hyperref will have wrapped \lst@MakeCaption first in \NROrg@lst@MakeCaption
    \xpatchcmd\caption@ORI@lst@MakeCaption{\NROrg@lst@MakeCaption}{\NROrg@lst@MakeCaption}
      {%
        \global\@namedef{cmdname@lst@MakeCaption}{NROrg@lst@MakeCaption}
        \global\@namedef{cmdname@caption@lst@MakeCaption}{lst@MakeCaption}% caption package's override of \lst@MakeCaption
      }{%
        \global\@namedef{cmdname@lst@MakeCaption}{caption@ORI@lst@MakeCaption}
        \global\@namedef{cmdname@caption@lst@MakeCaption}{NROrg@lst@MakeCaption}% caption package's override of \lst@MakeCaption
      }
  }{}
  \@ifpackageloaded{hyperref}{
    % If caption is loaded before hyperref, then caption will have wrapped \lst@MakeCaption first in \caption@ORI@lst@MakeCaption
    \xpatchcmd\NROrg@lst@MakeCaption{\caption@ORI@lst@MakeCaption}{\caption@ORI@lst@MakeCaption}
      {\global\@namedef{cmdname@lst@MakeCaption}{caption@ORI@lst@MakeCaption}}{\global\@namedef{cmdname@lst@MakeCaption}{NROrg@lst@MakeCaption}}
  }{}

  % Define convenient macro for patching the actual (potentially wrapped) \lst@MakeCaption
  \gdef\@patch@lst@MakeCaption#1#2{
    \expandafter\xpatchcmd\csname\cmdname@lst@MakeCaption\endcsname{#1}{#2}{}{\@latex@error{Failed to patch \@backslashchar lst@MakeCaption}\@ehd}
  }

  % Define convenient macro for patching the caption package's re-implementation of \lst@MakeCaption
  \gdef\@patch@caption@lst@MakeCaption#1#2{
    \@ifundefined{cmdname@caption@lst@MakeCaption}{}{
      \expandafter\xpatchcmd\csname\cmdname@caption@lst@MakeCaption\endcsname{#1}{#2}{}{\@latex@error{Failed to patch caption's \@backslashchar lst@MakeCaption}\@ehd}
    }
  }

  % Patch ToC-stuff in \lst@MakeCaption
  \global\@namedef{ext@lstlisting}{lol}
  \global\@patch@lst@MakeCaption
    {\addcontentsline{lol}{lstlisting}}
    {\addcontentsline{\@nameuse{ext@lstlisting}}{lstlisting}}
  \global\@patch@lst@MakeCaption
    {\addcontentsline{lol}{lstlisting}}
    {\addcontentsline{\@nameuse{ext@lstlisting}}{lstlisting}}
}

\gdef\@customlstenvironmenthook#1{
  % Override listing name as shown in caption
  \def\lstlistingname{\@nameuse{#1name}}
  % Override counter as shown in caption
  \expandafter\let\csname c@lstlisting\expandafter\endcsname\csname c@#1\endcsname
  \expandafter\let\csname thelstlisting\expandafter\endcsname\csname the#1\endcsname
  \expandafter\let\csname theHlstlisting\expandafter\endcsname\csname theH#1\endcsname
  %
  % Prepend #1- to \theHlstnumber to make this hyperref counter unique too
  % (otherwise, hyperref will complain with "destination with the same identifier
  % (name{lstnumber.X.Y.Z}) has been already used, duplicate ignored").
  % Thanks to Heiko "the man" Oberdiek!!
  % (see https://groups.google.com/forum/#!msg/comp.text.tex/vO5PaIkhfkE/ouW-6192yd4J)
  \begingroup
    \global\let\gtmp@theHlstnumber\theHlstnumber
    \toks@\expandafter{\theHlstnumber}%
    \xdef\gtmp@theHlstnumber{#1-\the\toks@}%
  \endgroup
  \let\theHlstnumber\gtmp@theHlstnumber
  %
  % Patch \lst@MakeCaption to use the counter of our customized lstlisting environment.
  \@patch@lst@MakeCaption
    {\ifx\lst@@caption\@empty\expandafter\lst@HRefStepCounter\else
      \expandafter\refstepcounter\fi {lstlisting}}
    {\ifx\lst@@caption\@empty\expandafter\lst@HRefStepCounter\else
      \expandafter\refstepcounter\fi {#1}}
  %
  % Patch ToC output from \lst@MakeCaption so we get a separate "List of ..." for each environment.
  \@patch@lst@MakeCaption
    {\addcontentsline{\@nameuse{ext@lstlisting}}{lstlisting}\lst@name}
    {\addcontentsline{\@nameuse{ext@#1}}{#1}\lst@name}
  \@patch@lst@MakeCaption
    {\addcontentsline{\@nameuse{ext@lstlisting}}{lstlisting}{\protect\numberline{\thelstlisting}\lst@@caption}}
    {\addcontentsline{\@nameuse{ext@#1}}{#1}{\protect\numberline\expandafter{\csname the#1\endcsname}\lst@@caption}}
  %
  % Now, patch caption's implementation of \lst@MakeCaption, so we may style each environment differently using \captionsetup
  \@patch@caption@lst@MakeCaption
    {\caption@setoptions{lstlisting}}
    {\caption@setoptions{#1}}
  \@patch@caption@lst@MakeCaption
    {\caption@begin{lstlisting}}
    {\caption@begin{#1}}
}

% Usage: \newcustomlstenvironment
%          {<Environment name>}{<Aux extension>}{<Caption/autref name>}
%          [<number>][<opt. default arg.>]{<starting code>}{ending code>} <-- These are passed directly to \lstnewenvironment
\lst@UserCommand\newcustomlstenvironment#1#2#3#4#{%
  \newcustomlstenvironment@{#1}{#2}{#3}{#4}
}
\gdef\newcustomlstenvironment@#1#2#3#4#5#6{
  % Define separate counter for our customized lstlisting environment.
  % Note: only supports numbering with or without chapter number first (same as lstlisting).
  \lst@ifnumberbychapter
    \newcounter{#1}[chapter]
    \global\@namedef{the#1}%
      {\ifnum \c@chapter>\z@ \thechapter.\fi \arabic{#1}}
  \else
    \newcounter{#1}
    \global\@namedef{the#1}{\arabic{#1}}
  \fi
  %
  % Hyperref has separate counter that needs a unique name.
  % Again, thanks to Mr. Oberdiek!
  \global\@namedef{theH#1}%
    {lst.#1-\csname the\@ifundefined{theHchapter}{}{H}chapter\endcsname .\arabic{#1}}
  %
  % Override autoref names for our environment.
  \global\@namedef{#1name}{#3}
  \global\@namedef{#1autorefname}{#3}
  %
  % Define aux extension
  \global\@namedef{ext@#1}{#2}
  %
  % Create new listing environment as usual, by calling \lstnewenvironment
  % Note: #4 contains something like "[N][]", corresponding to [<number>][<opt. default arg.>]
  %       arguments of \lstnewenvironment (see listings doc, §4.16).
  \lstnewenvironment{#1}#4{
    \@customlstenvironmenthook{#1}
    %
    % Contents passed to {<starting code} of \lstnewenvrionment (e.g. \lstset{#1,..})
    #5%
  }{%
    % Contents passed to {<ending code>} of \lstnewenvrionment
    #6%
  }
  \expandafter\newcommand\csname lstinput#1\endcsname[2][]{
    \begingroup
      \@customlstenvironmenthook{#1}
      #5%
      \lstinputlisting[##1]{##2}
      #6%
    \endgroup
  }
}

\makeatother

%%%%%%%%%%%%%%%%%%%%%%%% EXAMPLE %%%%%%%%%%%%%%%%%%%%%%%%

% Packages used by our example
\usepackage{xcolor}
\usepackage{float}
\usepackage{filecontents}

\begin{filecontents*}{samplecode.c}
#include <stdio.h>

int main(void)
{
  printf("Hello world!\n");
  return 0;
}
\end{filecontents*}

\lstset{captionpos=b,numbers=left,backgroundcolor=\color[rgb]{0.9,0.9,1}}

\makeatletter
\newcustomlstenvironment{codesnippet}{locodesnippet}{Code Snippet}[1][]
  {\lstset{#1,language=C,frame=tb,captionpos=t,backgroundcolor=\color[rgb]{1,1,0.9}}%
   \csname lst@SetFirstNumber\endcsname}
  {\csname lst@SaveFirstNumber\endcsname}

\newcustomlstenvironment{algorithm}{loalgorithm}{Algorithm}[1][]
  {\lstset{#1,language=C,frame=tblr,captionpos=t,backgroundcolor=\color[rgb]{0.9,1,0.9}}%
   \csname lst@SetFirstNumber\endcsname}
  {\csname lst@SaveFirstNumber\endcsname}
\makeatother

\begin{document}
\listof{lstlisting}{List of Listings}
\listof{codesnippet}{List of Code Snippets}
\listof{algorithm}{List of Algorithms}

\chapter{Chapter}

\begin{lstlisting}[caption={Test 1},label={listing:test1},name={test}]
Line 1
Line 2
\end{lstlisting}

\begin{lstlisting}[caption={Test 2},label={listing:test2},name={test}]
Line 3
Line 4
\end{lstlisting}

Referencing normal listings: \autoref{listing:test1}, \autoref{listing:test2}.

\begin{codesnippet}[caption={First snippet},label={snippet:first},name={hello}]
#include <stdio.h>
\end{codesnippet}

\begin{algorithm}[caption={First algorithm},label={alg:first},name={myalg}]
A
B
\end{algorithm}

\begin{codesnippet}[caption={Second snippet},label={snippet:second},name={hello}]
int main(void)
{
  printf("Hello world!\n");
  return 0;
}
\end{codesnippet}

Referencing snippets: \autoref{snippet:first}, \autoref{snippet:second}.

\begin{algorithm}[caption={Second algorithm},label={alg:second},name={myalg}]
C
\end{algorithm}

Referencing algorithms: \autoref{alg:first}, \autoref{alg:second}.

\chapter{Input}

\lstinputcodesnippet[caption={this is my caption},label={snippet:input}]{samplecode.c}

Referencing snippet: \autoref{snippet:input}.

\end{document}

我使用了书籍类来演示如何将章节号正确添加为参考编号的前缀。结果将如下所示:

房源列表 代码片段列表 算法列表 章节 输入

这是我第一次在这里发帖,我非常乐意收到一些反馈或建议!另外,我很抱歉重新打开一个几年前的帖子,但我认为这可能会有所帮助。

编辑

事实证明,为了能够使用 为每个列表环境的标题设置不同的样式\captionsetup,还需要修补软件包对 所做的更改\lst@MakeCaptioncaption并且根据hyperref之前是否加载caption,在这种情况下需要修补的命令可能是\lst@MakeCaption\NROrg@lst@MakeCaption命令。

我已更新上述代码,并做了必要的更改以使其正常\captionsetup工作。例如,可以将以下内容添加到序言的末尾

\captionsetup[codesnippet]{box=colorbox,boxcolor={yellow}}
\captionsetup[lstlisting]{box=colorbox,boxcolor={cyan},skip=-1pt}

使列表标题看起来像这样: 样式化字幕

相关内容