7

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.

Radoslav
  • 125

1 Answers1

14

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

Nirusu
  • 3
Arun Debray
  • 7,126
  • 2
  • 30
  • 54
  • Thanks for it, but it doesn't work. My output from your scrip is the same, but when I multiple your line with \lipsum[2] three times (listing is passed through end of page to new page), then listing is alone on page. – Radoslav Nov 20 '15 at 22:04
  • I might be misunderstanding, but do you want it to split across a page boundary if it's too large or near the bottom of a page? – Arun Debray Nov 21 '15 at 00:27
  • Yes, It is reason that I need float for listing. My listing is split across a page boundary and I don't want it. – Radoslav Nov 21 '15 at 10:51
  • 1
    Aha, I see. Floats in LaTeX don't work that way: they do not split across page boundaries. Sorry, but you're probably better off using a regular lstlisting, rather than a float. – Arun Debray Nov 22 '15 at 02:02
  • Behavior of float in lstlisting (in my example) or in wrapped lstlisting by float (in your example) seem be the same. Both create lstlisting alone on the page, if 2 rules are observed: 1.) count of lines of lstlisting is greater then halve page, 2.) lstlisting is split across a page boundary. – Radoslav Nov 22 '15 at 08:38
  • My resolution is similar to link. Listing must be on suitable location. When listing is alone on the page, so it must be moved before or after paragraph or two. – Radoslav Nov 25 '15 at 21:06
  • 4
    Note: for cleveref support, add \crefalias{lstfloat}{listing}. – Lucas Werkmeister Jul 25 '18 at 16:31
  • 1
    And for subcaption support, add \DeclareCaptionSubType{lstfloat} so you can use sublstfloat environments analogous to subfigure. – Lucas Werkmeister Oct 03 '18 at 15:53
  • 1
    (And for cleveref and subcaption support, also add \crefalias{sublstfloat}{listing}.) – Lucas Werkmeister Oct 03 '18 at 16:19
  • in your second block, your environment should also end with \end{lstfloat} right? At least that's what my latex complains about. I tried to edit the answer, but the edit is too short – bmurauer Apr 03 '20 at 10:04
  • 1
    When I'm using your solution, I'm not getting a list of listing when using \lstlistoflistings. Is there a way to fix this but still yout lstfloat? – drOmiTs Jul 13 '20 at 13:34