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 \mboxes. 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:

  \DeclareListParser{\dospacelist}{ }

  {\thechapter\ }

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


… because the \cpp command, which us a pretty-printed version of the string “C++”, doesn’t work in PDF labels.


Based on the \zap@space macro from source2e. It uses \fbox instead of \hbox for demonstration purposes.


  {\thechapter\ }

    \@mboxed#1 \@empty
\def\@mboxed#1 #2{%
   \fbox{#1}\space  % fbox here to have a visual test
   \ifx #2\@empty\else
%    \orig@chapter[#1]{\mboxed{#2}}%


\chapter{The Test of the code}

\mboxed{This should be tested}

\chapter{I like to thank you for this easy question}

\chapter{Also works with macros like \texorpdfstring{\cpp}{C++} very well}

\chapter{More difficult stuff should be wrapped in double braces {{A\empty{} B}} to hide the spaces!}

  • Nice. One thing: If the argument starts with a space (or multiple spaces) or is empty, an empty box is created in the beginning (as in \fbox{}, not \fbox{ }). Is there a way to prevent this? I guess one could just check if #1 is empty before doing anything, but I was wondering if there is a "better" way. I did some testing, and I'm actually a little surprised that this doesn't prevent ligatures and kerning from working, given that the first letter of each word is stripped of (as #2) and reinserted. – Villemoes Mar 3 '11 at 6:59
  • @Villemoes: In \@mboxed#1 #2 the #1 will never match a space. So it is the easiest way to test it it empty, e.g. using \ifx\@nnil#1\@nnil\else \fbox{#1} \fi. Note that ligatures and kerning do their work later, after the expansion is complete and are never impacted by such operations. – Martin Scharrer Mar 3 '11 at 7:08
  • Say we have something like \def\ptest{Hello there, some words here.} and try to use \mboxed{This should be tested \ptest}; the \ptest part will be \fbox'd as a whole, not per individual words. Is it possible to somehow expand the \ptest macro, so that \mboxed would frame each word individually? – sdaau Aug 28 '11 at 22:53

Based on Caramdir’s comment mentioning the TeX FAQ, the following solves my particular problem:

  {\thechapter\ }

I’d still be interested in a solution to the space-separated list parsing, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.