To center my listings on the page I followed a tutorial on tex.se.com and that resulted in the following code:
{\renewcommand{\figurename}{Listing}
\begin{figure}[H] % the figure provides the caption
\centering % which should be centered
\caption{Example of factorial using the SAL actor language.}
\begin{tabular}{c}
\begin{lstlisting}[basicstyle= \footnotesize\ttfamily,
belowcaptionskip=0em,
language=SAL,
label={lst:salfactorial}]
def Factorial() [val, cust]
become Factorial() ||
if val = 0
then send [1] to cust
else let cont = new FactorialCont(val, cust)
in send [val - 1, cont] to self
end def
def FactorialCont(val, cust) [arg]
send [val * arg] to cust
end def
// Top level expression
let x = [recep] new Factorial()
in send [5] to x
\end{lstlisting}
\end{tabular}
\end{figure}}
The problem with this approach is that I do have a listing now (when I use \autoref it shows Listing) but it uses the counter for Figures.
As a result, the above shown figure/listing is numbered Listing 2.1, but an actual listing above is also printed as Listing 2.1.
I don't want to use the same counter for all Figures and Listings, just make this figure use the Listing counter. So if I would have actual figures they would be numbered 1.1, 1.2 and 1.3. If I had two actual Listings and one Listing inside a Figure (as shown above) they would be numbered Listing 1.1, 1.2 and 1.3.
Minimal Example
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\title{Test}
\author{Christophe De Troyer}
\date{May 2015}
\begin{document}
\section{Foo}
{\renewcommand{\figurename}{Listing}
\begin{figure}[H] % the figure provides the caption
\centering % which should be centered
\caption{Example of factorial using the SAL actor language.}
\begin{tabular}{c}
\begin{lstlisting}[basicstyle= \footnotesize\ttfamily,
belowcaptionskip=0em,
language=SAL,
label={lst:salfactorial}]
first listing
\end{lstlisting}
\end{tabular}
\end{figure}}
\begin{lstlisting}[basicstyle= \footnotesize\ttfamily,
belowcaptionskip=0em,
language=SAL,
caption={second listing},
label={lst:second}]
second listing
\end{lstlisting}
\end{document}
floatandcaptionoptions to thelstlistingenvironment directly. :) You may also be interested in\lstsetas as to not repeat yourself (withbasicstyle,belowcaptionskip, etc.). If you want it to stay put but have a caption/reference, I think you can just say give it acaption. By the way, you should link to this tutorial you found. It would appear that it needs to be corrected. – Sean Allred May 19 '15 at 12:13