How to Prevent Text from Overflowing in LaTeX
At a glance
- 1. Find the cause
Check the log for an Overfull \\hbox warning and locate the long word, URL, equation, or narrow container.
- 2. Add break points
Use suitable hyphenation, discretionary breaks, or a URL command so LaTeX can split long text.
- 3. Adjust paragraph spacing
Set a modest emergency stretch to help LaTeX fit difficult lines without changing the whole document's style.
- 4. Fix narrow containers
For tables, boxes, and equations, change the container or layout; paragraph settings do not fix every overflow.
Title: How to Prevent Text from Overflowing in LaTeX Category: latex
---- ARTICLE BODY ----
LaTeX normally wraps prose to fit the page. When text sticks out into the margin, the cause is usually a long unbreakable item, a paragraph with very restrictive line breaks, or content placed in a box that is too narrow. The log often reports this as an Overfull \hbox.
Start by finding the warning and identifying the material on that line. Then choose a fix that gives LaTeX a sensible place to break the content. Increasing the page width or shrinking the font is rarely the best first step.
Find what is overflowing
Compile the document and inspect the .log file for Overfull \hbox. The warning includes the amount by which a line extends beyond its allowed width and usually gives a nearby line number. The line number is approximate: the problem may begin earlier in the paragraph.
Common causes include long technical terms, URLs, file paths, inline code, long mathematical expressions, and table cells. LaTeX can break ordinary words at spaces and at recognized hyphenation points, but it cannot always break a long string with no permitted break point.
Let LaTeX break long text
For ordinary words, check that the word is spelled correctly and that LaTeX can hyphenate it. If a particular word needs help, add a hyphenation pattern in the preamble or mark a possible break in that occurrence:
\hyphenation{data-base}
This declares a hyphenation point for the word “database.” The pattern is written using the word’s parts separated by hyphens; it does not add hyphens everywhere in the output. For a one-off break, put \- at a permitted point in a word. LaTeX will show a hyphen only if it breaks there.
For a long identifier where a hyphen would be misleading, add optional break points instead:
\texttt{configuration\allowbreak{}parameter\allowbreak{}name}
The identifier can then wrap at the marked locations without inserting hyphens. Use breaks only where splitting the item will remain readable.
URLs need special treatment because their punctuation provides useful break points. The url package defines \url to typeset URLs and allow line breaks at appropriate locations. Avoid inserting ordinary spaces into a URL, since that changes its text.
A minimal working example
This example combines URL line breaking with a small emergency stretch for difficult paragraphs. It compiles with pdfLaTeX, LuaLaTeX, or XeLaTeX.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage{url}
\setlength{\emergencystretch}{2em}
\begin{document}
A long reference URL can be typeset with \url{https://example.org/research/materials/long-document-name}.
A technical identifier can be broken at meaningful points:
\texttt{configuration\allowbreak{}parameter\allowbreak{}name}.
\end{document}
microtype provides subtle character-level adjustments that can improve line fitting in supported engines. \emergencystretch gives TeX extra flexibility when it tries to set a paragraph that would otherwise have an overfull line. A value such as 2em is a modest starting point; it is not a command to make every line arbitrarily loose. Neither setting creates break points inside an unbreakable string, so handle URLs and identifiers explicitly.
When the text is inside a table or box
Paragraph-level settings do not necessarily fix material in a fixed-width container. In a table, use a paragraph column such as p{5cm} instead of a basic l, c, or r column when a cell needs to wrap. For tables that should adapt to the available width, packages such as tabularx provide flexible-width columns. If a framed box is too narrow, widen it or reduce its contents; forcing a paragraph to fit cannot make a fixed box wider.
Long displayed equations need mathematical layout rather than prose line-breaking. Use environments such as aligned or multline to split an equation at meaningful operators. Avoid inserting arbitrary breaks that make the mathematical structure harder to follow.
Common mistakes
- Using
\\to force a break in every paragraph. This can create poor spacing and does not solve an unbreakable word. Use natural paragraph wrapping or add a specific break point. - Changing the margins before diagnosing the warning. A long URL or identifier will often overflow even on a wider page.
- Relying on
\sloppyfor the whole document. It permits much looser spacing and can make paragraphs look uneven. Prefer a targeted break point or a small\emergencystretch. - Expecting
\emergencystretchto fix everything. It can help TeX adjust paragraph spacing, but it does not automatically split every token or repair an overwide table, box, or equation.
Choose the remedy based on where the text lives: add legitimate break opportunities in prose, use URL-aware formatting for links, and redesign fixed-width content when the container is the problem.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.