[Tex/LaTex] How to prevent a \lstinputlisting being split across pages

inputlistingspage-breaking

This question shows how to prevent a long listing from being split across several pages when the code is written directly into the tex file. I want to achieve the same effect when including code in separate files using \lstinputlisting like so:

\lstinputlisting[label=lst:mylist,caption=A Caption]{code/parametrised.xml}

How do I modify the listing environment definition given in the above question to do so?

\lstnewenvironment{code}[1][]%
  {\minipage{\linewidth} 
   \lstset{basicstyle=\ttfamily\footnotesize,frame=single,#1}}
  {\endminipage}

Any help would be greatly appreciated!

Edit: The question has been rephrased for clarity – The answers here relate to preventing a listing from being split across pages by ensuring it's treated like a unbreakable float. Howvever, this also means that listings longer than a page will not be paginated and will run off the bottom of the page.

Best Answer

declare the listing to a float, then it will be automatically be set in a box:

\lstinputlisting[float=h]{<file>}
    
Related Question