2

I have used the listings package for including a program into a LaTeX document. But the program splits into two when a page is finished. I want to make the listing behaved like an image, so that it will not split and the whole program stays on the same page.

Code I used is:

\lstinputlisting[caption={Hello},label={pgm1}]{main.c}
Arun Debray
  • 7,126
  • 2
  • 30
  • 54
  • Welcome! Please help us to help you and provide a minimal working example of fully compilable code, illustrating your problem, starting with \documentclass{...} and ending with \end{document} – JMP Apr 27 '16 at 20:44

1 Answers1

3

You can pass the float option to \lstinputlisting to make it float:

\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\footnotesize\ttfamily}
\usepackage{lipsum}

\begin{document}
\lipsum[1]
\lstinputlisting[caption={Hello}, float]{main.c}
\end{document}

enter image description here

There's also this question and its answers which have solutions for avoiding page breaks that don't rely on floats.

Arun Debray
  • 7,126
  • 2
  • 30
  • 54