# How can I keep the content of one environment with the next paragraph from another environment on the same page?

I am working with environments to be able to easily format the content of a document.

Now I want to define a rule so that the content from the ccetheme environment appears on the same page as the content from the next environment: it can be ccebody or ccesection

I have seen that it can be done with needspace or minipage, but I can't do it.

This is my minimal example:

\documentclass[ebook,11pt,twoside,openright,showtrims]{memoir}
\setstocksize{9in}{6in}
\settrimmedsize{9in}{6in}{*}
\setbinding{0.5in}
\setlrmarginsandblock{0.5in}{0.4in}{*}
\setulmarginsandblock{0.5in}{0.5in}{*}
\checkandfixthelayout
\usepackage{fontspec}
\usepackage{polyglossia}
\setdefaultlanguage{spanish}
\usepackage{csquotes}
\usepackage{xcolor}
\usepackage{layout}
\usepackage{needspace}
\usepackage{lipsum}
\usepackage[showframe, pass]{geometry}
\newcommand{\txtred}{\textcolor{red}}
\newcommand\n[1]{\txtred{\textbf{#1}}}

%--------------------------------------------------------------------- CCE styles
\newenvironment{ccetheme}[1]{%
% \begin{cr}{#1}
\nopagebreak
\par% Start a new paragraph
\centering% Alignment
\bfseries% Weight
\color{red}% Colour
{\large #1\par}% Change to \LARGE or \Huge
\ignorespaces
}
{% \end{cr}
\par% Start a new paragraph
%\begin{minipage}[t]{0.5\textwidth}

%\end{minipage}
}

%Títulos en negrita del propio CCE
\newenvironment{ccesection}{%
% \begin{cr}{#1}
\par% Start a new paragraph
\bfseries% Weight
\ignorespaces
}
{% \end{cr}
\par% Start a new paragraph
}

\newenvironment{ccebody}{%
\begin{minipage}{0.5\linewidth}
%\textit{redtext}\
%\fontfamily{gentium}\selectfont
\end{minipage}
}%
{% \end{cr}
%\nopagebreak
%\par% Start a new paragraph
}

\newenvironment{ccecite}{%
% \begin{cr}{#1}
\par% Start a new paragraph
\alterfont% Font
\ignorespaces
\leftskip1cm\relax
\rightskip1cm\relax
}
{% \end{cr}
\par% Start a new paragraph
}

\begin{document}
\section{Temas}
\begin{ccetheme}{La Alianza con Noé} {CEC 56-58, 71}\end{ccetheme}

\begin{ccebody}
\n{56} \lipsum[1-3]
\end{ccebody}

\begin{ccetheme}{Test} {CEC NNNN}\end{ccetheme}

\begin{ccebody}
\n{57} \lipsum[1-3]
\end{ccebody}

\begin{ccebody}
\n{58} \lipsum[5]
\end{ccebody}

\begin{ccebody}
\n{59} \lipsum[6-8]
\end{ccebody}

\begin{ccetheme}{Test} {CEC MMMM}\end{ccetheme}

\begin{ccebody}
\begin{ccesection}This is title on bold\end{ccesection}
\n{59} \lipsum[1-3]
\end{ccebody}

\begin{ccebody}
\n{60} \lipsum[5]
\end{ccebody}

\end{document}


As you can see in the images below, in this particular case there is an unpleasant cut between the ccetheme environment and the ccebody environment.

Also in this case, where I use the ccesection environment I would like the content of ccetheme along with the following to go to the next page:

I want to know if there is any way to force ccetheme content to always appear on the same page as the next paragraph.

As you can see, to achieve this I am putting \nopagebreak in ccetheme environment and trying to implement minipage in ccebody, but it doesn't work.

• There are three ways to keep certain things together. The first is to put both inside an unbreakable box, like a minipage. The second is to compute how much space is needed and use the needspace package. The third is to max out the penalty for page breaking between the two (see \@afterheading). – John Kormylo Feb 3 at 22:06
• @JohnKormylo But how could I do it from the environments? It is a very long document and I would like to do this automatically. Is that possible? – A. Cedano Feb 3 at 22:09
• you haven't provided any usable test code so hard to answer but adding \nopagebreak after the \par in ccetheme probably does something – David Carlisle Feb 3 at 22:15
• @DavidCarlisle Thank you. Doing that works best. I think if I manage to make the ccebody paragraphs stick together on the same page it would work as I hope. Is there a way to indicate that in the ccebody environment? Sorry for not giving a complete example, the text is too long. – A. Cedano Feb 3 at 22:29
• you can use packages like lipsum to generate dummy paragraphs while keeping the example short. if your ccebody never needs to break over a page (so is never longer than a page for example) you can use a minipage in its definition. a minipage never breaks – David Carlisle Feb 3 at 22:31

This still has a few warnings and spacing could be tweaked but something like this I think is closer to what you want

also use commands when there is fixed number of braced arguments and environments when you need to surround larger chunks of text.

\documentclass[ebook,11pt,twoside,openright,showtrims]{memoir}
\setstocksize{9in}{6in}
\settrimmedsize{9in}{6in}{*}
\setbinding{0.5in}
\setlrmarginsandblock{0.5in}{0.4in}{*}
\setulmarginsandblock{0.5in}{0.5in}{*}
\checkandfixthelayout
\usepackage{fontspec}
\usepackage{polyglossia}
\setdefaultlanguage{spanish}
\usepackage{csquotes}
\usepackage{xcolor}
\usepackage{layout}
\usepackage{needspace}
\usepackage{lipsum}
\usepackage[showframe, pass]{geometry}
\newcommand{\txtred}{\textcolor{red}}
\newcommand\n[1]{\txtred{\textbf{#1}}}

%--------------------------------------------------------------------- CCE styles
\newcommand{\ccetheme}[2]{%
\def\cceA{#1}%
\def\cceB{#2}%
}
%Títulos en negrita del propio CCE
\newcommand{\ccesection}[1]{%
\par% Start a new paragraph
\noindent\textbf{#1}%
\par
}

\newenvironment{ccebody}{%
\par% Start a new paragraph
\noindent\begin{minipage}[t]{\linewidth}%
\setlength\parindent{15pt}%
\ifx\cceA\relax\else
\begin{center}
\bfseries% Weight
\color{red}% Colour
\large \cceA\\\cceB
\end{center}%
\fi
\global\let\cceA\relax
\par
}%
{\end{minipage}%
\par}

\newenvironment{ccecite}{%
% \begin{cr}{#1}
\quote
\alterfont% Font
}
{\endquote
}
\raggedbottom
\begin{document}
\section{Temas}
\ccetheme{La Alianza con Noé} {CEC 56-58, 71}

\begin{ccebody}
\n{56} \lipsum[1-3]
\end{ccebody}

\ccetheme{Test} {CEC NNNN}

\begin{ccebody}
\n{57} \lipsum[1-3]
\end{ccebody}

\begin{ccebody}
\n{58} \lipsum[5]
\end{ccebody}

\begin{ccebody}
\n{59} \lipsum[6-8]
\end{ccebody}

\ccetheme{Test} {CEC MMMM}

\begin{ccebody}
\ccesection{This is title on bold}
\n{59} \lipsum[1-3]
\end{ccebody}

\begin{ccebody}
\n{60} \lipsum[5]
\end{ccebody}

\end{document}

• It seems to work as expected. Thank you. – A. Cedano Feb 4 at 0:05
• With this code I have lost the indentation in the first line of each paragraph of ccebody, do you know how I could recover it? – A. Cedano Feb 4 at 0:50
• stick \setlength\parindent{15pt} (or whatever) just after \begin{minipage} – David Carlisle Feb 4 at 0:51
• Addint this: \noindent\begin{minipage}[t]{\linewidth}% \setlength\parindent{15pt} is working only after 2nd paragraph, not indenting the 1st paragraph. – A. Cedano Feb 4 at 0:58
• @A.Cedano should work, I added a picture unless I missed something (I also stopped the heading indenting with \noindent) – David Carlisle Feb 4 at 9:15