5

How can I make a reverse numbered caption with listings? So the package makes ''Listing 1.1'' style captions but I need it in form ''1.1 Listing'' .

czsan
  • 65

2 Answers2

6

Use the »caption« package to declare a new label format. Of course you can do other additional customizations in this way.

\documentclass[11pt]{report}
\usepackage[T1]{fontenc}

\usepackage{caption}
\DeclareCaptionLabelFormat{reverse}{#2 #1}
\captionsetup[lstlisting]{labelformat=reverse}

\usepackage{listings}

\begin{document}
  \chapter{Foo}

  \section{Bar}
    \begin{lstlisting}[
      gobble=6,
      frame=single,
      caption={Useless code},
      label=useless
    ]
      for i:=maxint to 0 do
      begin
        { do nothing }
      end;
    \end{lstlisting}
\end{document}

enter image description here

4

Without any additional package, add the following lines in your preamble:

\makeatletter
\renewcommand\fnum@lstlisting{%
  \ifx\lst@@caption\@empty\else\thelstlisting~\fi%
  \lstlistingname}%
\makeatother

MWE:

\documentclass{report}
\usepackage{listings}

\makeatletter
\renewcommand\fnum@lstlisting{%
  \ifx\lst@@caption\@empty\else\thelstlisting~\fi%
  \lstlistingname}%
\makeatother

\begin{document}
\chapter{Test}
\begin{lstlisting}[frame=single,caption={foo}]
Welcome to TeX.SX
\end{lstlisting}
\end{document} 

Output:

enter image description here

karlkoeller
  • 124,410