# Stop listings going over page breaks

I am using Pandoc to generate Tex from Markdown. It automatically generates listings when it comes across the appropriate Markdown (code indented four spaces). It then uses the listings package to instantiate those listings in Tex. The resulting code is:

\begin{lstlisting}[language=bash]
# java -version
java version "1.7.0_09-icedtea"
OpenJDK Runtime Environment (rhel-2.3.3.el6_3.1-i386)
OpenJDK Client VM (build 23.2-b09, mixed mode)
\end{lstlisting}


Sometimes these code listings extend across a page break and I'd prefer if they were bumped to the next page if this happens. I've looked around and seen a few suggestions about adding a minipage to the listing but since my Tex is being auto-generated via Pandoc it's not clear how I can do that without manually editing the resulting Tex document to add the minipages before and after the lstlistings.

So I am looking for:

1. A way to bump the listings to the next page automatically, or
2. A way to tell LaTex to redefine the lstlisting to have minipages before and after it.

EDIT: I should have added for those unfamiliar with Pandoc that the template it uses to generate Tex is here.

Thanks!

-
Do you have access to a document preamble? I'm unfamiliar with Pandoc's interface. –  Werner Dec 25 '12 at 5:25
This is the default LaTeX template that Pandoc generates this: github.com/jgm/pandoc-templates/blob/master/default.latex. –  jamtur01 Dec 25 '12 at 5:28
Line 47-49 designates a condition based on the existence of a listing (I guess). Instead of a single line (48) with \usepackage{listings}, use \usepackage{listings,float}\lstset{float=H}. Again, since I'm unfamiliar with Pandoc, you'll have to try it. –  Werner Dec 25 '12 at 5:40
You are correct - that enables the listings package when you pass the --listings option to Pandoc. I've made your change and given that a try but the listings continue to extend over the page break I am afraid. Thanks for your help though. –  jamtur01 Dec 25 '12 at 5:59
The float option seem to be specific to an environment, and cannot be set globally. Do you have access to the language settings it uses (language=bash)? –  Werner Dec 25 '12 at 16:04

As pointed out by Werner the normal way to prevent listings to do any package breaks is the option float. However this can't be done globally without changing the internals of listing.
Another hack would be to surround the environment listings with a non breakable box. This can be achieved with the facilities of the package etoolbox and the provided commands \BeforeEnvironment and \AfterEnvironment:
\BeforeBeginEnvironment{listings}{\par\noindent\begin{minipage}{\linewidth}}

Of course instead of minipage you can use any other environment.