6

Short version: how can I make a listing's caption show up in the List of Listings but not in the body of the document where the listing is?

I'm writing a university report, and many of my subsections contain only a single listing. Because of this the logical names of the subsection and the listing are the same. I'm currently using the listings package.

I would like there to be sensible descriptive names in both the Tables of Contents (for the subsection) and the List of Listings (for the listing), but I don't want both names in the body of the document, as the duplication looks silly. Preferably in the main document the subsection would be titled but not the listing, although vice versa is still helpful.

3 Answers 3

8

The package listings also provide an optional argument of caption. So simple use:

\begin{lstlisting}[caption={[entry]}]

This way is also described in the documentation at page 32.

The complete example.

\documentclass{article}
\usepackage{listings}

\begin{document}
\lstlistoflistings

\section{This is my section}
\begin{lstlisting}[caption={[foo]}]
 for i:=maxint to 0 do
 begin
 { do nothing }
 end;
\end{lstlisting}


\end{document}
2
  • The definite answer to the problem! (The documentation of the listings package is huge, but it is worth reading it :-) One is frequently surprised about the stuff that is already in the package.)
    – Daniel
    Commented May 11, 2012 at 21:13
  • Perfect! Thank you. For anyone else reading this: if 'lol' is your desired list-of-listings caption and 'id' is your desired in-document caption you can use 'caption={[lol]id}'. 'caption={[lol]}' won't give you any in-document caption at all, and 'caption={[lol]~}' will give you an in-document caption with only the listing number, not a title. (~ is a non-breaking space) Commented May 12, 2012 at 12:36
5

It sounds like you need the command

\addcontentsline{lol}{lstlisting}{<enter description here>}

where lol stands for the extension for the ListOfListings file. Perhaps to be used in something like the following.

enter image description here

\documentclass{article}
\usepackage{listings}

\begin{document}
\lstlistoflistings

\addcontentsline{lol}{lstlisting}{I entered this myself!}
\addcontentsline{lol}{lstlisting}{and this too!}

\section{This is my section}
\begin{lstlisting}
 for i:=maxint to 0 do
 begin
 { do nothing }
 end;
\end{lstlisting}


\end{document}
1
  • Should not the second argument be lstlisting instead of subsection?
    – Boris
    Commented May 11, 2012 at 20:28
2

You can patch listings's caption macro \lst@MakeCaption to never print the caption using etoolbox:

enter image description here

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\pretocmd{\lst@MakeCaption}{\let\lst@caption\@empty}{}{}
\makeatother
\begin{document}
\tableofcontents
\lstlistoflistings

\section{My first listing}
\begin{lstlisting}[caption={My first listing}]
\typeout{Hello world!}
\end{lstlisting}

\end{document}

As is shown, the above patch works well with hyperref.

You must log in to answer this question.

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