Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

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

share|improve this answer
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
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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