# Prevent pagebreak when nesting environments

I have two nested environments, each for a different purpose, the first one is giving me a title. Unforuntately, although I have a \nobreak at the appropriate spot (I think) when I call the secondlevel environment right after the first, it doesn't prevent a pagebreak from occurring right after the title.

\documentclass{report}
\usepackage{lipsum}
\usepackage{expex}
\newcounter{mycounter}[chapter]%
\renewcommand{\themycounter}{\thechapter.\arabic{mycounter}}
\newenvironment{firstlevel}{%
\refstepcounter{mycounter}%
\noindent\textbf{Testing\ \themycounter}%
\par\nobreak\addvspace{\medskipamount}% << nobreak ignored
\noindent\ignorespaces
}{%
}
\newenvironment{secondlevel}{}{}

\begin{document}

Extraline to reach end of page

Extraline to reach end of page

\lipsum[2-6]

\begin{firstlevel}
\begin{secondlevel}

\lipsum[2]

\end{secondlevel}
\end{firstlevel}
\lipsum[1-2]
\end{document}


How can I force \nobreak to be respected in this case?

(The secondlevel environment in the MWE is only a fake environment but in the real setup it does some important stuff which I cannot have the firstlevel environment do, as they don't always occur together.)

EDIT: Full disclosure, I'd need this not only in the above, but also in two other scenarios: one in which the next paragraph would start with a quote, as in \begin{firstlevel}\begin{quote}Quote\end{quote}\end{firstlevel}

This works with the above solution. But it also happens that the next paragraph starts with an expex example:

\begin{firstlevel}
\ex \begingl  \gla  Test  //
\glb  Test  //
\endgl \xe
\end{firstlevel}


In this scenario it doesn't work.

The page break is not ignored, it is just that you have introduces an all white paragraph just with a single line and no text and it is that line that is kept with the heading.

If you use

\par\nobreak\addvspace{\medskipamount}% << nobreak ignored
\noindent X\ignorespaces
}{%
}


then you will see

and if you omit the X the paragraph is still there, but harder to see.

If you use \noindent then the paragraph starts in the begin code of the environment so you can not start the environment body with a blank line or a list or any kind of display.

LaTeX never does this in the environments it defines, it would be better to define the enviornment using list and setting the environment using \item or as a heading. Both of which will prevent page breaks and optionally suppress indentation without using \noindent (internally they remove indentation using \setbox rather than use \noindent)

For example

\documentclass{report}
\usepackage{lipsum}
\newcounter{mycounter}[chapter]%
\renewcommand{\themycounter}{\thechapter.\arabic{mycounter}}
\newenvironment{firstlevel}{%
\refstepcounter{mycounter}%
\subsection*{Testing\ \themycounter}%
\par\nobreak\addvspace{\medskipamount}% << nobreak ignored
}{%
}
\newenvironment{secondlevel}{}{}

\begin{document}

Extraline to reach end of page

Extraline to reach end of page

\lipsum[2-6]

\begin{firstlevel}
\begin{secondlevel}

\lipsum[2]

\end{secondlevel}
\end{firstlevel}
\lipsum[1-2]
\end{document}

• That looks like it's getting me into the right direction, thank you! Works in the above, but I have two slightly different scenarios where it still doesn't, for example if I do \begin{firstlevel}\begin{quote}Quote\end{quote}\end{firstlevel} I still have the title as the last line. Is there maybe an even more robust approach, that would prevent the break no matter what the next part looks like? – jan Oct 23 '16 at 9:57
• I've clarified in the question the other two situations where I also use that environment. – jan Oct 23 '16 at 10:01
• OK, I had forgotten to get rid of the \noindent tag so with a quote environment it does work. Sorry, my bad! But it does not work if what comes next is an expex example (see edits in the question). Any ideas why? – jan Oct 23 '16 at 12:47
• @jan I just tried adding your \ex instead of the secondlevel in this answer and it did not break after the heading testing 0.1 carried over to the second page – David Carlisle Oct 23 '16 at 15:27
• @jan \def\lingexbreakpenalty{10000}% – David Carlisle Oct 23 '16 at 22:12