4

Is there a way to exclude a specific listing from being printed in the LoL? I put one in a figure environment for the sake of not being printed on two seperate pages. But now I have the figure as well as the listing in the toc

\documentclass{article}

\usepackage{tikz}
\usepackage{listings}


\begin{document}
\lstlistoflistings
\listoffigures
\begin{figure}
\begin{lstlisting}[caption=Listing]

\end{lstlisting}
\caption{Figure}
\end{figure}

\end{document}

Update:

I used this to have a special listing caption for all real listings.

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{%
  \parbox{\textwidth}{\colorbox{gray}{\parbox{\textwidth}{\bf\sffamily\smaller#1#2#3}}\vskip-1pt}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
lockstep
  • 250,273
Matthias
  • 1,729
  • Don't use \caption? And why do you use figure to avoid a page break. Maybe a minipage is better for your situation. – knut Jan 03 '13 at 22:57
  • 1
    Which one do you want to remove: the listing or the figure caption? Would you be able to provide a minimal working example (MWE) of your current setup? – Werner Jan 03 '13 at 23:10
  • I want to remove the listing entry. Just provided a MWE. I decided to put this in a figure also because I used tikz to annote some things in the listing. – Matthias Jan 03 '13 at 23:25
  • Do you really need two captions? If not, just do as knut said, and remove \caption{Figure}. Otherwise: http://tex.stackexchange.com/questions/43995/ignore-figure-for-list-of-figures – Torbjørn T. Jan 03 '13 at 23:27
  • I didn't provide a listing caption, but I need the figure caption. – Matthias Jan 03 '13 at 23:27
  • Yes you did: \begin{lstlisting}[caption=Listing] – Torbjørn T. Jan 03 '13 at 23:32
  • Mh, yes I did this in the MWE because I thought it's not important whether I declare a caption or not. In my real document, I didn't provide a caption. Nevertheless I have a toc entry... ?! – Matthias Jan 03 '13 at 23:35
  • 1
    If you remove the [caption=Listing] option, you don't obtain an entry in the LoL (and that's not a joke)... – Werner Jan 03 '13 at 23:36
  • I know it's not a joke, but I do get an entry. I updated the post. I think it's related to this. I know it's maybe a bit too localized, but I just realized that and tomorrow is the deadline :( Sorry and thanks for your help. – Matthias Jan 03 '13 at 23:39
  • [caption=Listing] goes into the List of Listings (LoL), and \caption{Figure} goes into the List of Figures (LoF). In your MWE you do not even have a \tableofcontents, nevertheless you have an entry in the Table of Contents (“ToC”)? Remove [caption=Listing] to get rid of the LoL entry, remove \caption{Figure} or use \caption[]{Figure} to remove the LoF entry. And by the way, your MWE misses \usepackage{caption} and at least a definition of \smaller (but that’s not the point). – Qrrbrbirlbel Jan 03 '13 at 23:40
  • 1
    If you want a caption for the Listing but not an entry in the LoL, you can try the environment option nolol: \begin{lstlisting}[caption=Listing,nolol]. Or: \begin{lstlisting}[caption={[]Listing}] which hides the Listing number and doesn’t produce a LoL entry. – Qrrbrbirlbel Jan 03 '13 at 23:46
  • Thank you so much :) nolol worked for me. It was strange, because before there was actually no caption printed, but I got a . in the LoL. Thanks again! – Matthias Jan 03 '13 at 23:55

1 Answers1

7

As we already figured out in a lengthy comment discussion:

  • ToC: Table of Contents (\tableofcontents)
  • LoF: List of Figures (\listoffigures)
  • LoL: List of Listings (\listoflistings)

Either way, you are looking for the lstlistings option nolol.

If you additionally want no Listings number to be applied you have to use caption={[]<caption>} (with or without nolol).

Reference

Not related, but noteworthy

Your \parbox exceeds the line width by 6pt. (You get a overfull hbox message!) The reason is that the \parbox has a small paddings to its contents, 3pt on each side (even vertically). This is removed by using \linewidth-2\fboxsep in the definition.

And as you may have noticed, I also changed \textwidth to \linewidth.

References

Code

\documentclass{article}
\usepackage{caption}
\usepackage{tikz}
\usepackage{listings}

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{%
  \parbox{\textwidth}{\colorbox{gray}{\parbox{\dimexpr\linewidth-2\fboxsep\relax}{\bf\sffamily\small #1#2#3}}\vskip-1pt}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

\begin{document}
\tableofcontents \lstlistoflistings \listoffigures
\begin{figure}
\begin{lstlisting}[caption=Listing,nolol]

\end{lstlisting}
\caption{Figure}
\end{figure}
\end{document}
Qrrbrbirlbel
  • 119,821