I'm having an issue with a document I've created. The text should be fully left and right justified, and for the most part it is. However, a couple times in the document, the text extends beyond the right margin. How do I tell LaTeX (or more specifically, XeLaTeX) that this is not okay?
[Tex/LaTex] Justified text extending beyond margin
horizontal alignment
Best Answer
The example
I can reproduce your example (with a different font, but it's irrelevant):
Analysis of the problem
TeX doesn't hyphenate words that have a hyphen in them except at the hyphen itself (compound words). While this seems bad, it is in fact good: splitting a composed word require judgment for avoiding ambiguities. The discretionary hyphen
\-
command can help.Remedy
It's easier to wait till the document is in its final form for the text and add discretionary hyphens
\-
where needed. With a wide line length, there should be only a few cases that need attention. In that case I would try withexperi-ment-based
but avoiding a discretionary hyphen after
ex
.How to know the hyphenation points?
There are several ways. The easiest is to place
at the bottom of the document, just before
\end{document}
; then you'll find this in the.log
file:The
Underfull \hbox
message is due to how the\showhyphens
command works (it purposely builds an underfull box that will be shown with the possible hyphens). So we know that in American English the hyphenation points should beThe hyphenation algorithm is not infallible, though, so maybe looking in a dictionary can be better in case of doubt. The Oxford dictionary for AmEn gives indeed
ex|per|i|ment
Alternatively, type somewhere in your document (the end is good)
and you'll get
because TeX will do its best in order to avoid overfull boxes, but it won't be able to fit words in 0pt.
Automatic solution
There are automatic solutions, but I wouldn't recommend them. See, for instance, cfr's answer here. In English the problem is quite rare. Look at the end if you want a better remedy.
Another possibility is to use a command for compound words, say
Then typing
you'd get
Instead of
\comp
you might want to usebabel
shorthands; see babel: Adding ngerman' s language shorthands to english as the main document language but the result would not be completely under control. For instance, withexperiment-related
you might getthat is even worse than the overfull box.
General advice
Load the
microtype
package, that does wonders; for instance, the same document as beforewill give
without any intervention. Loading
microtype
will surely minimize manual corrections during the final revision.Final advice: don't worry about these problems until the document is in its final form. A change in wording will most probably destroy the work done about overfull boxes and, according to Murphy's law, create other ones.