2

This question is mainly related to Multiple listings styles. If I have \usepackage{caption} definition in the head of file, list of custom listings disappear. How to make it work?

\documentclass{article}
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{caption}
\makeatletter
% --------------------------------------- C++
\newcommand{\lstlistcplusplusname}{List of C++}
\lst@UserCommand\lstlistofcplusplus{\bgroup
    \let\contentsname\lstlistcplusplusname
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{loc}}%
    \tableofcontents \egroup}
\lstnewenvironment{cplusplus}[1][]{%
  \renewcommand{\lstlistingname}{C++ Code}%
  \xpatchcmd*{\lst@MakeCaption}{lol}{loc}{}{}%
  \lstset{language=C++,#1}}
  {}
% --------------------------------------- R
\newcommand{\lstlistrcodename}{List of R}
\lst@UserCommand\lstlistofrcode{\bgroup
    \let\contentsname\lstlistrcodename
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lor}}%
    \tableofcontents \egroup}
\lstnewenvironment{rcode}[1][]{%
  \renewcommand{\lstlistingname}{R Code}%
  \xpatchcmd*{\lst@MakeCaption}{lol}{lor}{}{}%
  \lstset{language=R,#1}}
  {}
% --------------------------------------- Pseudocode
\newcommand{\lstlistpseudocodename}{List of Pseudocode}
\lst@UserCommand\lstlistofpseudocode{\bgroup
    \let\contentsname\lstlistpseudocodename
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lop}}%
    \tableofcontents \egroup}
\lstnewenvironment{pseudocode}[1][]{%
  \renewcommand{\lstlistingname}{Pseudocode}%
  \xpatchcmd*{\lst@MakeCaption}{lol}{lop}{}{}%
  \lstset{basicstyle=\ttfamily,#1}}
  {}
\makeatother
\begin{document}

\lstlistofcplusplus
\lstlistofrcode
\lstlistofpseudocode

\begin{cplusplus}[caption={Hello world}]
// 'Hello World!' program 

#include <iostream>

int main()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}
\end{cplusplus}

\begin{rcode}[caption={Hello world}]
cat('Hello, world!\n')
\end{rcode}

\begin{pseudocode}[caption={Hello world}]
print "Hello world"
\end{pseudocode}

\end{document}
Sezen
  • 165
  • 6

2 Answers2

2

The caption package needs to patch \lst@MakeCaption to adapt the listings package to the features of the caption package. It stores (and uses) the original definition of \lst@MakeCaption into a macro called \caption@ORI@lst@MakeCaption, so to make your solution work, this macro needs to be patched:

\documentclass{article}
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{caption}
\makeatletter
% --------------------------------------- C++
\newcommand{\lstlistcplusplusname}{List of C++}
\lst@UserCommand\lstlistofcplusplus{\bgroup
    \let\contentsname\lstlistcplusplusname
    \let\lst@temp@starttoc \def@starttoc##1{\lst@temp{loc}}%
    \tableofcontents \egroup}
\lstnewenvironment{cplusplus}[1][]{%
  \renewcommand{\lstlistingname}{C++ Code}%
  \xpatchcmd*{\lst@MakeCaption}{lol}{loc}{}{}%
  \xpatchcmd*{\caption@ORI@lst@MakeCaption}{lol}{loc}{}{}%
  \lstset{language=C++,#1}}
  {}
% --------------------------------------- R
\newcommand{\lstlistrcodename}{List of R}
\lst@UserCommand\lstlistofrcode{\bgroup
    \let\contentsname\lstlistrcodename
    \let\lst@temp@starttoc \def@starttoc##1{\lst@temp{lor}}%
    \tableofcontents \egroup}
\lstnewenvironment{rcode}[1][]{%
  \renewcommand{\lstlistingname}{R Code}%
  \xpatchcmd*{\lst@MakeCaption}{lol}{lor}{}{}%
  \xpatchcmd*{\caption@ORI@lst@MakeCaption}{lol}{lor}{}{}%
  \lstset{language=R,#1}}
  {}
% --------------------------------------- Pseudocode
\newcommand{\lstlistpseudocodename}{List of Pseudocode}
\lst@UserCommand\lstlistofpseudocode{\bgroup
    \let\contentsname\lstlistpseudocodename
    \let\lst@temp@starttoc \def@starttoc##1{\lst@temp{lop}}%
    \tableofcontents \egroup}
\lstnewenvironment{pseudocode}[1][]{%
  \renewcommand{\lstlistingname}{Pseudocode}%
  \xpatchcmd*{\lst@MakeCaption}{lol}{lop}{}{}%
  \xpatchcmd*{\caption@ORI@lst@MakeCaption}{lol}{lop}{}{}%
  \lstset{basicstyle=\ttfamily,#1}}
  {}
\makeatother
\begin{document}

\lstlistofcplusplus \lstlistofrcode \lstlistofpseudocode

\begin{cplusplus}[caption={Hello world}] // 'Hello World!' program

include

int main() { std::cout << "Hello World!" << std::endl; return 0; } \end{cplusplus}

\begin{rcode}[caption={Hello world}] cat('Hello, world!\n') \end{rcode}

\begin{pseudocode}[caption={Hello world}] print "Hello world" \end{pseudocode}

\end{document}

It's a pity that the listings feature does not define and use \def\ext@lstlisting{lol}, like all other environments which do offer a "List of" feature. In this case redefining \ext@lstlisting would be sufficient, with and without caption package (or any other package patching \lst@MakeCaption).

  • Thanks for the answer Axel. Can you elaborate why this does not work for beamer document class? Do you have any idea? – Sezen Apr 09 '19 at 16:16
  • @Sezen What problems do you have when using beamer? When I replace article with beamer the document will still compile fine. The lists are missing, the reason for this is that beamer simply does not support lists. –  Apr 10 '19 at 06:38
  • The issue was simply list of listings does not appear in beamer. Your answer is what I was after. Thank you very much. – Sezen Apr 10 '19 at 10:21
0

As an alternative to the caption package, I found the capt-of package in CTAN. When I used this package everything worked smoothly.

\documentclass{article}
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{capt-of}
\usepackage{graphicx}
\makeatletter
% --------------------------------------- C++
\newcommand{\lstlistcplusplusname}{List of C++}
\lst@UserCommand\lstlistofcplusplus{\bgroup
    \let\contentsname\lstlistcplusplusname
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{loc}}%
    \tableofcontents \egroup}
\lstnewenvironment{cplusplus}[1][]{%
  \renewcommand{\lstlistingname}{C++ Code}%
  \xpatchcmd*{\lst@MakeCaption}{lol}{loc}{}{}%
  \lstset{language=C++,#1}}
  {}
% --------------------------------------- R
\newcommand{\lstlistrcodename}{List of R}
\lst@UserCommand\lstlistofrcode{\bgroup
    \let\contentsname\lstlistrcodename
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lor}}%
    \tableofcontents \egroup}
\lstnewenvironment{rcode}[1][]{%
  \renewcommand{\lstlistingname}{R Code}%
  \xpatchcmd*{\lst@MakeCaption}{lol}{lor}{}{}%
  \lstset{language=R,#1}}
  {}
% --------------------------------------- Pseudocode
\newcommand{\lstlistpseudocodename}{List of Pseudocode}
\lst@UserCommand\lstlistofpseudocode{\bgroup
    \let\contentsname\lstlistpseudocodename
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lop}}%
    \tableofcontents \egroup}
\lstnewenvironment{pseudocode}[1][]{%
  \renewcommand{\lstlistingname}{Pseudocode}%
  \xpatchcmd*{\lst@MakeCaption}{lol}{lop}{}{}%
  \lstset{basicstyle=\ttfamily,#1}}
  {}
\makeatother
\begin{document}

\lstlistofcplusplus
\lstlistofrcode
\lstlistofpseudocode

\begin{cplusplus}[caption={Hello world}]
// 'Hello World!' program 

#include <iostream>

int main()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}
\end{cplusplus}

\begin{rcode}[caption={Hello world}]
cat('Hello, world!\n')
\end{rcode}

\begin{pseudocode}[caption={Hello world}]
print "Hello world"
\end{pseudocode}

\begin{figure}[h]
\centering
\includegraphics[scale=0.5]{example-image-a}
\captionof{figure}{Example image a.}
\end{figure}

\end{document}

enter image description here

  • Thanks for the answer but caption is widely used package and has a lot of methods like captionsetup to change captions. I really would like to have a solution with caption package. – Sezen Apr 08 '19 at 14:45
  • It's not clear to me why do you think this is a clash between regexpatch and caption. –  Apr 08 '19 at 19:22
  • As I said, it was just the assumption. –  Apr 08 '19 at 19:39