为什么 titlesec 会导致 \paragraph 内出现奇怪的换行符?

为什么 titlesec 会导致 \paragraph 内出现奇怪的换行符?

请考虑以下示例:

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

编译后的结果如你所料:

文档有合理的换行符

但是取消注释\usepackage{titlesec},各种奇怪的事情就会发生:

文档中有奇怪的换行符

在第一段中,现在有一个溢出的水平盒子,因为权衡现在拒绝在连字符处拆分。在第二段中,连字符被替换为短划线!而在第三段中,行在 LaTeX 中通常是非法的,因为 LaTeX 通常不会在已经连字符的单词中插入额外的断点

这是怎么回事?是什么导致自动连字符行为如此奇怪?这可能只是 titlesec 中的一个需要修复的错误,我会通过电子邮件通知软件包创建者此行为。但即使这是一个错误,我有没有办法在它被修复之前解决它?

答案1

包裹将标题装箱和拆箱,大概是为了测量一些东西。可能它不应该这样做,而是扔掉盒子并重新设置原始标记(尽管设置两次也会产生不良影响)。但拆箱列表与直接设置原始标记不同,请比较:

在此处输入图片描述

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

\begin{document}

aaaaaaaaaaaaaaaaaaaaaaaaaaaa  self-evident

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

\end{document}

您可以使用保留连字符惩罚的\sbox版本来替换使用:\Sbox


Javier 的版本 3,支持强制换行:

在此处输入图片描述

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

版本 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}

原来的解决方法是强制-像显式连字符一样操作,即使在取消装箱时也是如此:

在此处输入图片描述

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

相关内容