[Tex/LaTex] How to make a float that is not labelled ‘Figure’ but ‘Listing’

floatslistingsnaming

I would to include my listings as floats, pretty much as Figures, but I don't want to call them Figures. I want to call them listings. How can I do that?

Best Answer

There are two packages that let you format your listings and are able to let them float.

Here are two simple examples.

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{listings}

\lstset{
  basicstyle=\small\ttfamily,
  frame=single
}
\begin{document}
  \begin{lstlisting}[
    gobble=4,
    float,
    caption={Dummy Listing},
    label={lst:dummy}
  ]
    \documentclass{scrartcl}

    \begin{document}
      Hello World!
    \end{document}
  \end{lstlisting}
\end{document}

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{minted}

\begin{document}
  \begin{listing}[!ht]
    \begin{minted}[
      frame=single,
      gobble=6
    ]{latex}
      \documentclass{scrartcl}

      \begin{document}
        Hello World!
      \end{document}
    \end{minted}
    \caption{Dummy listing}
    \label{mnt:dummy}
  \end{listing}
\end{document}

The respective manuals have more details about customization of your listings.

Related Question