I am planning a "small thesis" for university which will need to display lots of code (C, C++, gnuplot, LaTeX, ...) - thus I spent this evening reading about listings and fancyvrb. The latter I've already been using together with the newfloat- and caption-package. The former seems very appealing and handy (linebreaking, indexing, caption without floating body...) during my first experiments.

All the four packages grant compability with each other, yet I am confused as for instance both, caption and listings, provide caption-functionality, or both, newfloat and listings, could be used to create a "Table of Listings", etc...

I'm afraid I have no specific question... I'm rather asking for hints or advice on how to use these four packages together. For example: Should I assign the float/caption-management to newfloat and caption only? Which of the code-packages should "do the main part of the job"? Is it possible to define independent code-environments with their individual "lists of listings"? Is there a convenient way to generate different styles of inline listings? Etc.

I would be glad if you could start me off and steer me in a promising direction so that I can - if necessesary - ask more precise questions in the course of setting everything up.


1 Answer 1


From your question, it seems that you need the ability to write code in several languages, provide captions for the listings and to create a "List of listings". In this case, the listings package offers you everything you need.

  1. \lstlistoflistings allow you to create the list of all captioned listings. With not much effort, you can even have independent list for each type of language; see the example code below in which environments were defined for LaTeX, C++ and Gnuplot listings and each one will have its own counter and List of listings.

  2. It offers a number of predefined languages (among them, the ones you mentioned C, C++, Gnuplot, LaTeX) and you can further customize them.

  3. It provides its own caption mechanism, but if you use the caption package to customize your captions, you can easily override the listings settings, if required.

  4. By default, long listings don't float and will admit page breaks. Using the float key, your listing will be treated like a floating object (which could be handy under some circumstances); notice, however, that floating objects don't admit page breaks.

The caption package will allow you to customize the look and feel of your captions. The package does cooperate with listings.

You would only use newfloat in case you required to use a new type of floating object, which doesn't seem necessary for your code snippets; as I mentioned, before, you can easily turn a lstlisting environment into a floating object (if required) without any new package

In short, I'd say (judging by your question), that listings is the way to go and you can eventually use caption if you want to change the feel and look of your captions.

A simple example showing how quickly you can set customized environments for your listings using the listings package; I defined two environments, one for C++ code and the other one for LaTeX.

I used the caption package to change the default font used for the label of the captions and the separator:


% some colors for the listings

% for captions: boldfaced labels, separator=period

% style for c++ listings

% style for LaTeX listings

% style for Gnuplot listings

% Common style for all listings

% We tweak the default settings for C++ listings
\renewcommand\lstlistlistingname{\texttt{C++} listings}

  \renewcommand\lstlistingname{\texttt{C++} listing}

% Counters for LaTeX and Gnuplot listings and an auxiliary counter

% Environment for LaTeX listings; extension for auxiliar file: lll
  \renewcommand\lstlistingname{\LaTeX\ listing}

% Settings for the List of LaTeX listings
  \renewcommand\lstlistlistingname{\LaTeX\ listings}
  \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lll}}%

% Environment for Gnuplot listings; extension for auxiliar file: lgl
  \renewcommand\lstlistingname{\texttt{Gnuplot} listing}

% Settings for the List of Gnuplot listings
  \renewcommand\lstlistlistingname{\texttt{Gnuplot} listings}
  \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lgl}}%



\begin{cpp}[caption={test code for \texttt{C++}},label={lst:c++}]
long some_function();
int other_function(); 

int calling_function()
        long test1;
        int test2;

\begin{latex}[caption={test code for \LaTeX},label={lst:latex}]

\begin{gnuplot}[caption={test code for \texttt{Gnuplot}},label={lst:gnuplot}]
# 1st step
set term "table"
set output "temp.dat"
splot ....

\LaTeX code: \lstinline[style=latex]{\documentclass}\par
\texttt{Gnuplot} code: \lstinline[style=gnuplot]{set output "temp.dat"}\par
\texttt{C++} code: \lstinline[style=cpp]{int calling_function()}


enter image description here

  • Thanks for the ample response. The features you mentioned are the ones I already stumbled over, but listed and demonstrated very neatly! By now listings indeed seems to cover everything I will need. Do the lstdefinestyle- and lstnewenvironment-commands etc. provide the means to, for instance, create two independent "lists of listings" (which would be possible using the newfloat-package, I guess)? If you could maybe imply this in your answer, please? I then could edit into the introducing question, as well.
    – LCsa
    Commented Feb 3, 2014 at 11:55
  • 1
    @LCsa it's not convenient to use newfloat since the new environments would be floating objects, not admitting page breaks. In any case, it's not necessary to use it; you can define your independent lists using some appropriate definitions (I've added some code to my answer showing three independent lists without any additional packages). Commented Feb 3, 2014 at 13:17
  • 1
    @LCsa yes, lstMakeShortInline will allow you to define several in-line styles; you can also use \lstinline[style=<style>]{<code>} as I did in my updated answer. Commented Feb 3, 2014 at 13:19
  • 1
    @LCsa I would simply use the float option for the selected environments. Commented Feb 3, 2014 at 14:24
  • 1
    @LCsa yes; to store the current value of the lstlisting counter, so as to restore the value once the other environments have redefined and used it. Commented Feb 6, 2014 at 0:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .