LaTeX Warning: Overfull \hbox (text in margin)
An "Overfull \hbox (12.3pt too wide)" warning means one line is wider than the text block and sticks into the right margin. Find the line from the log, then load \usepackage{microtype}, which clears many small overruns; break long URLs with \usepackage{xurl}; add hyphenation points with \- or \hyphenation{...}; and size images and tables to \linewidth.
Not sure it worked on your document? Paste it into LetX and compile it, free and in the browser.
Why it happens
TeX breaks each paragraph into lines by choosing break points that keep the spaces between words within a set tolerance. When no choice works, usually because of a long word it cannot hyphenate, a URL, code in \texttt, inline math, or a box such as an image or a table that is wider than the line, it lets the line run long and reports by how much. An overrun of a point or two is invisible; anything bigger shows as text in the margin.
Example
\usepackage{microtype} % clears many small overruns
\usepackage{xurl} % URLs may break at any character
\hyphenation{electro-encephalo-graphy} % teach TeX a word
% in the document:
See \url{https://example.com/some/very/long/path/to/a/file.html}
for the data.
% One-off break points inside a word:
thermo\-lumi\-nescenceReading the warning
The log gives the size of the overrun and the source lines, then the text of the offending line, for example "Overfull \hbox (15.0pt too wide) in paragraph at lines 80--82". "in paragraph at lines 80--82" points to a paragraph in your source. "detected at line 12" means a box you sized yourself, such as \hbox to 2cm{...}, is too small for what you put in it.
To see the lines in the PDF, set \overfullrule in the preamble, as below: LaTeX draws a black bar in the margin next to every overfull line. The draft class option does the same, but it also replaces every image with an empty frame.
\setlength{\overfullrule}{5pt} % mark overfull lines in the PDFFixes by cause
A long word: add break points where it may hyphenate (data\-base), or list it once in the preamble with \hyphenation{data-base}. A compound with a hyphen, such as state-of-the-art, can break only at its hyphens; \hyp{} from the hyphenat package lets each part hyphenate too. Do not load hyphenat with the none option to fix an overrun: none switches hyphenation off, which makes overfull lines more likely.
A URL or a file path: load xurl (or url, which hyperref loads for you) and always write it as \url{...}, so it can break at slashes or, with xurl, anywhere.
Code in \texttt: typewriter fonts are set up with hyphenation switched off, so long identifiers overflow. Allow a break with \allowbreak inside the name, or set code blocks with the listings package and breaklines=true.
Inline math: TeX breaks a formula only after a relation or a binary operator at its top level. Move a long formula into a display.
An image or a table: set images to width=\linewidth. A tabular that is too wide is reported as overfull "in paragraph" at the lines of the table; use tabularx with X columns, a smaller font, or the landscape environment.
microtype, \emergencystretch and \sloppy
microtype lets pdfLaTeX and LuaLaTeX stretch or shrink letters very slightly and hang punctuation into the margin, which gives the line breaker more room. Try it first: it often clears small overruns without any other change. Under XeLaTeX it can only do the margin part.
\setlength{\emergencystretch}{3em} gives TeX extra stretch for a last-resort pass over any paragraph it could not set, which fixes most of the rest at the cost of a few looser lines.
Neither can break a word that has no break points. For a paragraph that still overflows, \sloppy, or the sloppypar environment around that one paragraph, sets a very high tolerance. It removes the overfull line by allowing very loose ones, and the log may then say "Underfull \hbox (badness 10000)" instead, so use it locally, not for the whole document.
\usepackage{microtype}
\setlength{\emergencystretch}{3em}
% in the document:
Our measurements of the electroencephalographically monitored
participants showed considerable variability in the
thermoluminescence dosimetry.
% Without the two preamble lines, this paragraph gives
% Overfull \hbox (0.61176pt too wide); with them, no warning.The image that is exactly 15pt too wide
\includegraphics[width=\textwidth]{...} at the start of a paragraph is overfull by exactly the paragraph indent, 15pt in a 10pt article, because the indent comes first and the image after it. Put \noindent before it, or place the image in a figure environment, where paragraphs are not indented.
\usepackage{graphicx}
% in the document:
\noindent\includegraphics[width=\textwidth]{example-image}Frequently asked questions
Can I just ignore Overfull \hbox?
Small overflows (a point or two) are often harmless, but visible text in the margin should be fixed. Set \hfuzz=2pt to silence tiny ones.
How do I see which line?
The log shows the source lines and how many points the line overran, e.g. "Overfull \hbox (15.0pt too wide) in paragraph at lines 80--82". \setlength{\overfullrule}{5pt} also marks each one with a black bar in the PDF.
What is the difference between Overfull and Underfull \hbox?
Overfull means a line is too wide and runs into the margin. Underfull means a line had to be stretched so far that its spacing looks loose; it is often harmless, for example after a forced line break.
Does \sloppy fix Overfull \hbox?
It removes most of them by allowing very loose lines, which can look worse than the overrun. Try microtype and \emergencystretch first, and use sloppypar only on the paragraphs that need it.
Why does my image cause Overfull \hbox?
An image as wide as \textwidth, plus the paragraph indent in front of it, is wider than the line. Add \noindent, or put it in a figure environment.
Related LaTeX fixes
Browse all LaTeX problems & fixes, or start with the LaTeX guide.
Try the fix in LetX
Open the editor, paste your code, and compile in seconds to see the error clear. Free, in your browser.
Open LetX Free