# Invisible listing captions

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.

-

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}
-
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 May 11 '12 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) –  Aaron J Lang May 12 '12 at 12:36

It sounds like you need the command

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

\documentclass{article}
\usepackage{listings}

\begin{document}
\lstlistoflistings

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

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

\end{document}
-
Should not the second argument be lstlisting instead of subsection? –  Boris May 11 '12 at 20:28
@Boris thanks, made the edit :) –  cmhughes May 11 '12 at 20:39

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

\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.

-