Is there a way to prohibit pagebreaking at the beginning/end of an lstlisting environment?
I'm trying to keep together the following two pairs of lines:
- the first line of
lstlistingand the line before that - the last line of
lstlistingand the line after that
But pagebreaking should be available (so using minipage is not a solution) - there are code snippets more than one page long.
As far as I know \end{lstlisting} implicitly adds a feasible breakpoint so it has to be undone to do this.
\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{listings}
\begin{document}
\textbf{Keep the first two together}
\vspace{22\baselineskip}
\noindent Line Before
\begin{lstlisting}
First line
Middle lines
Middle lines
Middle lines
Last line
\end{lstlisting}
Line After
\newpage
\textbf{Keep the last two together}
\vspace{17\baselineskip}
\noindent Line Before
\begin{lstlisting}
First line
Middle lines
Middle lines
Middle lines
Last line
\end{lstlisting}
Line After
\end{document}

egregabout this "feasible breakpoint"). – Oct 23 '13 at 17:51\vboxas in Fran's answer in the mentioned post doesn't work? – karlkoeller Oct 24 '13 at 15:01