I have a document with very long section titles which span multiple lines (potential exam questions). I'd like to restrict them to one line in the table of contents.
I just found out that this is easy to achieve manually by providing a short title, e.g., \subsection[Short Title]{Title}
. However, I'd like to automatize this. I'm pretty sure that it's easy to redefine \subsection
etc. to automatically set the respective short title, I'm just not adept enough to come up with this myself.
Example:
\documentclass{article}
\begin{document}
\tableofcontents
\section{What I have}
\subsection{This is an insanely long subsection title that is supposed to span multiple lines, which it does because it's insanely long}
\subsubsection{This is a similarly long subsubsection title that should also span more than one line, which it does as it's very long}
\section{What I want}
\subsection[This is an insanely long subsection title that is supposed to span\ldots]{This is an insanely long subsection title that is supposed to span multiple lines, which it does because it's insanely long}
\subsubsection[This is a similarly long subsubsection title that should\ldots]{This is a similarly long subsubsection title that should also span more than one line, which it does as it's very long}
\end{document}
Ideally, the titles in the TOC would automatically be truncated wordwise such that they just fit on one line, but a solution with a fixed width or even a fixed number of characters would be OK as well.
Bonus requirement: I'm also coloring the titles to indicate the status of the respective section, i.e., a solution that also works with titles wrapped in \textcolor{...}{...}
(or allows to set the color by means of an optional argument or so) would be great.
\subsection{This is a long \splithere subsection title}
. Do you want an answer with that? – Phelype Oleinik Oct 10 '18 at 13:02