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.

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.

share|improve this question

3 Answers 3

up vote 8 down vote accepted

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

\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}
share|improve this answer
    
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:

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.

share|improve this answer

Your Answer

 
discard

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.