9

When using \documentclass{article}, the MWE

\documentclass{article}
\begin{document}
\section{methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl (Compound 1)}
\end{document}

will produce: article gives a result as expected

If, however, the corresponding KOMA-Script class is used,

\documentclass{scrartcl}
\begin{document}
\section{methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl (Compound 1)}
\end{document}

will return:

scrartcl will produce an unpleasant line break within the heading

The line break between 'Compound' and '1' is obviously not necessary. One can prevent this by adding ~, but without understanding why scrartcl behaves this way, I see it more as a workaround. I'm using pdflatex from an up-to-date TeX Live 2019. Thanks for your help.

tstone-1
  • 467
  • 4
  • 13
  • +1: Interesting! – Dr. Manuel Kuehner Jan 11 '20 at 10:47
  • Hyphenation in headings (and titles) should be considered as really bad typography. So this is actually a noproblem-problem. Always set headings with \raggedright. – Sveinung Jan 11 '20 at 12:16
  • 1
    Names of chemical structures may easily span two or more lines and thus require the specification of hyphenation patterns. And this is where I can't adjust content to better reflect typographical conventions and recommendations. – tstone-1 Jan 11 '20 at 12:30
  • 2
    See https://github.com/latex3/latex2e/issues/247 – egreg Jan 11 '20 at 13:25

1 Answers1

8

What happens is that a hyphen in the last-but-one line is deemed bad by TeX, which sets \finalhyphendemerits=5000.

With just two lines there would be a hyphenated word in the last-but-one line, so TeX prefers to make one line more.

Workaround: set, for section titles, \finalhyphendemerits to zero.

\documentclass{scrartcl}
\usepackage{etoolbox}

\makeatletter
\appto\raggedsection{\finalhyphendemerits=\z@}
\makeatother

\begin{document}

\section{methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl
(Compound 1)}

\end{document}

enter image description here

Note that the tie Compound~1 is recommended in all contexts, but this is not the main point.

The same behavior can be replicated with \raggedright in normal text:

\documentclass{article}

\begin{document}

\raggedright

methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-%
methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl\-methyl (Compound 1)

\end{document}

enter image description here

If I add \finalhyphendemerits=0 after \raggedright, the output is two-line long.

enter image description here

egreg
  • 1,121,712
  • Thanks. I'm with you concerning the ~ in this case where we're talking about referencing a named part of the document. Could you maybe briefly explain why the output of the KOMA-Script class differs in this case? You're talking about TeX specific behavior there. – tstone-1 Jan 11 '20 at 13:07
  • 3
    @user18258 scrartcl uses \raggedright, whereas article doesn't. That's the main difference. Without \raggedright, creating a new line would have a very high cost, much more than the demerits for a final hyphen. – egreg Jan 11 '20 at 13:08
  • @user18258 I added an example with plain and simple \raggedright. – egreg Jan 11 '20 at 13:15