[Tex/LaTex] How to avoid wrong hyphenation in headings

hyphenationsectioning

I have the problem, that I am writing a (long) document. Now I found while checking the document for errors a very bad formatting of some heading:

The headline is quite long and therefore it has to be broken. For now I get a hyphenation quite few letters before the end of the headline. So it looks like:

Chapter 2: Fooooooo baaaaaaar fooooooo baaa-
                                          ar

Of course this is not what I want. I'd like to have the whole baaaar on the second line. For the moment I am not able to reproduce the problem in a minimal example, I will try this evening when I am at home again.

One other problem is that LaTeX breaks at a wrong position in heading sometimes. In normal Text I give a minus-sign to correct the hyphenation position but in headings this does not work properly, because in the headline (using fancyhdr or similar pagestyle) the minus also appears.

I think, general deactivation of hyphenation is not a good idea. I (have to) work on A5 paper and therefore it is easy to gain a complete line with few words. So hyphenation will be needed for a good look of the document, I think.

So in sum:

  • How to get rid of orphans in headings?
  • How to set the positions for the hyphenations in headings correctly?

Best Answer

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.