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.
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityI 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
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.
""
from babel (however it is only defined for some languages, I think)
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.
\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?
\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.
Dec 2, 2011 at 6:42
\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.
Dec 5, 2011 at 12:30
Hyphenation of a fully-qualified Java class name is going to be wrong. While, e.g., org.spring-framework.beans.factory.BeanFactory
is not valid Java, the rules about hyphens in package names are one of those things that can slip past junior developers consulting printed documentation (and perhaps a few senior devs as well). Better to allow breaks at the periods.
Since no one has mentioned it, I would suggest using \allowbreak
to allow breaking after the periods. I'd even go one step further and wrap up the \allowbreak
in its own command to make it a bit easier to type:
In preamble:
\NewDocumentCommand{\0}{}{\allowbreak}
In document:
\texttt{org.\0springframework.\0beans.\0factory.\0BeanFactory}
If we're really clever, we could even automate this by creating a new command that splits its argument at the periods, then reassembles it with \allowbreak
between each piece. I'm not at a computer with LaTeX at the moment, but something like this should work:
In Preamble
\ExplSyntaxOn
\NewDocumentCommand{\javaname}{ m }
{
\seq_set_split:Nnn \l_tmpa_seq { . } { #1 } % ❶
\texttt{
\seq_use:Nn \l_tmpa_seq { .\allowbreak } % ❷
}
}
\ExplSyntaxOff
In document
\javaname{org.springframework.beans.factory.BeanFactory}
The basic idea here is to split on the periods ❶ then join the split tokens again with a period followed by \allowbreak
❷.