16

I have a listing like so:

\lstset{language=Python,caption={Caption}}
\begin{lstlisting}
some Python code goes here
\end{lstlisting}

In the PDF this appears as Listing 1. Caption. How can I make it appear as Figure 1. Caption? It is worth mentioning that mixing real figures and listings has to keep consistent figure numbering.

lockstep
  • 250,273
Adam
  • 263
  • You should check next link that worked for me: http://stackoverflow.com/questions/2814714/how-to-change-title-in-listings – user828814 Mar 27 '12 at 18:46

1 Answers1

19

I think it's appropriate to use a little trick here: don't set the caption in the lstlisting environment, but enclose the listing in figure and use \caption:

\documentclass{article}

\usepackage{listings}

\begin{document}

\listoffigures

\begin{figure}
  \caption{Hello World}
  \begin{lstlisting}[gobble=2]
  print "Hello World!"
  \end{lstlisting}
\end{figure}

\end{document}

This way you can mix real figures and listings while keeping consistent figure numbering.

Andrey Vihrov
  • 22,325
  • Excellent, worked perfectly. I tried putting it in figures before but it didn't do any good because I didn't move the captions. – Adam Jun 02 '11 at 07:26
  • This worked for me but the figures only show when the \listoffigures command is added, is there a way not to add the list? – darc Aug 22 '21 at 19:16
  • Can the Figure label below? – alper Nov 07 '22 at 08:50