Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Feb 22 '12 at 15:54

This question came from our site for professional and enthusiast programmers.

    
Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. –  Werner Feb 22 '12 at 16:39

2 Answers 2

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.

share|improve this answer
1  
Would be good to know the reason for the vote down. Without it's hard to improve. –  Thorsten Donig Mar 16 '12 at 8:26

It would be possible to re-purpose the figure environment and associated macros, but it may make the code harder to read. For example,

\begin{figure}[..]
  % <contents>
  \caption[..]{...}
\end{figure}

would then serve the (possibly) dual purpose of posting a listing or a figure. Additionally, what should be contained within \listoffigures?

The float package allows for the easy definition of new floats. Here's a minimal example declaring a new float listing, as well as the appropriate \listoflistings command to display a "List of Listings":

enter image description here

\documentclass{article}
\usepackage{float}% http://ctan.org/pkg/float
% Create new "listing" float
\newfloat{listing}{tbhp}{lst}%[section]
\floatname{listing}{Listing}
\newcommand{\listoflistings}{\listof{listing}{List of Listings}}
\begin{document}
\tableofcontents
\listoffigures
\listoflistings
\begin{figure}
  \centering\rule{150pt}{100pt}
  \caption{This is a figure}
\end{figure}
\begin{listing}
  \begin{verbatim}
Here is some code.
  \end{verbatim}
  \caption{This is a listing}
\end{listing}
\end{document}
​

For more information on the user interface, consult the float package documentation.

share|improve this answer
1  
New floats can also be created by the caption package. The classes of the KOMA-Script bundle offer this feature, too. –  Thorsten Donig Feb 22 '12 at 17:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.