Wrap latex environment

I'm a beginning latex user. I have some programming code in my document which I format using the lstlisting environment from the package listings.

However this still allows the code segments to be split into two pieces when placed near the end of a page.

To fix this I also use a figure environment for code. This causes an often recurring pattern:

\begin{figure}[H]
\begin{lstlisting}
..code..
\end{lstlisting}
\end{figure}

How can I define a new environment for this pattern? I tried:

\newenvironment{mylisting}
{
\begin{figure}[H]
\begin{lstlisting}
}
{
\end{lstlisting}
\end{figure}
}

However using this environment gave errors all over.

-

migrated from stackoverflow.comApr 5 '11 at 14:07

This question came from our site for professional and enthusiast programmers.