# Source code description in LaTeX

I have a problem concerning the creation of the name below a "Source code" (Listing) in LaTeX. When I have a figure I use:

       \begin{figure}[t]
\centering
\includegraphics{Figures/drawing.pdf}
\caption{Spoken Dialogue System}

\label{fig:SLDS2}
\end{figure}


I see

Figure 4.5.: Spoken Dialog System


on the screen. However, when I could not figure out how to print

Listing.4.5 Java Code


below this framed code on the screen. What should I add to this code? The source code is as shown in the following:

     \begin{framed}
\begin{lstlisting}[frame=single, backgroundcolor=\color{light-gray}, basicstyle=\footnotesize\ttfamily, language=Java, numbers=left, numberstyle=\tiny
\color{black}]

[HERE IS THE JAVA CODE]

\end{lstlisting}

\end{framed}


-
Before you copy-paste the question, though, call over some moderator help to see if you can get this migrated. Or, search over there first and see if your question's been answered. –  Makoto May 25 '13 at 18:55
Welcome to TeX.SX! Your post was migrated here from another Stack Exchange site. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. –  texenthusiast May 26 '13 at 0:45

## migrated from stackoverflow.comMay 25 '13 at 19:41

This question came from our site for professional and enthusiast programmers.

You can use the caption=<caption text> option for lstlisting:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\colorlet{light-gray}{gray!20}

\begin{document}

\begin{lstlisting}[frame=single, backgroundcolor=\color{light-gray}, basicstyle=\footnotesize\ttfamily, language=Java, numbers=left, numberstyle=\tiny\color{black},caption= {A desciption of the listing}]
[HERE IS THE JAVA CODE]
\end{lstlisting}

\end{document}


Since you didn't provide your definition for light-gray I used a temporary one in my example.

Initially I overlooked the requirement about the caption position to be on the bottom; this can be a chieved using the captionpos=b option (although for listings I would suggest to have the caption on top, as in the default settings):

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\colorlet{light-gray}{gray!20}

\begin{document}

\begin{lstlisting}[frame=single, backgroundcolor=\color{light-gray}, basicstyle=\footnotesize\ttfamily, language=Java, numbers=left, numberstyle=\tiny\color{black},caption= {A desciption of the listing},captionpos=b]
[HERE IS THE JAVA CODE]
\end{lstlisting}

\end{document}


If the same settings are going to be used for various listings, it's better to use \lstset instead of having to repeat them for each lstlisting environment:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\colorlet{light-gray}{gray!20}

\lstset{
frame=single,
backgroundcolor=\color{light-gray},
basicstyle=\footnotesize\ttfamily,
language=Java,
numbers=left,
numberstyle=\tiny\color{black},
captionpos=b
}

\begin{document}

\begin{lstlisting}[,caption= {A desciption of the listing}]
[HERE IS THE JAVA CODE]
\end{lstlisting}

\end{document}

-