LaTeX forum ⇒ Graphics, Figures & Tables ⇒ Caption package displays no number in caption

Information and discussion about graphics, figures & tables in LaTeX documents.
redted
Posts: 5
Joined: Sat Jul 17, 2010 8:32 pm

Caption package displays no number in caption

Hi,
I use the caption package to add a captionof{lstlisting} to a tikz image. I do this to highlight code/place listings side by side (packages used are: tikz/listings/caption). The normal listingcaptions have a title of the form "CODE No: My Caption text", the captions created with the captionof command however lack the No bit. I would like to know how to add the number back into the caption. I noticed however that the captionof command increases the counter, in the minimal example the listing, comming after the picture captioned as listing, is labeld with the no 2.

Geetings ted

(almost) minimal example
\documentclass{scrreprt}%\input{conf/xcolor_ppt}\usepackage{listings}%\input{conf/listings}\usepackage[latin1]{inputenc}\usepackage{tikz}\usepackage{listings}\usepackage{caption}\begin{document} \begin{figure}[H]\centering\begin{tikzpicture} \end{tikzpicture}\captionof{lstlisting}[short]{long}\end{figure} \begin{lstlisting}[caption=demo]asd\end{lstlisting}  \end{document}

P.S.: I looked into viewtopic.php?f=45&t=6652 briefly, however this does not solve my highlight problem, and it does not give me one caption for both listings as one.
Last edited by redted on Sun Jul 18, 2010 11:22 am, edited 1 time in total.

sommerfee
Posts: 500
Joined: Mon Apr 09, 2007 4:20 pm
There is an internal command inside the listings package which controls if the caption will be numbered or not. When typesetting a caption outside a listings environment, this command will not be set and therefore \captionof{lstlisting}{...} will be unnumbered.

I used to have a workaround for this in old versions of the caption package, but had to remove this in 2007 because of unwanted side-effects. Obviously I came to the conclusion that there is no possibility to re-implement this without side-effects, but since this was three years ago I don't remember.

I will re-examine this problem in the future and will either try to build a new workaround for this or at least document this behaviour. Until then you could give

\captionsetup[lstlisting]{labelformat=simple}

a try. This will re-define the label format of the listing captions, so there will always be a number.

redted
Posts: 5
Joined: Sat Jul 17, 2010 8:32 pm
Thank you very much, it does exactly what i want. Problem solved.

Upon reading your post, I tried simply wrapping the caption in a listing which lead to unsatisfying results, since an additional listings box popped up. So there has to be another way to make that numbering command public / available to the caption package. (just random thoughts of a latex novice)

Thank you again for your help, ted