[Tex/LaTex] Float for lstlisting

floatslistings

I'm trying to set float to my lstlisting (It means lstlisting without pagebreak). I found 2 ways, but it not works for me:

  • Float attribute in lstlisting. It is OK only for lstlisting then count of lines is smaller then half page (in example 18 lines is maximum). If count of lines is greater then half page, lstlisting is alone on the page.

    \documentclass[12pt]{article}
    \usepackage{listings}
    \usepackage{color}
    \definecolor{backcolour}{rgb}{0.9,0.9,0.9}
    \lstset{
      backgroundcolor=\color{backcolour},
      numbers=left,
    }
    
    \begin{document}
      \par Paragraph 1 \\ Text \\ Text \\ Text \\ Text \\ Text \\ Text \\ Text \\
      \par Paragraph 2 \\ Text \\ Text \\ Text \\ Text \\ Text \\ Text \\ Text \\
      \par Paragraph 3 \\ Text \\ Text \\ Text \\ Text \\ Text \\ Text \\ Text \\
    
      \begin{lstlisting}[float]
        Code
        Code
        Code
        Code
        Code
        Code
        Code
        Code
        Code
        Code
        Code
        Code
        Code
        Code
        Code
        Code
        Code
        Code
        Code
      \end{lstlisting}
    
      \par Paragraph 4 \\ Text \\ Text \\ Text \\ Text \\ Text \\ Text \\ Text \\
      \par Paragraph 5 \\ Text \\ Text \\ Text \\ Text \\ Text \\ Text \\ Text \\
    \end{document}
    
  • Minipage is wrong, because I can not use 100 % of code width for lstliting. When I use it without minipage, core area started as well as the text area started. (In minipage it is shifted right.)

    \documentclass[12pt]{article}
    \usepackage{listings}
    \usepackage{color}
    \definecolor{backcolour}{rgb}{0.9,0.9,0.9}
    \lstset{
      backgroundcolor=\color{backcolour},
    }
    
    \begin{document}
      A a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a.
    
      \begin{minipage}{\linewidth}
        \begin{lstlisting}
          Code
        \end{lstlisting}
      \end{minipage}
    
    \end{document}
    

Thanks for advices.

Best Answer

I'd consider defining your own floating environment with the float package, doing something like this:

\usepackage{float}
\usepackage{listings}

\newfloat{lstfloat}{htbp}{lop}
\floatname{lstfloat}{Listing}
\def\lstfloatautorefname{Listing} % needed for hyperref/auroref

Now, you can create a floating listing with the following syntax:

\begin{lstfloat}
\begin{lstlisting}
% code here
\end{lstlisting}
\end{lstfloat}

This also plays well with page breaks, at least as much as figure and table do. For example, suppose the following code is in floatlist.tex:

\documentclass{article}
\usepackage{float}
\usepackage{listings}
\usepackage{lipsum}

\lstset{
    basicstyle=\ttfamily\footnotesize,
}

\newfloat{lstfloat}{htbp}{lop}
\floatname{lstfloat}{Listing}

\begin{document}
\lipsum[2]
\begin{lstfloat}
\lstinputlisting{floatlist.tex}
\lstinputlisting[firstline=1,lastline=10]{floatlist.tex}
\caption{The source of this document, one and a half times.}
\end{lstfloat}
\lipsum[3]

\end{document}

Here's the first page of output; notice that the float doesn't have to be on a new page.

Picture of the first page generated by the example code from floatlist.tex

Related Question