3

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.

0
4

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:

\documentclass{article}
\usepackage{caption}
\usepackage{xcolor}
\usepackage{listings}
\usepackage[T1]{fontenc}
\usepackage{beramono}

% some colors for the listings
\colorlet{cppkeyword}{teal}
\colorlet{latexkeyword}{green!50!black}
\colorlet{gnuplotkeyword}{magenta!60!black}
\colorlet{gnuplotcomment}{teal}
\colorlet{symb}{red!50!black}
\colorlet{comm}{brown}

% for captions: boldfaced labels, separator=period
\captionsetup[lstlisting]{labelfont=bf,labelsep=period}

% style for c++ listings
\lstdefinestyle{cpp}{%
  language=C++,
  keywordstyle=\bfseries\color{cppkeyword},
  literate=%
    {\{}{\textcolor{symb}{\{}}1
    {\}}{\textcolor{symb}{\}}}1
    {(}{\textcolor{symb}{(}}1
    {)}{\textcolor{symb}{)}}1
    {;}{\textcolor{symb}{;}}1
}

% style for LaTeX listings
\lstdefinestyle{latex}{%
  language=[LaTeX]{TeX},
  texcsstyle=*\bfseries\color{latexkeyword}, 
  literate=%
    {\{}{\textcolor{symb}{\{}}1
    {\}}{\textcolor{symb}{\}}}1
    {(}{\textcolor{symb}{(}}1
    {)}{\textcolor{symb}{)}}1
    {\%}{\textcolor{comm}{\%}}1
}

% style for Gnuplot listings
\lstdefinestyle{gnuplot}{%
  language=Gnuplot,
  keywordstyle=\bfseries\color{gnuplotkeyword},
  commentstyle={\itshape\color{gnuplotcomment}},
  literate=%
    {"}{\textcolor{symb}{"}}1
}

% Common style for all listings
\lstset{
  basicstyle=\small\ttfamily,
  columns=fullflexible,
  tabsize=3
}

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

\lstnewenvironment{cpp}[1][]{%
  \renewcommand\lstlistingname{\texttt{C++} listing}
  \lstset{style=cpp,#1}}{}

% Counters for LaTeX and Gnuplot listings and an auxiliary counter
\newcounter{latexlst}
\newcounter{gnuplotlst}
\newcounter{tmp}

\makeatletter
% Environment for LaTeX listings; extension for auxiliar file: lll
\lstnewenvironment{latex}[1][]{%
  \renewcommand\lstlistingname{\LaTeX\ listing}
  \setcounter{tmp}{\value{lstlisting}}
  \setcounter{lstlisting}{\value{latexlst}}
  \lstset{style=latex,nolol,#1}
}{%
  \refstepcounter{latexlst}
  \addcontentsline{lll}{lstlisting}{\protect\numberline{\thelstlisting}\lst@@caption}%
  \setcounter{lstlisting}{\value{tmp}}
}

% Settings for the List of LaTeX listings
\newcommand\lstlistoflatexlistings{%
  \bgroup
  \renewcommand\lstlistlistingname{\LaTeX\ listings}
  \let\contentsname\lstlistlistingname
  \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lll}}%
  \tableofcontents
  \egroup%
}

% Environment for Gnuplot listings; extension for auxiliar file: lgl
\lstnewenvironment{gnuplot}[1][]{%
  \renewcommand\lstlistingname{\texttt{Gnuplot} listing}
  \setcounter{tmp}{\value{lstlisting}}
  \setcounter{lstlisting}{\value{gnuplotlst}}
  \lstset{style=gnuplot,nolol,#1}
}{%
  \refstepcounter{gnuplotlst}
  \addcontentsline{lgl}{lstlisting}{\protect\numberline{\thelstlisting}\lst@@caption}%
  \setcounter{lstlisting}{\value{tmp}}
}

% Settings for the List of Gnuplot listings
\newcommand\lstlistofgnuplotlistings{%
  \bgroup
  \renewcommand\lstlistlistingname{\texttt{Gnuplot} listings}
  \let\contentsname\lstlistlistingname
  \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lgl}}%
  \tableofcontents
  \egroup%
}
\makeatother

\begin{document}

\lstlistoflistings
\lstlistoflatexlistings
\lstlistofgnuplotlistings

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

int calling_function()
{
        long test1;
        int test2;
}
\end{cpp}

\begin{latex}[caption={test code for \LaTeX},label={lst:latex}]
\newcommand\mycom[1]{%
    \fbox{#1}%
}
\end{latex}

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

\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()}

\end{document}

enter image description here

10
  • 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
    Feb 3 '14 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). Feb 3 '14 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. Feb 3 '14 at 13:19
  • 1
    @LCsa I would simply use the float option for the selected environments. Feb 3 '14 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. Feb 6 '14 at 0:11

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.