[Tex/LaTex] “Listing #” below listings without a caption

captionslistings

When not specifying a caption for a lstlisting nothing is shown below the listing while "Listing #: mycaption" is displayed as soon as I specify a caption.

However, I'd like "Listing #" to be displayed for all listings; even those without a caption (those with a caption should keep their "Listing #: mycaption" of course).
Is this possible? If yes, how?

Best Answer

You can use caption=\relax; however, then you'll get "Listing #:" with the colon. I don't know how to remove this.

EDIT: OK, here's a nasty hack that removes the colon if the caption is \relax. It works with my version of listings, but it might not work in future versions.

\documentclass{article}
\usepackage{listings}
\begin{document}
\makeatletter
\def\test@relax{\relax}
\let\save@fnum@lstlisting\fnum@lstlisting
\def\fnum@lstlisting{%
    \save@fnum@lstlisting
    \ifx\lst@caption\test@relax\expandafter\@gobble\fi
    }
\makeatother
\begin{lstlisting}[caption=\relax]
a:=b
\end{lstlisting}
\end{document}