How to avoid wrong hyphenation in headings?

If you are looking for a manual solution then you can use \- to indicate hyphen positions explicitly.

Warning: You should never use just an explicit minus sign as any text change make this invalid even in normal text!

You can also use \mbox{word} to prevent a word from being broken completely. Finally you might want to use \\ to explicitly break a line (without spreading it out) or \linebreak which keeps the line width.

However heading texts also go into the table of contents and possibly into the running header. So you either need to ensure that your modifications are ok in both places (e.g., \-would do no harm, but \\ might in those places) or you need to use the optional argument of the heading command to differenciate between the two uses, e.g.,

\section[Fooooo baaaaaaar foooooo baaaaar]
        {Fooooo baaaaaaar foooooo\\ baaaaar}

For a more general solution one could disable hyphenation within the heading command definition (and ensure that it is typeset raggedright) but this would need to be done in a class or package file and would depend on the base definitions for those commands.

Update

In a comment it was asked how one should denote a linebreak that should happen only in the TOC but neither in the main title nor in the running header.

Unfortunately this is a little tricky, because in the standard classes the interface of the section commands does not offer different arguments for the material that goes to TOC and running header. This is really a 2e deficiency.

So what you can do is to to define a command, say \TOCnewline which is given a definition in the main text so that it does nothing other than producing a space when typeset, but when written to the .toc file is preserved without changes. We can do this simply by making it a "robust" command (which isn't expanded in a \write):

\DeclareRobustCommand\TOCnewline[1][]{\unskip\space\ignorespaces}

This definition accepts an optional arg but ignores it. It produces a space and removes a space before and a space after. And during the TOC processing we give it a different definition: simply \\. So the whole thing looks like this if we put everything together:

\documentclass{article}

\pagestyle{headings}

\begin{document}

\newcommand\TOCnewline[1][0pt]{\\[#1]}
\tableofcontents
\DeclareRobustCommand\TOCnewline[1][]{\unskip\space\ignorespaces}

\newpage % to see a running header other than "CONTENTS"

\section{A test with\TOCnewline three lines
              \TOCnewline[10pt] and here with extra space}

some text

\end{document}

Not terribly elegant but it'll do :-) ... but clearly something to do better in LaTeX3.

Here's the output from page 1:

enter image description here

... and 2:

enter image description here

Warning: Using \\ directly works without an error message but it produces wrong spacing as it generates a penalty for breaking (which is ignored in the running header normally) plus \hfil which is not ignored. and if the class supports multiline running headers then it will generate a break in the running header too.


You can specify hyphenation with the \hyphenation command in the preamble.