[Tex/LaTex] Avoid page break in theorem

page-breakingtheorems

I have a theorem which is half of it on another page, as I do for you automatically (without \newpage) get full theorem on the next page?

\documentclass[a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{theorem}
\newtheorem{teo}{Teorema}[chapter]

\begin{document}
\lipsum[1-5]

\begin{teo}
\lipsum[1]
\end{teo}

\end{document}

here

I have the left and I need right automatically (without \newpage).

Best Answer

Building on @cmhughes' answer, and therefore technically similar, the etoolbox package provides a means to hook into the start and end of an environment using \AtBeginEnvironment and \AtEndEnvironment. As such, a similar result is obtained (without using the ntheorem package):

\documentclass[a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\newtheorem{teo}{Teorema}[chapter]
\AtBeginEnvironment{teo}{\begin{minipage}{\textwidth}}
\AtEndEnvironment{teo}{\end{minipage}}

\begin{document}
\lipsum[1-5]

\begin{teo}
\lipsum[1]
\end{teo}

\end{document}

enter image description here

As you see from the input, the theorem package is not needed to reproduce the output. However, this may also just be due to your MWE. I would think, this is probably the best way to answer your question. However...


I'm more curious as to why you would want to do this. For example, you'll notice in the aforementioned example that the paragraph skips are stretched to fill the remainder of the page since the theorem overflows to the following page. This can be avoided by using something like a \newpage. So, in the hypothetical yet likely scenario where you have a very long theorem description, you might want to consider foregoing to flush the entire theorem body over to the next page. In this case I would suggest conditioning on where to break or not, based on the amount of space left on the current page. The needspace package provides \Needspace{<length>} and issues a \break if the remaining open space on the page is less than <length>. You may use it in the following sense:

\documentclass[a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{needspace}% http://ctan.org/pkg/needspace
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\newtheorem{teo}{Teorema}[chapter]
\AtBeginEnvironment{teo}{\Needspace{5\baselineskip}}% \break if fewer than 5\baselineskip is available on page

\begin{document}
\lipsum[1-5]

\newpage
\begin{teo}
\lipsum[1]
\end{teo}

\end{document}

The above code adds \Needspace{5\baselineskip} to the beginning of the teo environment (using \AtBeginEnvironment) which will issue a \break if less than 5\baselineskip is available on the current page. If more than this length is available, no \break will be issued. For this example, it produces the same result. Compare that to the disastrous output generated by

\documentclass[a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\newtheorem{teo}{Teorema}[chapter]
\AtBeginEnvironment{teo}{\begin{minipage}{\textwidth}}
\AtEndEnvironment{teo}{\end{minipage}}

\begin{document}
\lipsum[1-2]

\begin{teo}
\lipsum[1-5]
\end{teo}

\end{document}

and you'll understand why.