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.

lstlisting, rather than a float. – Arun Debray Nov 22 '15 at 02:02cleverefsupport, add\crefalias{lstfloat}{listing}. – Lucas Werkmeister Jul 25 '18 at 16:31subcaptionsupport, add\DeclareCaptionSubType{lstfloat}so you can usesublstfloatenvironments analogous tosubfigure. – Lucas Werkmeister Oct 03 '18 at 15:53cleverefandsubcaptionsupport, also add\crefalias{sublstfloat}{listing}.) – Lucas Werkmeister Oct 03 '18 at 16:19\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\lstlistoflistings. Is there a way to fix this but still youtlstfloat? – drOmiTs Jul 13 '20 at 13:34