[Tex/LaTex] How to prevent words from running into the right margin

line-breaking

I'm revising one of the final drafts of my thesis and on several pages there are words running into the right margin.

My question is whether rewording the paragraph is the only way to get rid of this problem? (I'd like to avoid using the command \sloppy because it screws up spacing between words.)

The related question How can I make my text never go over the right margin by always hyphenating or breaking on word-boundaries? offers using \sloppy or rewording the paragraph as solutions. I'd like to know whether there are alternative solutions.

Update (January 31st 2016). Using the microtype package did the job. Thanks to Henri Menke for suggesting this.

Best Answer

The German LaTeX Wikibook contains a chapter about hyphenation -- here's the link.

babel Package

First of all make sure that you use the babel package. It loads hyphenation rules for the language you use.

hyphsubst Package

If you write in German maybe the hyphsubst package is a good option.

Manually Help LaTeX with the Hyphenation (Globally)

As stated here or here or here for example you can add specific hyphenation rules in the preamble of your document:

% needs babel package
\hyphenation{cryp-to-graphy lalala-la lolo-lolo-lulu}

Manually Help LaTeX with the Hyphenation (Locally)

I would recommend this as the last step when you finished your document. Then you can manually help LaTeX.

The command \overfullrule=2pt in the preamble will show you where LaTeX has a problem with hyphenation:

\documentclass{article}

\usepackage[ngerman]{babel}

% showframe to show the layout frames... :)
\usepackage[textwidth=50mm,showframe]{geometry}

% Show overfull boxes and lines (TeX primitive)
\overfullrule=2pt

\begin{document}

Lalalalalalala lalalalala lalalalala lalalalala

\end{document}

enter image description here

Here's a list of manual options:

  • \- Hyphenation a word only here and nowhere else (all other hyphenation rules are not valid anymore).
  • "- Hyphenation a word also here (the other hyphenation rules are still valid).
  • "= This produces a dash. Hyphenation is allowed here and the other hyphenation rules are still valid. Normally if a word contains a dash the dash is the only place where a hyphenation can occur.
  • "~ This produces a dash where no hyphenation is allowed.
  • "" Here a hyphenation can occur but no automatic hyphenation dash will be produced by LaTeX.

These commands work if you use ngerman option for babel. I do not know how to make them work if you use other languages. Can somebody help me on this?

\documentclass{article}

\usepackage[ngerman]{babel}

% showframe to show the layout frames... :)
\usepackage[textwidth=50mm,showframe]{geometry}

% Show overfull boxes and lines (TeX primitive)
\overfullrule=2pt

\begin{document}

Lalalalalalala lalalalala lal"-lalala lalalalala

\end{document}

enter image description here

microtype Package

Stolen from the comments (@Henri Menke)!

The microtype package also improves hyphenation. The following cite is taken from the package manual:

Micro-typography is the art of enhancing the appearance and readability of a document while exhibiting a minimum degree of visual obtrusion. It is concerned with what happens between or at the margins of characters, words or lines. Whereas the macro-typographical aspects of a document (i.e., its layout) are clearly visible even to the untrained eye, micro-typographical refinements should ideally not even be recognisable. That is, you may think that a document looks beautiful, but you might not be able to tell exactly why: good micro-typographic practice tries to reduce all potential irritations that might disturb a reader.