3

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 lstlisting and the line before that
  • the last line of lstlisting and 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}
masu
  • 6,571
  • 2
    could you add a link to the previous question of yours where this issue arose (I read it recently, there was a comment of egreg about this "feasible breakpoint"). –  Oct 23 '13 at 17:51
  • @jfbu: I intented to do that but forgot it somehow. Here it is, but I don't think it will help much, most of the content is in the last sentence of my actual question. – masu Oct 23 '13 at 17:58
  • Using \vbox as in Fran's answer in the mentioned post doesn't work? – karlkoeller Oct 24 '13 at 15:01
  • @karlkoeller no, it doesn't do the things mentioned above. – masu Nov 04 '13 at 18:27
  • related: http://tex.stackexchange.com/questions/159874/penalize-page-breaks-in-lstlistings – jub0bs Mar 02 '14 at 13:42

1 Answers1

3

An unanswered question with my ol' listings. How nice.

Replace the incumbent vspace - depending how you look at it - on listings.sty with abovedisplayskip and belowdisplayskip respectively.

To sum it up, we are going to line 1723:

 \else
 \lst@ifdisplaystyle
    \lst@EveryDisplay
    \par\penalty-50\relax
    \vspace\lst@aboveskip

And modify it to:

\else
\lst@ifdisplaystyle
\lst@EveryDisplay
\par\penalty-50\relax
\lst@belowskip\lst@aboveskip

Then to line 1776 regardless of its historical context:

 \else
         \lst@ifdisplaystyle
             \par\penalty-50\vspace\lst@belowskip

and modify it as well:

 \else
         \lst@ifdisplaystyle
             \par\penalty-50\lst@aboveskip\lst@belowskip

Your sample:

 \documentclass{article}

 \usepackage[a6paper]{geometry}
 \usepackage{listings}

 \begin{document}

 \textbf{Keep the first two together}

 \vspace{15\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{15\baselineskip}

 \noindent Line Before
 \begin{lstlisting}
 First line
 Middle lines
 Middle lines
 Middle lines
 Last line
 \end{lstlisting}
 Line After

 \end{document}

enter image description here

doed
  • 938
  • Does not really matter now for me as I have not used LaTeX for about 3 years, but thanks anyway. :D – masu Nov 18 '17 at 22:57
  • @masu the crystal ball whispered you are using LuaTeX... is the ball correct? – doed Nov 22 '17 at 22:19
  • It could be. But it is not. :D

    As I finished the PhD studies but did not go any further in the academic life I don't really do anything in LaTeX now... I mostly use python (and Office for documents). A bit of blasphemy here, I know. :D

    – masu Nov 23 '17 at 12:25
  • 1
    @masu That's unheard of. :) But it frees you from the constraints of TeX ubiquities. On the other hand, a great many who also completed their thesis without it, have said that they simply lacked the knowledge with TeX. So they did not pursue it any further. Had they been endowed with your skills to do so, they would have probably press on it. – doed Nov 23 '17 at 21:53