# Multiple listings styles

In using the listings package, how can I have a distinct \lstlistingname value for each type of listing.

Lets say my document has C++, R and Pseudo listings, and I would like the \lstlistingname value to be: C++ Code, R Code and Pseudo Code respectively, is it possible to define these values as part of the \lstdefinestyle{...} definition, rather than calling \renewcommand{\lstlistingname}{...} each time it needs changing?

Additionally, is it possible to have separate list of listings table in the front-matter of my document, for each type of listing style in preference to having all of the styles mixed into the same table?

-

I would define separate lstlisting environments for each of these entries using \lstnewenvironment. There you can reset the caption title and set the style the way you want to. Additionally, the code related to \lstlistoflistings from listings.dtx has been update to produce individual "List of ":

\documentclass{article}
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch
\usepackage{listings}% http://ctan.org/pkg/listings
\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}


Each listing patches the \lst@MakeCaption command to insert the appropriate file extension to write the contents-related macros. From the traditional .lol to .loc for C++ code, .lor for R code and .lop for Pseudocode.

It would be possible to update this to use separate counters for each listing as well. However, this would require a little more work.

Perhaps, as a reference for typesetting C++, see Prettiest way to typeset “C++” (cplusplus)?

-
Perfect. Thanks. –  Nicholas Hamilton May 11 '13 at 12:08
But how to use \lstinputlisting with the new environments? –  Nicholas Hamilton May 11 '13 at 12:40