Overfull hbox - How do I fix this?

Overfull \hbox warnings are associated with text chunks that overflow over some text boundary. This could be the right text block margin, or some other fixed-width box that contains something that is wider than the width of the box.

In your case, most likely the typewriter-style string org.springframework.beans.factory.BeanFactory does not match any existing line-breaking rules (since it is technically not a word that has a hyphenation pattern).

You could force hyphenation, if this is what you're after, by inserting \- wherever hyphenation could occur. For example:

\texttt{org.spring\-frame\-work.beans.fac\-tory.Bean\-Fac\-tory}

For more on hyphenations, see Why do hyphenated words cause margin violations (and how can I prevent it)?

Of course, if hyphenation is not what you're after, you could also issue a \linebreak or \newline wherever you want to break the line. However, such hard-coded breaks should be left as a last resort since edits to your document may result in improper line breaks if the text is shifted.


I would not hyphenate such terms but break them without hyphenation. One suggestion would be to use package url:

\documentclass[a5paper]{article}
\usepackage{showframe}% for demontration only!
\usepackage{url}

\begin{document}
Line width violation by tt-terms like
\url{org.springframework.beans.factory.BeanFactory} and others.
\end{document}

But this would not break inside a letter sequence. You may define (and use) a new url style to change this:

\documentclass[a5paper]{article}
\usepackage{showframe}
\usepackage{url}

\makeatletter
\def\url@allbreakstyle{%
  \def\UrlBreaks{\do\.\do\@\do\\\do\/\do\!\do\_\do\|\do\;\do\>\do\]%
    \do\)\do\,\do\?\do\'\do+\do\=\do\#%
    \do A\do B\do C\do D\do E\do F\do G\do H\do I\do J\do K\do L\do M%
    \do N\do O\do P\do Q\do R\do S\do T\do U\do V\do W\do X\do Y\do Z%
    \do a\do b\do c\do d\do e\do f\do g\do h\do i\do j\do k\do l\do m%
    \do n\do o\do p\do q\do r\do s\do t\do u\do v\do w\do x\do y\do z%
    \do 0\do 1\do 2\do 3\do 4\do 5\do 6\do 7\do 8\do 9%
  }%
}
\def\url@restrictedbreakstyle{%
  \def\UrlBreaks{\do\.\do\@\do\\\do\/\do\!\do\_\do\|\do\;\do\>\do\]%
    \do\)\do\,\do\?\do\'\do+\do\=\do\#}%
}
\makeatother

\begin{document}
% Problem using the default style
Line width violation by tt-terms like this one:
\url{org.springframework.beans.factory.BeanFactory} and others.

% Break inside letter sequences using new style
\urlstyle{allbreak}%
Line width violation by tt-terms like this one:
\url{org.springframework.beans.factory.BeanFactory} and others.

\end{document}

You may switch back to the more restricted break style using \urlstyle{restrictedbreak}.

Note: If you are using package hyperref you need to use the original definition of \url without links. hyperref provides \nolinkurl to do so. But without \hyperref this wouldn't be defined. So a general solution, that may be used either with url or with hyperref would be to define \nolinkurl on you own, if and only if it hasn't been defined already:

\documentclass[a5paper]{article}
\usepackage{showframe}
% Only one of the following two lines is needed:
\usepackage{url}
\usepackage{hyperref}

\providecommand*{\nolinkurl}{\url}

\begin{document}
Line width violation by tt-terms like
\breaktext{org.springframework.beans.factory.BeanFactory} and others.

\end{document}

You may combine this with defining a new url style shown before.

Note: You should not define \nobreakurl reading the argument already, because this would change behavior of several characters at the argument. This is because original \nobreakurl and \url change cat-code of several characters before reading the argument! So you can write, e.g., \nobreakurl{this%and%that} without replacing % by a macro like \%. But, if you'd define \providecommand*{\nobreakurl}[1]{\url{#1}} (using url package) this will not work any longer, so don't do it!

Note: You may add additional changes to the new defined url style, e.g., redefinition of \UrlFont so change the font, that will be used. Have a look at the documentation of the package for more information about.

Tags:

Hyphenation