# How can I ensure that a listing is not going to be split?

How can I ensure that a listing is not going to be split? If it is going to be split I want that the entire listing to move to the following page, effectively moving everything at pass. I mean any text that comes after the listing must remain after the listing.

-

You can wrap the lstlisting environment in a minipage environment to disallow page breaks:

 \begin{minipage}{\textwidth}
\begin{lstlisting}
content
\end{lstlisting}
\end{minipage}


However, this might lead to large white space at the page before the listing when it doesn't fit there. It would be better style to turn the listing into a float with a caption plus label and reference it in the text using \ref like a figure or table. This can be done using:

 \begin{lstlisting}[float,caption=<Caption text>,label=<label>]
content
\end{lstlisting}


and then using \ref{<label>} in the text.

This of course will allow the text written in the .tex file after the listing to appear before it in the final document, what you don't won't. As states, the better style would be to adjust your document for this instead.

-
thanks @Martin, i use the minipage advice and i'm taking special care adjusting the document to avoid the existence of lstlisting environment greater than a certain number of lines, so the white spaces never going very large –  mjsr May 18 '11 at 11:10
+1 for the float option! –  Trefex Apr 22 '14 at 16:32

\begin{framed} \begin{lstlisting} or \lstinputlisting{...} \end{lstlisting} \end{framed}

-
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. To start to make this a usable answer, you should make it into a complete compilable document including, explain what it does. In particular, your code snippet won't compile in a standard document, so you need to include about which packages are needed. –  Andrew Swann Dec 2 '13 at 19:19
...there's only no need to use framed specifically. What about minipage? –  Werner Dec 2 '13 at 19:26