# How to keep an environment with the last line of the previous environment?

I quite often include attributed quotes in my documents, using something like:

\begin{quote}
Now I am become Death, the destroyer of worlds.
Also, I like bananas!

I like bananas so much that my quotes about them run to \emph{multiple paragraphs}!
\end{quote}
\begin{flushright}Ronald MacDonald\end{flushright}


I would like to keep my flushright environment on the same page as the last line of my quote environment.

My idea was to write a new environment which would force a page break, if one is needed, to occur at or before the last line of my quote.

I can see from other questions (in particular this one) that I should use widowpenalties, but I'm having trouble working out how to apply a penalty only to the last line.

The examples on the other question seem to suggest that \windowpenalties 3 x y 0 will apply a penalty of x to line -1, and y to all other lines.

In particular, it suggests

{\widowpenalties 4 10000 10000 -10000 0 \brokenpenalty 0 \par}


for breaking before the 3rd-last line of the paragraph.

However, I can't think how to write a newenvironment which will apply a penalty such that LaTeX breaks before the last line of a paragraph, but only apply it to the last paragraph of my quote.

• Could you bring us a MWE complete? Personally I don't thin you need something so strange. The solution is for a case where those environments fall in different pages, but if the document is not complete yet I think it won't be useful, but if it does, then you can find another solution as you've mentioned. For me works fine this lines in the preamble: \raggedbottom \clubpenalty=10000 \widowpenalty=10000 You can play with the values for fit it to your document. – Aradnix Aug 25 '14 at 6:06
• If the first environment introduces a vertical skip in anyway, they second will never be on the same line. Are you trying to quote a person? If so, have a look at package csquotes. – Johannes_B Aug 25 '14 at 6:30

You should not break the two parts into two environments:

\documentclass{article}
\usepackage{xparse}

\NewDocumentEnvironment{mquote}{m}
{\begin{quote}}
{{\interlinepenalty 10000 \brokenpenalty 0 \par}% <--- change to \par, if you like
\nopagebreak\makebox[\linewidth][r]{#1}\end{quote}}

\setlength\textheight{2cm}

\begin{document}
\noindent X\hrulefill X

\begin{mquote}{Ronald MacDonald}
Now I am become Death, the destroyer of worlds.
Also, I like bananas!

I like bananas so much that my quotes about them run to \emph{multiple paragraphs}!
\end{mquote}
\end{document}


With this code, the entire last paragraph will go in the next page together with the attribution; if you change

{\interlinepenalty 10000 \brokenpenalty 0 \par}


into \par, only the last line will move to the next page.

With

{\widowpenalties 3 10000 10000 0 \brokenpenalty 0 \par}


you'd ensure that the last two lines of the final paragraph will accompany the attribution on the next page.

For quotes like that I usually include the attribution into the quote environment and make it "flush right" with an \hfill

\begin{quote}
Now I am become Death, the destroyer of worlds.
Also, I like bananas!

I like bananas so much that my quotes about them run to \emph{multiple paragraphs}!\hfill Ronald MacDonald
\end{quote}


A MWE would help. The answer might depend on our format for "quote".