LaTeX forum ⇒ Graphics, Figures & TablesCaption 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

Postby redted » Sat Jul 17, 2010 8:40 pm

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
  1. \documentclass{scrreprt}
  2. %\input{conf/xcolor_ppt}
  3. \usepackage{listings}
  4. %\input{conf/listings}
  5. \usepackage[latin1]{inputenc}
  6. \usepackage{tikz}
  7. \usepackage{listings}
  8. \usepackage{caption}
  9. \begin{document}
  10.  
  11. \begin{figure}[H]
  12. \centering
  13. \begin{tikzpicture}
  14.  
  15. \end{tikzpicture}
  16. \captionof{lstlisting}[short]{long}
  17. \end{figure}
  18.  
  19. \begin{lstlisting}[caption=demo]
  20. asd
  21. \end{lstlisting}
  22.  
  23.  
  24. \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.

User avatar
sommerfee
Posts: 500
Joined: Mon Apr 09, 2007 4:20 pm

Postby sommerfee » Sun Jul 18, 2010 7:38 am

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

  1. \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

Postby redted » Sun Jul 18, 2010 11:21 am

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


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 5 guests