Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?
share|improve this question
add comment

3 Answers

up vote 15 down vote accepted

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.

share|improve this answer
1  
to follow up on what frank has said, \- immediately preceding a word will suppress hyphenation in just that word. in my opinion, hyphenation should generally be turned off entirely in headings, and they should be set ragged rather than justified. in some exceptional cases, e.g. to avoid an extra line, if there is enough horizontal space to the right within the margin (headings are often limited to a narrower width than the regular text width), there's always \rlap, and a method of overriding the automatic entry into the t-of-c isn't too difficult to devise. –  barbara beeton Dec 20 '11 at 18:45
    
another tactic -- use the ~ tie to encourage more words onto a new line if it otherwise looks too short; best used in conjunction with suppressed hyphenation. –  barbara beeton Dec 20 '11 at 18:48
    
@barbarabeeton The preceding \- did not work properly. I got a Minus in the first line and the complete word wrapped around to the second line. Am I doing something wrong with \chaper{A verly long text that should \-exceed a line}? –  Christian Wolf Dec 21 '11 at 20:47
    
@Christian you did nothing wrong, Barbara is mistaken in this case. \- produces a discretionary hyphen and this means a break can be taken there and if so you will get the hyphen on the first line. Just use an \mbox around the word. –  Frank Mittelbach Dec 21 '11 at 21:39
    
@FrankMittelbach The trick with the mbox around does also not really help: Then the whole word is typeset in the FIRST line and produces a overfull hbox. This looks ofen nasty and sometimes even goes across the page edge. I inserted a \texorpdfstring{\\}{ } before the mbox, but that destroys the table of contents and the running header. –  Christian Wolf Dec 23 '11 at 9:00
show 6 more comments

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

share|improve this answer
add comment

In you heading you need to give \raggedright it will automatically break the word:

\newcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries\raggedright}}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.