I’ll briefly explain the background in case this can be solved simpler but I’d still be interested in an answer to the actual question, too.
My chapter titles are very narrow and use a large font so that it may happen that it has to be broken. I want to prevent the hyphenation of words at all cost, so I thought I could simply wrap all the individual words inside the caption into \mbox
es. This would cause line breaks to be inserted between words, not inside of them.
But of course, I don’t want to change my usage of \chapter
. That is, I do not want to write \chapter{\mbox{Some} \mbox{chapter}}
. Instead, I wanted to handle this by redefining \chapter
(well, using [explicit]titlesec
actually).
My idea was to create a list parser using etoolbox
that separates the words on whitespace and wraps the words in mboxes. Conceptually, this is extremely simple:
\newcommand*\mboxed[1]{%
\let\do\mbox
\DeclareListParser{\dospacelist}{ }
\dospacelist#1}
\titleformat{\chapter}
{\sffamily\Huge}
{\thechapter\ }
{0pt}
{\mboxed{#1}}
Not very surprisingly, this doesn’t work. I also tried changing the catcode of space to 11 in the mboxed
command inside a group but this didn’t work either.
Is it even possible to write the mboxed
macro in (La)TeX? How should this look like?
Increased difficulty (for extra points): The captions can also contain macros themselves, for example, one of my chapters is defined as
\chapter{{\texorpdfstring{\cpp}{C++}}
… because the \cpp
command, which us a pretty-printed version of the string “C++”, doesn’t work in PDF labels.