# Overfull hbox - How do I fix this?

I have a line in tex which has a markup (as a part of a line) as follows:

\texttt{org.springframework.beans.factory.BeanFactory}


This is giving a overfull \hbox warning. How do I fix this?

I am using MiKTeX 2.7 on Windows 7.

-

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 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 text the text is shifted.

-
oh how I wish there was an easy way of doing that sort of hyphenation without adding the actual hyphen. Adding hyphens to URLs and such is asking for misunderstandings... –  jonalv Dec 1 '11 at 8:17
By default, TeX will never hyphenate what's in typewriter type. –  egreg Dec 1 '11 at 8:19
That is a URL? Say what? No http://, including some CamelCase? It's anyone's guess. –  Werner Dec 1 '11 at 8:19
@Werner: Well, chimanrao's string definitely is not an URL, but most likely a Java namespace (following the convention to start with a component-wise reversed FQDN). Note that jonalv wasn't the one who asked the original question, and that he wrote "URLs and such" (where "such" obviously refers to other cases where an extra hyphen would change the meaning). What he is probably looking for is "" from babel (however it is only defined for some languages, I think) –  celtschk Dec 1 '11 at 9:08
yes this is java code, ideally i would want a line break to be inserted automatically, since this is regular text. I have tried the \- option, it works the problem is that it kind of hinders the reading of the tex file! –  chimanrao Dec 5 '11 at 10:03

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}

\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.

-
Would \href{#1}{\protect\nolinkurl{#2}}, where #1 is the url and #2 is something like org.\allowbreak springframework.\allowbreak beans.\allowbreak factory.\allowbreak BeanFactory not be sufficient? –  Stephen Dec 1 '11 at 19:00
@Stephen: This would not allow breaks everywhere and would set links. The question was how to break text not how to set links. And reading the argument already before calling \nolinkurl would prevent \nolinkurl to change cat-codes before reading the argument. So it's not a good idea. if you have e.g. _ at the argument. Maybe \nobreakurl or \Url could be used instead of \breaktext. I've not tested this. –  Schweinebacke Dec 2 '11 at 6:42
This works as in the line break happens automatically, however this is not a URL, so I dont want it to show like a url, its like a code snippet, so I was using a different font. How can I prevent it from showing up as a URL? –  chimanrao Dec 5 '11 at 10:36
@chimanrao: See my additional note about \UrlFont and please read the manual of package url. Nevertheless: You've used \texttt at your answer and the default \UrlFont is \ttfamily. So it's the same font. But you may additional use \urlstyle{tt} or \urlstyle{rm} or \urlstyle{sf}. All these url styles are defined by default. –  Schweinebacke Dec 5 '11 at 12:30