0

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}
  • 2
    …*or* you could read in §4.3 of the manual that you can provide float and caption options to the lstlisting environment directly. :) You may also be interested in \lstset as as to not repeat yourself (with basicstyle, belowcaptionskip, etc.). If you want it to stay put but have a caption/reference, I think you can just say give it a caption. 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
  • The caption would not be centered if I put it inside the listing, hence the enclosing figure defined the caption. I will read through the manual! Thanks for the pointer. – Christophe De Troyer May 19 '15 at 12:29
  • Oh goodness, alright. Well, I'll be investigating a better way to center the caption :) Can you provide me a reference image for desired output? (Reference numbering notwithstanding.) – Sean Allred May 19 '15 at 13:02
  • Take a look at egreg's new answer to the linked question; it might solve your problem. – Sean Allred May 19 '15 at 14:14
  • Any news here? Did you have a look at the linked answer? – TeXnician Aug 29 '18 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.