[Tex/LaTex] Disallow hyphenated words go into the margin in section heading

line-breakingsectioning

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!

Best Answer

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

enter image description here