如何将 lstlisting 添加到算法列表中

如何将 lstlisting 添加到算法列表中

我写了一个文档,其中我使用伪代码来描述一些通用算法(带有\usepackage{algpseudocode}),可以在 toc 之后的开头视为“算法列表”。

我还使用了一些用 Java 编写的代码\usepackage{listings}来描述更多代码。我还想将这些用 Java 编写的算法添加到用伪代码编写的算法列表中。

是否可以?

列表配置:

\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{listings}
\usepackage{courier}
\usepackage{subcaption}
\lstset{
  language=Java,
  basicstyle=\footnotesize\ttfamily, % Standardschrift
  %numbers=left,               % Ort der Zeilennummern
  numberstyle=\tiny,          % Stil der Zeilennummern
  %stepnumber=2,               % Abstand zwischen den Zeilennummern
  numbersep=6pt,              % Abstand der Nummern zum Text
  tabsize=4,                  % Groesse von Tabs
  extendedchars=true,         %
  breaklines=true,            % Zeilen werden Umgebrochen
  keywordstyle=\color{blue},
  commentstyle=\itshape\color{gray},
  stringstyle=\ttfamily, % Farbe der String
  showspaces=false,           % Leerzeichen anzeigen ?
  showtabs=false,             % Tabs anzeigen ?
  xleftmargin=17pt,
  framexleftmargin=17pt,
  framexrightmargin=5pt,
  framexbottommargin=4pt,
  %backgroundcolor=\color{lightgray},
  showstringspaces=false,
  morekeywords={get,set,interface, null, var, in}
}
\DeclareCaptionFont{white}{\color{black}}
\DeclareCaptionFormat{listing}{\colorbox[cmyk]{0.13,0.14,0.10,0.1}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}

答案1

有一种可能性;etoolbox用于修补内部\lst@MakeCaption(来自listings),因此标题写在.loa文件中(用于\listofalgorithms):

\documentclass{report}
\usepackage{listings}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{etoolbox}

\makeatletter
\patchcmd{\lst@MakeCaption}{\addcontentsline{lol}}{\addcontentsline{loa}}{}{}
\patchcmd{\lst@MakeCaption}{\addcontentsline{lol}}{\addcontentsline{loa}}{}{}
\AtBeginDocument{%
  \let\c@lstlisting\c@algorithm
  \let\thelstlisting\thealgorithm
  \renewcommand\listalgorithmname{List of algorithms}
}
\makeatother

\begin{document}

\listofalgorithms

\begin{algorithm}
This is an algorithm
\caption{An algorithm}
\end{algorithm}

\begin{lstlisting}[caption=A listing]
This is a listing
\end{lstlisting}

\begin{algorithm}
This is another algorithm
\caption{Another algorithm}
\end{algorithm}

\begin{lstlisting}[caption=Another listing]
This is another listing
\end{lstlisting}

\end{document}

在此处输入图片描述

现在,原始问题已被编辑,我之前示例中的代码必须放在加载中subcaption

\documentclass{report}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{listings}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{etoolbox}
\usepackage{courier}

% Before loading subcaption %%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\patchcmd{\lst@MakeCaption}{\addcontentsline{lol}}{\addcontentsline{loa}}{}{}
\patchcmd{\lst@MakeCaption}{\addcontentsline{lol}}{\addcontentsline{loa}}{}{}
\AtBeginDocument{%
  \let\c@lstlisting\c@algorithm
  \let\thelstlisting\thealgorithm
  \renewcommand\listalgorithmname{List of algorithms}
}
\makeatother
% Before loading subcaption %%%%%%%%%%%%%%%%%%%%%%%

\usepackage{subcaption}
\lstset{
  language=Java,
  basicstyle=\footnotesize\ttfamily, % Standardschrift
  %numbers=left,               % Ort der Zeilennummern
  numberstyle=\tiny,          % Stil der Zeilennummern
  %stepnumber=2,               % Abstand zwischen den Zeilennummern
  numbersep=6pt,              % Abstand der Nummern zum Text
  tabsize=4,                  % Groesse von Tabs
  extendedchars=true,         %
  breaklines=true,            % Zeilen werden Umgebrochen
  keywordstyle=\color{blue},
  commentstyle=\itshape\color{gray},
  stringstyle=\ttfamily, % Farbe der String
  showspaces=false,           % Leerzeichen anzeigen ?
  showtabs=false,             % Tabs anzeigen ?
  xleftmargin=17pt,
  framexleftmargin=17pt,
  framexrightmargin=5pt,
  framexbottommargin=4pt,
  %backgroundcolor=\color{lightgray},
  showstringspaces=false,
  morekeywords={get,set,interface, null, var, in}
}
\DeclareCaptionFont{white}{\color{black}}
\DeclareCaptionFormat{listing}{\colorbox[cmyk]{0.13,0.14,0.10,0.1}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}


\begin{document}

\listofalgorithms

\begin{algorithm}
This is an algorithm
\caption{An algorithm}
\end{algorithm}

\begin{lstlisting}[caption=A listing]
This is a listing
\end{lstlisting}

\begin{algorithm}
This is another algorithm
\caption{Another algorithm}
\end{algorithm}

\begin{lstlisting}[caption=Another listing]
This is another listing
\end{lstlisting}

\end{document}

在此处输入图片描述

相关内容