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 ":

enter image description here

\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch
\usepackage{listings}% http://ctan.org/pkg/listings
% --------------------------------------- C++
\newcommand{\lstlistcplusplusname}{List of C++}
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{loc}}%
    \tableofcontents \egroup}
  \renewcommand{\lstlistingname}{C++ Code}%
% --------------------------------------- R
\newcommand{\lstlistrcodename}{List of R}
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lor}}%
    \tableofcontents \egroup}
  \renewcommand{\lstlistingname}{R Code}%
% --------------------------------------- Pseudocode
\newcommand{\lstlistpseudocodename}{List of Pseudocode}
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lop}}%
    \tableofcontents \egroup}


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

#include <iostream>

int main()
  std::cout << "Hello World!" << std::endl;
  return 0;

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

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


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)?

  • 3
    But how to use \lstinputlisting with the new environments? – Nicholas Hamilton May 11 '13 at 12:40
  • works great! but how do you manage the numbering of each style independently? thanks – crazyGuy Apr 7 '18 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.