12

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:

Document with reasonable line breaks

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

Document with weird line breaks

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?

1 Answers1

13

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:

enter image description here

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

enter image description here

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

enter image description here

\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}
David Carlisle
  • 757,742
  • updated with workaround – David Carlisle Oct 22 '13 at 01:43
  • Thanks very much! Your workaround was exactly the kind of thing I was hoping for. – Jeffrey Barnes Oct 22 '13 at 02: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
  • @Dan updated so you can just use - – David Carlisle Oct 22 '13 at 21:39
  • Sadly, it doesn't work if there are explicit penalties (part of the title vanishes). – Javier Bezos Feb 26 '14 at 15:04
  • @JavierBezos I don't suppose "I don't care as I got a tick and a badge anyway" is the correct response? I can't really remember what the issue is solving, if you edit a failing example into the end of my answer I'll look later – David Carlisle Feb 26 '14 at 15:32
  • @DavidCarlisle Currently, titlesec creates an hbox which is unpacked later. Well, this is problematic because of the optimization done by TeX in H mode with discretionaries. Your solution is to typeset the text in a very wide vbox and then get the last box inside it. It works if the paragraph is a single line, but with an explicit penalty (I mean a line break), we have more than one line, and the last box is just the last one. I'm now trying to find a solution working in both cases. – Javier Bezos Feb 28 '14 at 16:07
  • @JavierBezos presumably in the original hboxed version the forced linebreak is ignored? so you could ignore it by redefining \\ and similar to be space, or alternatively let it break but then collect up all the resulting boxes, I suppose. – David Carlisle Feb 28 '14 at 16:29
  • @DavidCarlisle Redefining \, \linebreak and the like is an option. Perhaps extending your macro somehow so that text is not discarded is another one. I'm not yet sure and I'm investigating. – Javier Bezos Feb 28 '14 at 16:50
  • 1
    @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
  • @DavidCarlisle An offer I can't refuse :-). Thanks, if you can, perfect. – Javier Bezos Feb 28 '14 at 17:47
  • @JavierBezos try now:-) – David Carlisle Feb 28 '14 at 19:53
  • @DavidCarlisle Thanks. I'll try, and if it works (I think it will) I'll fix the package. – Javier Bezos Mar 04 '14 at 18:11