Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

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

share|improve this question
4  
You could use any of the techniques described in tex.ac.uk/cgi-bin/texfaq2html?label=wdnohyph+hyphoff to prevent hyphenation. –  Caramdir Feb 20 '11 at 22:07
1  
Maybe your problem has already been solved here? (Probably not a duplicate since you want the space-separated list anyway.) –  Hendrik Vogt Feb 21 '11 at 7:00

2 Answers 2

up vote 7 down vote accepted

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

\documentclass{book}
\usepackage[explicit]{titlesec}
\usepackage{hyperref}
\def\cpp{C++}

\titleformat{\chapter}
  {\sffamily\Huge}
  {\thechapter\ }
  {0pt}
  {\mboxed{#1}}


\makeatletter
\def\mboxed#1{%
    \@mboxed#1 \@empty
}
\def\@mboxed#1 #2{%
   \fbox{#1}\space  % fbox here to have a visual test
   \ifx #2\@empty\else
    \expandafter\@mboxed
   \fi
   #2%
}
%\let\orig@chapter\chapter
%\renewcommand*{\chapter}[2][]{%
%    \orig@chapter[#1]{\mboxed{#2}}%
%}%
\makeatother


\begin{document}

\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!}

\end{document}
share|improve this answer
    
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:

\titleformat{\chapter}
  {\sffamily\Huge}
  {\thechapter\ }
  {0pt}
  {\begingroup%
     \parbox[b]{\textwidth}{%
       \hyphenpenalty=10000%
       \exhyphenpenalty=10000%
       #1}%
   \endgroup}

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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