[Tex/LaTex] Titles too wide and not hyphenated correctly

hyphenationtable of contents

In my master thesis, I have several complicated titles that are longer than one line and that are not hyphenated but go further than the textwidth (see MWE below).

Title too wide

Additionally, while the hyphenation seems to work ok in most cases in the TOC, in some cases were the title is just a little bit too long, the page numbers are out of alignment (I was not able to reproduce this in a simple example, see picture). How can I fix this? For the compound numbers I use the bpchem package.

Page numbers misaligned

  \documentclass[a4paper, 11pt, titlepage]{article} 
    \usepackage[T1]{fontenc}

    \begin{document}

    \section{Experimental}

    \subsection{4,4''-bis[2-(4-methoxy-2,6-dimethylphenyl)ethynyl]-5,5''-bis(methoxymethoxy)-2,2':6',2''-terpyridine }

    text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text

    \end{document} 

Best Answer

Section 3.2 of the bpchem manual shows how to add extra hyphenation to an IUPAC name. The short version is that you need to add additional \- and \| where hyphenation is allowed. Aside from that, you may need to adjust the default ToC margins so that if a word runs slightly long due to hyphenation rules, it won't interfere with the page numbers. This can be done at a lower level with the \@pnumwidth and \@tocrmarg lengths, or you can use a package like tocloft to handle it at a higher level. A complete example including the ToC:

\documentclass[a4paper, 11pt, titlepage]{article} 
\usepackage{bpchem}
\usepackage{lipsum}
\usepackage[T1]{fontenc}
\usepackage{tocloft}
\cftsetpnumwidth{1em}
\cftsetrmarg{3.5em}

\newcommand{\makesection}{%
\section{Experimental}

\subsection{\IUPAC{4,4''-bis[2-(4-meth\|oxy-2,6-di\|methyl\|phenyl)\|eth\|ynyl]\|\-5,5''\-bis\|(meth\|oxy\|meth\|oxy)-2,2':6',2''-ter\|pyr\|idine}}
\lipsum

\subsection{\IUPAC{4,4''-bis[2-(4-meth\|oxy-2,6-di\|methyl\|phenyl)\|eth\|ynyl]\|\-5,5''\-bis\|(meth}}

\lipsum

}

\begin{document}

\tableofcontents

\makesection
\makesection
\makesection
\makesection
\makesection

\end{document}

enter image description here

Related Question