如果单词中有连字符,则用连字符右移

如果单词中有连字符,则用连字符右移

标准 \raggedright 完全阻止任何连字符。ragged2e通过操作右侧文件或类似方法允许所有连字符。有没有办法设置 raggedright,但其中已经有连字符的单词可以被拆分,而普通单词根本不会连字符?

答案1

TeX 会在显式连字符处对换行符收取参数当前值的惩罚\exhyphenpenalty,而“隐式”连字符(TeX 在连字符期间自动插入的连字符)则会收取由不同参数的当前值给出的惩罚\hyphenpenalty。因此,您需要做的就是定义一个\raggedright声明的变体,将前一个参数清零,但不将后一个参数清零:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{lipsum}

\newcommand*{\YZraggedright}{%
    \raggedright
    \exhyphenpenalty 0 % space is intentional
}
\newenvironment*{YZflushleft}{\trivlist \YZraggedright \item\relax}{}



\begin{document}

In the normal setting, \( \verb|\hyphenpenalty| = \number\hyphenpenalty \) and  
\( \verb|\exhyphenpenalty| = \number\exhyphenpenalty \).

\begin{flushleft}
    Inside this environment,
    \( \verb|\hyphenpenalty| = \number\hyphenpenalty \) and
    \( \verb|\exhyphenpenalty| = \number\exhyphenpenalty \).

    This means that \TeX\ will never choose a line break at a word containing an
    explicit hyphen, like ``mother-in-law'', as we can see here:
    moooootheeeer-iiin-laaaww and faaaaatheeeeer-iiiiiin-laaaaaaawww (the break 
    will appeal to \TeX\ only if it makes the paragraph at least one line 
    shorter, see \verb|\linepenalty|).
\end{flushleft}

\begin{YZflushleft}
    Inside this environment,
    \( \verb|\hyphenpenalty| = \number\hyphenpenalty \) and
    \( \verb|\exhyphenpenalty| = \number\exhyphenpenalty \).

    This means that, this time, \TeX\ \emph{can} choose a line break at a word
    containing an explicit hyphen, like ``mother-in-law'', as we can see here:
    moooootheeeer-iiin-laaaww and faaaaatheeeeer-iiiiiin-laaaaaaawww (the break
    will appeal to \TeX\ only if it makes the paragraph at least one line
    shorter, see \verb|\linepenalty|).
\end{YZflushleft}

\end{document}

答案2

标准\raggedright设置不会禁止在明确的连字符处断行:

\documentclass{article}

% for showing what happens
\usepackage{showframe}
\setlength{\textwidth}{4cm}

\begin{document}

\begin{flushleft}
aaabbb abcdef-ghijklmno-pqr-stuv
aaa gggggg abcdef-ghijklmno-pqr-stuv
aaabbb abcdef-ghijklmno-pqr-stuv
aaa ggggg abcdef-ghijklmno-pqr-stuv
aaabbb abcdef-ghijklmno-pqr-stuv
aaa ggggg abcdef-ghijklmno-pqr-stuv
aaabbb abcdef-ghijklmno-pqr-stuv
\end{flushleft}

\end{document}

在此处输入图片描述

实际情况是,在 TeX 看来,在连字符处断行比在空格处断行更不理想。结果是,显式连字符之前的短片段可能会被推到下一行,而不是允许在连字符处断行。您可以这样做

\newenvironment{hflushleft}[1][\exhyphenpenalty]
  {\flushleft\exhyphenpenalty=#1 }
  {\endflushleft}

因此在hflushleft环境中,可以更改显式连字符处断行的惩罚。如果你这样做

\begin{hflushleft}
<text>
\end{hflushleft}

行为将与标准相同flushleft

\begin{hflushleft}[0]
<text>
\end{hflushleft}

甚至

\begin{hflushleft}[-50]
<text>
\end{hflushleft}

使在连字符处断开“更加可取”。尝试使用这些值,然后决定要向 中添加什么\raggedright,您可以通过以下方式执行

\usepackage{etoolbox}

\preto{\raggedright}{\exhyphenpenalty=<value>}

例如

\preto{\raggedright}{\exhyphenpenalty=0}

相关内容