[Tex/LaTex] How to add lstlisting to Algorithms List

listingspseudocode

I have written a document where I use pseudo code to describe some general algorithms (with \usepackage{algpseudocode} ) which can be viewed at the begining after toc as „List of Algorithms”.

I also use some java code written using \usepackage{listings} to describe more code. I would like to also add these algorithms written in java to the List Of Algorithms among those written in pseudo code.

Is it possible?

Listings configuration:

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

Best Answer

Here's one possibility; etoolbox was used to patch the internal \lst@MakeCaption (from listings) so the caption is written in the .loa file (used for \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}

enter image description here

Now that the original question has been edited, the code from my previous example has to be placed before loading 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}

enter image description here

Related Question