How to Prevent a Page Break in LaTeX
LaTeX's automatic page breaking usually works well, but sometimes it splits content that should stay together — a heading from its first paragraph, a figure from its caption, or a code block in half. Several tools let you control where page breaks can and cannot occur. Bibby AI shows exact page break positions in the live preview, so you can spot bad breaks before compiling — a real time-saver compared to Overleaf's compile-and-check workflow.
Using samepage and \nopagebreak
The samepage environment keeps its contents on one page. \nopagebreak prevents a break at a specific point:
\documentclass{article}
\begin{document}
% Keep a block of content on the same page
\begin{samepage}
\section{Important Results}
This paragraph and the section heading will stay together
on the same page. No page break will occur within this block.
\end{samepage}
% Prevent break at a specific point
\section{Another Section}
\nopagebreak
This paragraph will not be separated from the heading above.
% \nopagebreak with penalty strength [1] to [4]
\nopagebreak[4] % strongest prevention
\end{document}Using minipage for Unbreakable Blocks
A minipage is completely unbreakable — LaTeX will never split it across pages. Use it for content that absolutely must stay together:
% Unbreakable block with minipage
\noindent
\begin{minipage}{\textwidth}
\textbf{Theorem 1.} Every bounded sequence in $\mathbb{R}^n$
has a convergent subsequence.
\textit{Proof.} We proceed by induction on the dimension $n$.
The base case $n=1$ follows from the Bolzano-Weierstrass
theorem. \qed
\end{minipage}
\bigskip % Add space after the minipageUsing needspace to Check Available Space
The needspace package lets you say 'start a new page unless there's at least X space remaining':
\usepackage{needspace}
% Only start this section if at least 4 lines of space remain
\needspace{4\baselineskip}
\section{Section That Needs Room}
This section will start on the next page if there isn't
enough room for the heading plus a few lines of content.
% Require at least 5cm of space
\needspace{5cm}
\begin{tabular}{lll}
a & b & c \\
d & e & f \\
\end{tabular}💡 Tips
- •\nopagebreak[4] is the strongest page break prevention; [1] is a mild suggestion.
- •minipage blocks can cause underfull page warnings — add \vfill before the minipage to push it to the next page gracefully.
- •For keeping a section heading with its first paragraph, many classes handle this automatically; add \nopagebreak after \section only if you see orphaned headings.
- •Bibby AI previews page boundaries live, so you can immediately see whether your \nopagebreak or samepage is working.
Try This in Bibby AI
Write LaTeX faster with AI auto-complete and instant compilation.
Start Writing Free