[Tex/LaTex] Prevent environments from breaking in two pages

environmentspage-breaking

I am making my own environments for a mathematical document (environments such as Theorem, Definition, Lemma… which often consists of a title corresponding to the environment and possible things at the end of the environment (such as the proof environment which has a cute little square at the end)). However, I hate it when something such as

Lemma.

————————–Page break here—————-

Beginning of the text in the lemma

happens. Is there any way to require the environment to fit all its content in one page in the environment description? Something like a 'no-page-break-allowed-here' command.

EDIT : For example, I'd like this environment to be modified so that it doesn't break :

\newenvironment{definition}
{
    \textbf{\underline{Definition.}}
    \vspace{12 pt}
    \itshape
}

Best Answer

For your theorem-like structures, it's better to use a dedicated package (amsthm or ntheorem are the favorite ones, and there's a fron-end for both of them: thmtools). Using either of those packages you can easily define theorem styles and numbered or unnumbered structures that use those styles.

In the following example I used the amsthm package to define an unnumbered structure having similar specifications as the definition environment of your code snippet. The \newline used in the sixth mandatory argument causes the head of the theorem to appear on a line of its own, but doesn't prevent an undesired page break between the head and the body. For this, I used \needspace from the needspace package:

\documentclass{article}
\usepackage{needspace}
\usepackage{amsthm}

\newtheoremstyle{definition}
  {\topsep}
  {\topsep}
  {\itshape}
  {0pt}
  {\bfseries}
  {\newline}
  {0pt\needspace{2\baselineskip}}%
  {\thmname{\underline{#1}}~\thmnote{(#3)}}
\theoremstyle{definition}
\newtheorem*{definition}{Definition}

\begin{document}

\vspace*{43\baselineskip}% just to test that no separation is produced
\begin{definition}
Test
\end{definition}

\end{document}

As a final remark, not related to the question, underlined text is usually not considered a good typographical practice; in your case, using bold-faced font and underlining seems superfluous.