我有一段文字(不超过 15 或 30 个字符),分成几个单词。我想对这一段文字添加惩罚,但我不想完全禁止它,以免出现真正糟糕的段落。我该怎么做?
I have some text and \DiscourageBreak{some text that \emph{shouldn't} be broken.}
我在看本网站提供的处罚清单,但我没看到一个合适的。
答案1
不够稳健,因为它不能作为另一个命令的参数;空格被激活并定义为发出高惩罚和空格。惩罚后面的空格不能作为换行点,只有惩罚可以,所以这不鼓励换行。您可能想禁止连字符,这可以通过添加\language=\l@nohyphenation
的定义轻松实现\DiscourageBreak
。
\documentclass{article}
\makeatletter
\newcommand{\DiscourageBreak}{%
\begingroup\obeyspaces\sean@penalizespace
\sean@discouragebreak
}
\newcommand{\sean@discouragebreak}[1]{#1\endgroup}
\newcommand{\sean@penalizespace}{%
\begingroup\lccode`\~=`\ %
\lowercase{\endgroup\def~}{\penalty9000 \space}%
}
\makeatother
\begin{document}
I have some text and some other text and again.
I have some text and \DiscourageBreak{some text that \emph{shouldn't} be broken.}
What about the breaks?
I have some text and some other text and again.
I have some text and some text that \emph{shouldn't} be broken.
What about the breaks?
\end{document}