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'' .

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Feb 16 '14 at 11:58
  • Remember that you can accept one of the answers below, since both seem to solve your problem. – karlkoeller Feb 19 '14 at 17:04
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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.