2

It seems that Latex has a problem with the correct hyphenation of very long, one word (sub-)section titles in the title itself and the toc. With the following MWE:

\documentclass[12pt]{scrartcl}
\usepackage{fontspec}
\setmainfont{Times New Roman}
\usepackage[ngerman]{babel}
\usepackage[left=2.5cm,right=2.5cm,
           top=2.5cm,bottom=2.5cm]
           {geometry}
\usepackage{sectsty,ragged2e}
\allsectionsfont{\RaggedRight}
\begin{document}
\tableofcontents
\section{Section}

\subsection{(5\textit{R},6\textit{E},8\textit{E})-5-[(2-methoxyethoxy)methoxy]-\textit{N}-methoxy-\textit{N}-methyldeca-6,8-dienamid}

\end{document}

i end up with overfull hboxes in the toc and the title of the subsection.

enter image description here

Is there any way to prevent this mess?

  • Possible related: https://tex.stackexchange.com/questions/343188/fancyhdr-chapter-and-section-titles-overlap-when-titles-are-long – Sebastiano Aug 20 '22 at 19:30
  • Since the "very long word" is the IUPAC name of a chemical, you may want to take a look at the \iupac command provided by the \chemmacros package. You may also be interested in using its \cip command in order to avoid using \textit{R} over and over. – leandriis Aug 20 '22 at 19:56
  • This is a situation where it would seem best to set these headings ragged right, and avoid additional word-hyphenation altogether. – barbara beeton Aug 20 '22 at 20:48

2 Answers2

1

With the help of the \iupac command from chemmacros:

enter image description here

(Red lines indicate the margins.)

\documentclass[12pt]{scrartcl}
\usepackage{fontspec}
\setmainfont{Times New Roman}
\usepackage[ngerman]{babel}
\usepackage[left=2.5cm,right=2.5cm,
           top=2.5cm,bottom=2.5cm]
           {geometry}
\usepackage{sectsty,ragged2e}
\allsectionsfont{\RaggedRight}
\usepackage{chemmacros}

\begin{document} \tableofcontents \section{Sectionn}

\subsection{\iupac{\cip{5R,6E,8E}-5-[(2-methoxyethoxy)methoxy]-\N-methoxy-\N-methyldeca-6,8-di|enamid}}

\end{document}

leandriis
  • 62,593
  • 1
    Thank you! This comes very close to what I am looking for but it appears to me that the hyphenation is sometimes a little bit odd (even tough grammatically correct). I think it would be a little bit better if latex wouldnt use the whole textwidth given by breaking the line in the middle of word but adding a linebreak at one of the "-". – SomeDude Aug 21 '22 at 10:14
  • I'm unable to test, but it seems to me that setting \hyphenpenalty quite high and \exhyphenpenalty quite low, even slightly negative, might encourage line breaking preferentially at the existing hyphens. \Raggedright is definitely a good thing here. – barbara beeton Sep 20 '22 at 00:37
0

polish.sty rules!

\documentclass[12pt]{scrartcl}
\usepackage{fontspec}
\setmainfont{Times New Roman}
\usepackage[ngerman]{babel}
\usepackage[left=2.5cm,right=2.5cm,
           top=2.5cm,bottom=2.5cm]
           {geometry}
\usepackage{sectsty,ragged2e}
\usepackage{polski}
\allsectionsfont{\RaggedRight}

\begin{document}

\overfullrule5pt \tableofcontents \section{Section}

\subsection{(5\textit{R},6\textit{E},8\textit{E})-5-[(2-methoxyethoxy)methoxy]-\textit{N}-methoxy-\textit{N}-methyldeca-6,8-dienamid}

And the solution:

\subsection{(5\textit{R},6\textit{E},8\textit{E})\dywiz5\dywiz[(2\dywiz{}methoxyethoxy)methoxy]\dywiz\textit{N}\dywiz{}methoxy\dywiz\textit{N}\dywiz{}methyldeca\dywiz6,8\dywiz{}dienamid}

\end{document}

enter image description here

To tell the truth, it is enough only to extract \dywiz from polish.sty, defined as \def\dywiz{\kern0sp\discretionary{-}{-}{-}\penalty10000\hskip0sp\relax}.