[Tex/LaTex] How to change “Listing 1. Caption” to “Figure 1. Caption”

captionsfloatslistingsnumbering

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.

Best Answer

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.