# Why does titlesec cause bizarre line breaks within \paragraph?

Consider the following example:

\documentclass[12pt,a6paper]{article}
%\usepackage{titlesec}
\usepackage[textwidth=79mm]{geometry}

\begin{document}

\paragraph{I guess we'll have to make a few trade-offs.} That's really the
truth of the matter, isn't it? It's just a reality we'll have to face.

\paragraph{I have to say, that's a very twentieth-century mindset.} Frankly,
you need to update your way of thinking and stop living in the past.

\paragraph{We hold these truths to be self-evident.} That all men are
created equal, that they are endowed by their creator with certain
unalienable rights, that among these are life, liberty, and the pursuit of
happiness.

\end{document}


Compiling this yields what you would expect:

But uncomment \usepackage{titlesec}, and all kinds of weirdness happens:

In the first paragraph, there is now an overfull hbox, because trade-offs now refuses to be split at the hyphen. In the second paragraph, the hyphen is replaced by an en dash! And in the third, the line is broken at a point that would normally be illegal in LaTeX, because LaTeX normally won't insert an additional break point in an already hyphenated word.

What's going on here? What's causing the automatic hyphenation to behave so strangely? This may simply be a bug in titlesec that needs to be fixed, and I'll e-mail the package creator to notify him of this behavior. But even if it is a bug, is there a way I can work around it until it's fixed?

• Try to use trade\-offs. – Sigur Oct 22 '13 at 0:44
• With trade\-offs or just tradeoffs the line will be broken correctly. It's willing to break at a discretionary hyphen, just not an actual hyphen. – Jeffrey Barnes Oct 22 '13 at 0:47

The package boxes and unboxes the title, presumably to measure something. Possibly it shouldn't, and instead throw away the box and re-set the original tokens (although setting things twice can have bad effects too). But an unboxed list is not the same as directly setting the original tokens, compare:

\documentclass[12pt,a6paper]{article}
\usepackage{titlesec}
\usepackage[textwidth=79mm]{geometry}

\begin{document}

aaaaaaaaaaaaaaaaaaaaaaaaaaaa  self-evident

aaaaaaaaaaaaaaaaaaaaaaaaaaaa  \setbox0\hbox{self-evident}\unhbox0

\end{document}


You can replace the use of \sbox by a version \Sbox that preserves the hyphenation penalties:

Version 3 for Javier, supporting forced line breaks:

\documentclass[12pt,a6paper]{article}
\usepackage{titlesec}
\usepackage[textwidth=79mm]{geometry}

\makeatletter

\def\Sbox#1#2{%
\setbox\z@\vbox{\hsize\maxdimen%
#2\par
\global\setbox#1\box\voidb@x
\loop
\setbox\z@\lastbox
\global\setbox#1\hbox{%
\ifvoid#1\else\unhbox#1\hfill\break\fi
\unhbox\z@
\unskip\unskip\unpenalty}%
\unskip\unskip\unpenalty
\ifnum\lastnodetype=\@ne
\repeat
}}

%
\def\ttlh@runin#1#2#3#4#5#6#7#8{%
\global\@noskipsectrue
\gdef\ttl@makeline##1{##1}%
\ttl@changecentercr
#1{\ifhmode\ttl@hmode@error\fi
\Sbox%\global\sbox
\ttl@box{%
\ttl@calc\hspace{#6}%
\ifttl@label{\strut#2}\ttl@calc\hspace{#3}\fi
#4{#8}#5\unskip}}%
\gdef\@svsechd{\unhbox\ttl@box}}

\makeatother
\begin{document}

\paragraph{I guess we'll have to make a few trade-offs.} That's really the
truth of the matter, isn't it? It's just a reality we'll have to face.

\paragraph{I have to say, that's a very\\twentieth-century mindset.} Frankly,
you need to update your way of thinking and stop living in the past.

\paragraph{We hold these truths to be self-evident.} That all men are
created equal, that they are endowed by their creator with certain
unalienable rights, that among these are life, liberty, and the pursuit of
happiness.

\end{document}


version 2:

\documentclass[12pt,a6paper]{article}
\usepackage{titlesec}
\usepackage[textwidth=79mm]{geometry}

\makeatletter

\def\Sbox#1#2{%
\setbox\z@\vbox{\hsize\maxdimen%
#2\par
\global\setbox#1\lastbox
\global\setbox#1\hbox{\unhbox#1\unskip\unskip\unpenalty}}}

%
\def\ttlh@runin#1#2#3#4#5#6#7#8{%
\global\@noskipsectrue
\gdef\ttl@makeline##1{##1}%
\ttl@changecentercr
#1{\ifhmode\ttl@hmode@error\fi
\Sbox%\global\sbox
\ttl@box{%
\ttl@calc\hspace{#6}%
\ifttl@label{\strut#2}\ttl@calc\hspace{#3}\fi
#4{#8}#5\unskip}}%
\gdef\@svsechd{\unhbox\ttl@box}}

\makeatother
\begin{document}

\paragraph{I guess we'll have to make a few trade-offs.} That's really the
truth of the matter, isn't it? It's just a reality we'll have to face.

\paragraph{I have to say, that's a very twentieth-century mindset.} Frankly,
you need to update your way of thinking and stop living in the past.

\paragraph{We hold these truths to be self-evident.} That all men are
created equal, that they are endowed by their creator with certain
unalienable rights, that among these are life, liberty, and the pursuit of
happiness.

\end{document}


original workaround is to force - to act like an explicit hyphen even when unboxed:

\documentclass[12pt,a6paper]{article}
\usepackage{titlesec}
\usepackage[textwidth=79mm]{geometry}

\begin{document}

\paragraph{I guess we'll have to make a few trade\mbox{-}\penalty\exhyphenpenalty offs.} That's really the
truth of the matter, isn't it? It's just a reality we'll have to face.

\paragraph{I have to say, that's a very twentieth\mbox{-}\penalty\exhyphenpenalty century mindset.} Frankly,
you need to update your way of thinking and stop living in the past.

\paragraph{We hold these truths to be self\mbox{-}\penalty\exhyphenpenalty evident.} That all men are
created equal, that they are endowed by their creator with certain
unalienable rights, that among these are life, liberty, and the pursuit of
happiness.

\end{document}

• updated with workaround – David Carlisle Oct 22 '13 at 1:43
• Thanks very much! Your workaround was exactly the kind of thing I was hoping for. – Jeffrey Barnes Oct 22 '13 at 2:13
• Instead of \penalty\exhyphenpenalty, one could instead use \discretionary{}{}{}. What's the difference? The former uses the current value of \exhyphenpenalty while the latter uses the value in force when the paragraph ends. Thus, it behaves more like a regular hyphen. Also, it's easier to type. – Dan Oct 22 '13 at 21:18
• @Dan yes although a better idea is probably to replace the sbox in titlesec's definition by a boxing that preserves implict penalties. – David Carlisle Oct 22 '13 at 21:22
• @JavierBezos picking up each line would be better as the original wouldn't ignore the linebreak it would be ignored when measuring but then break the second time. I have several \lastbox loops for picking out every linefrom a vbox on this site so I could probably do it at the weekend if you haven't, let me know if you want me to look (no point us both doing it:-) – David Carlisle Feb 28 '14 at 17:15