I noticed lstlisting
environment seems to supress the caption name and number when no caption is specified.
See the code below:
\documentclass{article}
\usepackage{listings}
\lstset{language=C}
\begin{document}
The listing below will have no caption.
\begin{lstlisting}
int main(int argc, char *argv[])
{
return 0;
}
\end{lstlisting}
Now this code will show the caption.
\begin{lstlisting}
int main(int argc, char *argv[])
{
return 0;
}
\end{lstlisting}
\end{document}
The only solution I found was to set the caption to be an empty space and to get rid of the separator, but this solution does not look very nice:
\documentclass{article}
\usepackage{listings}
\lstset{language=C}
\usepackage{caption}
\captionsetup{labelsep=none}
\begin{document}
This is not an elegant solution, but works:
\begin{lstlisting}
int main(int argc, char *argv[])
{
return 0;
}
\end{lstlisting}
\end{document}
Any ideas?
Best Answer
Define a default caption (I used
\relax
) and setup a caption format that checks whether the given caption is different. The trick is knowing that the caption text is stored in\lst@caption
.