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 have several section titles in my document that are getting hyphenated, which is quite ugly.

I am now looking for some way to avoid the hyphenation, which is is somewhat related to this question, but unfortunately my titles have to be centered (so raggedright is not an option), and adding \sloppy to my titlesec definition of the subsection format didn't help:

\newcommand{\trailthesubsection}[1]{\MakeUppercase{#1} (\thesubsection)}
\titleformat{\subsection}
    {\normalfont\fontsize{15pt}{16pt}\bfseries\sffamily\sloppy} % <- "sloppy" didn't help.
    {}
    {0pt}
    {\filcenter\trailthesubsection}

I could, of course, set manual linebreaks - but in order not to destroy the layout of my TOC in the process, I would have to use the optional parameter to the section commands:

\subsection[Could do this but it is not nice]{Could do this\\ but it is not nice}

Isn't there some way to suppress hyphenation for a given text (e.g. by some command in the \titleformat), or to suppress manually-set linebreaks in the TOC?

share|improve this question
    
While the proper way to handle this is avoiding line breaks in the title, it's worth mentioning the package option newlinetospace, with does what the name suggests in headlines and tocs. –  Javier Bezos May 23 '12 at 12:30

2 Answers 2

up vote 6 down vote accepted

See this question in the UK TeX FAQ for a discussion of four different methods for turning off hyphenation. Method three and four consist of adding the instructions

\righthyphenmin62 %% or whatever's large enough ...

and

\hyphenchar\font=-1

respectively. The latter approach is used in the following MWE:

\documentclass{article}
\usepackage{titlesec}
\titleformat*{\section}{\sloppy\bfseries\Large\hyphenchar\font=-1}
\begin{document}
\section{Supercalifragilisticexpialidocious 
   Supercalifragilisticexpialidocious 
   Supercalifragilisticexpialidocious Supercalifragilisticexpialidocious 
   Supercalifragilisticexpialidocious Supercalifragilisticexpialidocious 
   Supercalifragilisticexpialidocious Supercalifragilisticexpialidocious }

\ldots with apologies to Julie Andrews.
\end{document}

Note that the \sloppy command will prevent TeX from creating (seriously!) overfull lines in the section title.

Note (thanks to egreg): the \hyphenchar\font-1 instruction is global, so all other text in the document set in \bfseries\Large will also see hyphenation suppressed -- which may (or may not) be what's desired. Therefore, the \righthyphenmin62 method may be marginally safer to use (pun intended). However, according the UK TeX FAQ's answer noted earlier, if you're a babel user the method of resetting the \left- and/or \righthyphenmin macros won't work as babel resets them internally.

Addendum: To prevent hyphenation in the optional shortened section title (to be displayed in the table of contents), one would have to insert the instructions

\protect{\hyphenchar\font=-1}\protect\sloppy

at the start of the material in the optional argument of the \section command. (My feeling is that if a section caption is longer than one line, it's probably wise to provide a shortened caption that's short enough not to need a line break at all.)

share|improve this answer
    
I read that part of the FAQ, but didn't come up with the idea to use \hyphenchar\font=-1 within the titleformat definition. (I dismissed it as "I don't want to disable it for the whole font / document".) Indeed a most elegant solution for a no-longer existing problem... –  DevSolar Sep 30 '11 at 10:08
    
You're welcome! I've inserted an addendum in my answer, in which I explain how one would suppress hyphenation of the section caption in the table of contents. –  Mico Sep 30 '11 at 10:36
1  
Remember, though, that \hyphenchar=-1 is a global declaration. So no other part of the document in \bfseries\Large will be hyphenated. I'd prefer \righthyphenmin=62, which is a local declaration and will do the same. –  egreg Sep 30 '11 at 10:50
    
Good point! I'll add this to the answer above. –  Mico Sep 30 '11 at 10:57

One way that work is

\newif\ifintoc
\DeclareRobustCommand{\titlebreak}{%
  \ifintoc
    \unskip\space
  \else
    \newline
  \fi}

Then do the Table of Contents by

\begingroup\intoctrue
\tableofcontents
\endgroup

and your title can be input as

\subsection{Could do this\titlebreak but it is not nice}

It's also possible to inject \intoctrue in the code for \tableofcontents.

LaTeX will write \titlebreak in the .toc file, but when this one is read in, \titlebreak will mean "leave a normal space". During normal processing of the document it will mean "break line here". The \unskip\space is to prevent a double space in

 \subsection{Could do this \titlebreak but it is not nice}

or no space in

 \subsection{Could do this\titlebreak but it is not nice}
share|improve this answer
1  
I am interest in a minimal working example that shows the effect. I am not able to create one with a hyphenated section. –  Marco Daniel Sep 30 '11 at 9:22
    
Much better already... I have this nagging memory that I read some other solution not too long ago but didn't really pay attention (or set a bookmark)... I keep the question open for a while to see if someone else has some even more elegant solution. –  DevSolar Sep 30 '11 at 9:24
    
@Marco: ...and there I was, going "hey d'oh, I have examples right here"... Then I found that I got things mixed up. I have hyphenated sections (but those are block-set and thus easily solved with `\raggedright), and hyphenated subsections where the hyphenated words are too long to fit the column in the first place (so it's not a matter of suppressing anything, but chosing shorter words)... so your legitimate request for a MWE just evaporated my problem. I'm ashamed... –  DevSolar Sep 30 '11 at 9:41

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.