# Disallow hyphenated words go into the margin in section heading

I had a long section heading that the last word will hyphenate at the end, which goes into the margin.

Minimal working example:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\begin{document}
\subsection{longlonglong wordswordswordwords that will cause problem}
\blindtext
\end{document}


produces:

MWE output

I don't want the problem be hyphenated and want it to break line before the last word. I tried three ways:

1. Manullly add \\. This produces the desired output but it's dirty and bad: the pdf bookmark do not recognize \\, the heading may fit into TOC but it just breaks, I may want to switch to double column or other document class.

2. I tried the solutions in this question. That is, using nohyphens provided by hyphenat package, but things gets worse:

nohyphens example

1. use penalty for hyphen:

\subsection{
\begingroup
\hyphenpenalty 10000
\exhyphenpenalty 10000
longlonglong wordswordswordwords that will cause problem
\endgroup
}


result:

hyphenpenalty example

Yeah it breaks at the last word but the first line becomes right-justified. I want them to be left-justified (and it should).

Hope some want can show me some workaround. Thanks in advance!

-

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\begin{document}
\subsection{\sloppy longlonglong wordswordswordwords that will cause problem}
\blindtext
\end{document}


-
That seems works! – Ivan Z. Xiao May 6 '11 at 22:55

in my opinion (agreed by the american math society as demonstrated in their document classes), "standalone" headings shouldn't be justified, but ragged right. this redefinition of the basic article section command adds only \raggedright at the end. the basic latex definition of \raggedright suppresses hyphenation as well as omitting the stretching of continued lines to the full text width. this operates only on the section heading itself, and does not affect the table of contents (though an argument can be made there as well for "controlled" ragged right (i.e., done in such a way as to not affect the positioning of page numbers).

\makeatletter
\renewnewcommand\section{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\normalfont\Large\bfseries\raggedright}}
\makeatother

-
So do you mean the behavior is correct as what it shows in solution #3? Seems that I have confusion between justified and ragged right. – Ivan Z. Xiao May 4 '11 at 17:01
@Ivan -- not sure which one is solution #3. the example shown by @Herbert is justified; that is, the first line is stretched to the full text width (compare the spaces between words in the heading with the spaces in the text). with ragged right, the first line of the heading would extend only as far as the "natural" length of the phrase through the last word that fits fully on the line. an example of ragged right text is the text that shows up in questions and answers in this forum -- the lines are not all the same length, and the width of spaces is uniform. – barbara beeton May 4 '11 at 18:12

You can use a \linebreak command to force the line break and an \mbox to prevent the problematic word from being hyphenated; the optional argument of the sectioning command can then be used to prevent problems in the ToC, in possible headings using the title, and in the bookmarks:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{hyperref}
\usepackage{blindtext}
\begin{document}
\subsection[longlonglong wordswordswordwords that will cause problem]{longlonglong wordswordswordwords that will cause\linebreak \mbox{problem}}
\blindtext
\end{document}

-
This appears to be a quick hack. I am just concerning the \linebreak will behave ugly when I change the format to two columns. In fact when I change the document type to IEEEtran, I do not need a \mbox to disallow hyphenation, since the hyphenation of the last word do not go to the margin. – Ivan Z. Xiao May 4 '11 at 1:46
@Ivan: Quick hacks tend to be OK for getting a particular document working; it's usually only when one writes a package that one needs to take care of alternative special cases. Do you need to be able to switch between output formats for this document? – Charles Stewart May 4 '11 at 6:33
@Charles I require pdf be generated, but dvi is also needed as backup. – Ivan Z. Xiao May 4 '11 at 16:52