# enclosing a verbatim block in samepage creates both widow and orphan

I'm missing something: enclosing a verbatim block in a samepage expands the previous page and moves the verbatim block to the new page, but also drags the last line of the previous paragraph with it, creating both an orphan on the previous page and a widow on the new page, right before the verbatim block:

paragraph line one bla bla etc and so on
line two bla bla etc end of paragraph

\begin{samepage}
\begin{verbatim}
line  1
line  2
line  3
line  4
line  5
line  6
line  7
line  8
line  9
line 10
\end{verbatim}
\end{samepage}


If I exclude the samepage then the page break happens after line 8 within the verbatim block.

When I include samepage then the previous page is stretched (not raggedbottom) to bring the verbatim block to the next page intact. BUT it also breaks the last paragraph between line one and line two, creating an orphan line on the previous page and a widow on the new page.

Why is that and how do I prevent it?

• Welcome to TeX.SE! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – Troy Jan 3 '18 at 14:10
• samepage sets \@beginparpenalty to 10000. As verbatim is a list this prevents a page break before the verbatim. (see e.g. tex.stackexchange.com/a/46795/2388). You could add e.g. \goodbreak before. – Ulrike Fischer Jan 3 '18 at 14:15
• Thanks Ulrike, how do I accept your comment as an answer ? – netjiro Jan 3 '18 at 14:25
• You can't accept comments, you can only ask that someone changes a helpful comment into an answer. I will add one. – Ulrike Fischer Jan 3 '18 at 14:50

samepage sets \@beginparpenalty to 10000. This penalty is used by lists, with it you can prevent a page break directly before a list. The implementation of verbatim uses a list and so it is affected by the setting too (other environment which would suffer too are e.g. center and quote. You can add a break point e.g. with \goodbreak:

\documentclass{article}
\begin{document}
x

\vspace{37\baselineskip}
paragraph line one bla bla etc and so on\\
line two bla bla etc end of paragraph

%\goodbreak %uncomment
\begin{samepage}
\begin{verbatim}
line  1
line  2
line  3
line  4
line  5
line  6
line  7
line  8
line  9
line 10
\end{verbatim}
\end{samepage}
\end{document}