这是一个最小但不工作的例子:
\documentclass{article}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\begin{document}
% some text
\parbox{\linewidth}{a short phrase, followed by another}
\parbox{\linewidth}{this is also a short one, but it is followed by this very long one which will need a line break, as I have designed it to need a line break, since I am trying to create a MnWE}
\end{document}
现在,这并不总是人们想要做的事情,但我想以某种方式给 LaTeX 一个提示,如果它需要换行,它应该优先在逗号后换行(一般来说,可能是其他符号),因此它会创建如下输出:
由于这不是人们总是喜欢做的事情,我们可能希望将我们希望以这种方式格式化的文本包装在一个环境中,该环境以应该用作换行提示的符号作为参数。
这怎么可能呢?
答案1
以下是一个概念证明:逗号的定义是产生一个标准逗号,后跟\linebreak[2]
:
\documentclass{article}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\newenvironment{specialcomma}
{\begingroup\lccode`~=`, \lowercase{\endgroup\def~}{\string,\linebreak[2]}%
\catcode`,=\active\par}
{\par}
\begin{document}
this is also a short one, but it is followed by this very
long one which, will need a line break, as I have designed
it to need a line break, since I am trying to create a MnWE
\begin{specialcomma}
this is also a short one, but it is followed by this very
long one which, will need a line break, as I have designed
it to need a line break, since I am trying to create a MnWE
\end{specialcomma}
\end{document}
答案2
您可以“建议”在任意位置进行换行(使用 弱换行\linebreak[1]
或使用 强换行\linebreak[3]
)或强制换行(\linebreak[4]
或简单地\linebreak
)。但是,如果您强制换行,单词之间的空格会更糟。当 不是强制命令时,通常会被忽略,因为这样。
\documentclass{article}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\def\extraword{xxxxxx}
\begin{document}
\parbox{\linewidth}{a short phrase,\linebreak[2] % this will be ignored
followed by another}
\parbox{\linewidth}{
this is also a short one, \linebreak % this work always, but you want this?
but it is followed by this long one which will need line break \extraword,
% \linebreak[1] will work with a \extraword as long as 'xxxxxx'.
% \linebreak[2] will work with a \extraword of 4-5 characters.
% \linebreak[3] will work with a \extraword of 1-3 characters (no 0).
% \linebreak[4] will work always (as without the optional argument)
\linebreak[1]
as I have designed it to need a line break,
\linebreak[3] % This will never be enough here ...
since I am trying to create a MnWE.}
\end{document}
此外,内容或格式的任何微小变化都会使此类修复变得无用或有害。我建议谨慎使用连字符,而使用microtype
逗号结尾的地方则不管。