[Tex/LaTex] Add listings to list of figures

listingstable of contents

I am using the listings package to display code in my document. is it possible to add the listings to the List of Figures. Instead of Listing, there should be Figure in the caption and it should share the same counter as the figures.

Best Answer

The easiest way to obtain this is to insert your listing directly into a figure environment:

enter image description here

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\lstset{% general command to set parameter(s)
  basicstyle=\small, % print whole listing small
  keywordstyle=\color{black}\bfseries\underbar, % underlined bold black keywords
  identifierstyle=, % nothing happens
  commentstyle=\color{white}, % white comments
  stringstyle=\ttfamily, % typewriter type for strings
  showstringspaces=false} % no special string spaces
\begin{document}
\listoffigures
\begin{figure}
  \rule{150pt}{100pt}%
  \caption{A normal figure}
\end{figure}
\begin{figure}
\begin{lstlisting}
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{lstlisting}
\caption{This is a listing}
\end{figure}
\end{document}

The above listing example was taken from the listings documentation (section 1.3 Figure out the appearance, p 5).

Related Question