# Justified text extending beyond margin

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?

• Self-hyphenation here could be the problem. You can adjust the hyphenation using ex\-peri\-ment-based. – Werner May 2 '14 at 20:06
• That fixed it. Any way to tell LaTeX not to panic when I hyphenate some words? – Ryan May 2 '14 at 20:08
• @Ryan TeX doesn't hyphenate words that have a hyphen in them except at the hyphen itself. While this seems bad, it is in fact good: splitting a composed word require judgment for avoiding ambiguities. 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 with experi\-ment-based, avoiding a discretionary hyphen after ex. – egreg May 2 '14 at 20:16
• You can set hyphenation globally, or locally. The above suggestion is local, and helps when you have a one-off case of hyphenation that stumps LaTeX. If you have something that is re-occurring, then you can set it using \hyphenation. See this answer. – Werner May 2 '14 at 20:17
• @Werner You can't use \hyphenation for compound words. – egreg May 2 '14 at 20:21

# The example

I can reproduce your example (with a different font, but it's irrelevant):

\documentclass{article}
\usepackage[textwidth=7.6in]{geometry}
\usepackage{mathpazo}

\begin{document}

A review of relevant literature indicates that researchers are evaluating
the four factors identified previously using experiment-based empirical
trials (Nunamaker et al.\ 2011). While these efforts yield critical insights,
experiment participants tasked with deceiving and avoiding detection likely
have no knowledge of the system that they are encountering. Consequently,
these experiments often lack elements of ecological validity as the adoption
and use of such a system publicly would mean that potential interviewees
would possess knowledge concerning its capabilities. Interviewees with
knowledge of the system can then attempt to avoid detection by employing
strategic behaviors designed to mitigate specific technologies and sensors
used by the system; these behaviors are often referred to as
countermeasures.

\end{document}


# 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 with

experi-ment-based

but avoiding a discretionary hyphen after ex.

\documentclass{article}
\usepackage[textwidth=7.6in]{geometry}
\usepackage{mathpazo}

\begin{document}

A review of relevant literature indicates that researchers are evaluating
the four factors identified previously using experi\-ment-based empirical
trials (Nunamaker et al.\ 2011). While these efforts yield critical insights,
experiment participants tasked with deceiving and avoiding detection likely
have no knowledge of the system that they are encountering. Consequently,
these experiments often lack elements of ecological validity as the adoption
and use of such a system publicly would mean that potential interviewees
would possess knowledge concerning its capabilities. Interviewees with
knowledge of the system can then attempt to avoid detection by employing
strategic behaviors designed to mitigate specific technologies and sensors
used by the system; these behaviors are often referred to as
countermeasures.

\end{document}


# How to know the hyphenation points?

There are several ways. The easiest is to place

\showhyphens{experiment}


at the bottom of the document, just before \end{document}; then you'll find this in the .log file:

Underfull \hbox (badness 10000) in paragraph at lines 18--18
[] \OT1/ppl/m/n/10 ex-per-i-ment


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 be

ex-per-i-ment


The 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)

\parbox{0pt}{\hspace{0pt}experiment}


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

\newcommand{\comp}{\hspace{0pt}\nolinebreak-\hspace{0pt}}


Then typing

experiment\comp based


you'd get

Instead of \comp you might want to use babel 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, with experiment-related you might get

experiment-re-
lated

that is even worse than the overfull box.

# General advice

Load the microtype package, that does wonders; for instance, the same document as before

\documentclass{article}
\usepackage[textwidth=7.6in]{geometry}
\usepackage{microtype}
\usepackage{mathpazo}

\begin{document}

A review of relevant literature indicates that researchers are evaluating
the four factors identified previously using experiment-based empirical
trials (Nunamaker et al.\ 2011). While these efforts yield critical insights,
experiment participants tasked with deceiving and avoiding detection likely
have no knowledge of the system that they are encountering. Consequently,
these experiments often lack elements of ecological validity as the adoption
and use of such a system publicly would mean that potential interviewees
would possess knowledge concerning its capabilities. Interviewees with
knowledge of the system can then attempt to avoid detection by employing
strategic behaviors designed to mitigate specific technologies and sensors
used by the system; these behaviors are often referred to as
countermeasures.

\end{document}


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

• Thank you very much @egreg. But, I have a question. How can I load the microtype package? – xiuhays Nov 30 '14 at 16:55
• @xiuhays \usepackage{microtype} – egreg Nov 30 '14 at 19:03