【问题标题】:How do I get LaTeX to hyphenate a word that contains a dash?如何让 LaTeX 连字符包含破折号的单词?
【发布时间】:2010-02-03 16:02:57
【问题描述】:

在我正在编写的 LaTeX 文档中,我收到一个 overfull hbox 警告,因为“多学科”一词恰好出现在一行的末尾。

我可以通过将其更改为multi-discipli\-nary 来消除这个特殊警告,但同样的问题也会在其他地方发生,因为这个词在论文中被大量使用。

我想改用\hyphenation{} 命令,但显然我试探性的\hyphenation{multi-disci-pli-na-ry} 不起作用,因为它不能正确理解第一个破折号。

我需要什么咒语才能在已经包含破折号的单词中获得正确的缩进?

额外问题:我自己在哪里可以找到该问题的答案?

【问题讨论】:

  • 为什么单词在硬连字符处被打破是个问题? “多\换行维度”不行吗?
  • 奖励答案:“latex hyphenation”的谷歌搜索产生了各种有用的结果,包括有答案的乳胶维基书:en.wikibooks.org/wiki/LaTeX/Formatting#Hyphenation
  • @mica:不,它没有。几年前我已经阅读了所有这些页面。 @geoff:确实,如果乳胶在那里使用连字符就可以了。但在本例中,它会导致“hbox 不足”,并且 Latex 选择根本不使用连字符。
  • 这个问题似乎离题了,因为它是关于乳胶的,应该迁移到 tex.stackexchange.com
  • 感谢您的评论@DrewSteen。显然我同意。但在我的辩护中,当我问这个问题时,并没有 tex.SX 这样的东西:-)

标签: latex hyphenation


【解决方案1】:

问题(正如 KennyTM 所指出的)是 LaTeX 不会将带有破折号的单词连字符连接起来。幸运的是,有一个标准包(ncctools 的一部分)可以解决这个问题,称为extdash。这定义了新的连字符和破折号命令,它们不会中断连字符,并且可以允许或防止在连字符/破折号处换行。我更喜欢将它与shortcuts 选项一起使用,因此我可以使用\-/ 而不是\Hyphdash。这就是你想要的:

\usepackage[shortcuts]{extdash} ... multi\-/disciplinary

为防止在该连字符处中断,请使用multi\=/disciplinary

(除此之外:The Chicago Manual of Style 建议删除附加诸如“multi”之类的词缀的连字符,除非这个词没有它是模棱两可或难以理解的。)

【讨论】:

  • 这很好,但是当我在章节字符串中使用它时会产生超引用警告:Package hyperref Warning: Token not allowed in a PDF string。在那里,另一个建议的解决方案 (\def\hyph{-\penalty0\hskip0pt\relax}) 有效。
  • @Jan-PhilipGehrcke:这些警告也可以使用\texorpdfstring(它为进入章节标题的字符串提供条件编译)修复。我将使用隐藏在语义宏中(这里可能是\multidisciplinary)。是的,它并不完美。
【解决方案2】:

来自https://texfaq.org/FAQ-nohyph

TeX 不会对已经连字符的单词进行连字符。为了 例如,(漫画)英国姓氏 Smyth-Postlethwaite 不会 连字符,这可能很麻烦。这是正确的英语 排版风格(对于其他语言可能不正确),但如果 需要必须,您可以将名称中的连字符替换为\hyph 命令,定义

 \def\hyph{-\penalty0\hskip0pt\relax}

不是这个常见问题解答通常会推荐的那种东西...... hyphenat 包定义了一组这样的命令(例如 在各种标点符号处引入连字符)。


或者您可以将 \newcommand 扩展为 multi-discipli\-nary 的命令(使用搜索 + 全部替换来替换现有单词)。

【讨论】:

  • 非常感谢。正如我所怀疑的,没有“完美”的答案。因为我不是该文档中的唯一作者,所以我真的不想强迫其他人在任何地方都使用\newcommand(这就是为什么我正在寻找基于\hyphenation 的东西)。我想我会默认保持“原样”,并在 Latex 抱怨 hbox 过满时手动添加显式连字符。
【解决方案3】:

我使用包hyphenat,然后将复合词如芬兰词Internet-yhteys(英语。Internet 连接)写为Internet\hyp yhteys。看起来很傻,但似乎是我找到的最优雅的方式。

【讨论】:

    【解决方案4】:

    multi-disciplinary 不会被连字符,正如 kennytm 所解释的那样。但是multi-\-disciplinary 具有与multidisciplinary 相同的断字机会。

    我承认我不知道为什么会这样。它与here 描述的行为不同(强调我的):

    命令\- 在单词中插入一个任意连字符。 这也成为该单词中唯一允许连字的地方

    【讨论】:

    • -\- 替换- 不会改变输出,至少在overleaf.com 中的默认设置下(可能是pdflatex,但overleaf 实际上并没有说)。
    【解决方案5】:
    multi\hskip0pt-\hskip0pt disciplinary
    

    你可以例如定义喜欢

    \def\:{\hskip0pt}
    

    然后写

    multi\:-\:disciplinary
    

    请注意,babel 俄语语言包有自己的一组破折号,不禁止连字符,例如"~(双引号+波浪号)。

    【讨论】:

    • 应该是multi-\hskip0pt disciplinary。就像你的 MWE 一样,它会过度宣传。只需设置\setlength{\textwidth}{0.1cm} 即可尝试。
    【解决方案6】:

    我遇到了同样的问题。我使用hyphenat 加上以下宏:

    \RequirePackage{hyphenat}
    \RequirePackage{expl3}
    
    
    % The following defs make sure words that contain an explicit `-` (hyphen) are still hyphenated the normal way, and double- and triple hyphens keep working the way they should. Just don't use a `-` as the last token of your document. Also note that `-` is now a macro that is not fully expandable
    
    \ExplSyntaxOn
    
    % latex2e doesn't like commands starting with 'end', apparently expl3 doesn't have any problems with it
    \cs_new:Npn \hyphenfix_emdash:c {---}
    \cs_new:Npn \hyphenfix_endash:c {--}
    
    \cs_new:Npn \hyphenfix_discardnext:NN #1#2{#1}
    
    
    \catcode`\-=\active
    
    \cs_new_protected:Npn -{
        \futurelet\hyphenfix_nexttok\hyphenfix_i:w
    }
    
    \cs_new:Npn \hyphenfix_i:w {
        \cs_if_eq:NNTF{\hyphenfix_nexttok}{-}{
            %discard the next `-` token
            \hyphenfix_discardnext:NN{\futurelet\hyphenfix_nexttok\hyphenfix_ii:w}
        }{
            % from package hyphenat
            \hyp
        }
    }
    
    \cs_new:Npn \hyphenfix_ii:w {
        \cs_if_eq:NNTF{\hyphenfix_nexttok}{-}{
            \hyphenfix_discardnext:NN{\hyphenfix_emdash:c}
        }{
            \hyphenfix_endash:c
        }
    }
    
    
    \ExplSyntaxOff
    

    请注意,这使用了来自 latex3 的 expl3 包。

    它使- 成为一个活动字符,向前扫描以查看它后面是否有更多破折号。如果是这样,它保持-,以确保----- 继续工作。如果不是,它将变成来自连字符的\hyp 命令,从而在单词的其余部分启用分词。这是一个通用的解决方案,它使包含显式连字符的所有单词都正常连字符。

    注意- 变成了一个不能完全扩展的宏,所以在加载其他可能不期望- 成为宏的包之后尝试包含它

    编辑:这是我的第二个版本,当{} 后跟一个连字符时,第一个版本不太健壮。这个不是,但与第一个版本不同的是,此版本中的- 不是完全可扩展的。

    编辑 2: 我用于修复此问题的模块最终变成了以下内容。由于我不再使用 Latex,而且我在 10 多年前写了这篇文章,我不知道以下内容是否仍然有效。警告购买者!

    \RequirePackage{hyphenat}
    \RequirePackage{expl3}
    
    
    % The following defs make sure words that contain an explicit `-` (hyphen) are still hyphenated the normal way, and double- and triple hyphens keep working the way they should. Just don't use a `-` as the last token of your document. Also note that `-` is now a macro that is not fully expandable
    
    % The original hyphen is available as the \hp command.
    
    \ExplSyntaxOn
    
    \cs_new:Npn \hp {-}
    
    % make hyphen the normal character
    \cs_new:Npn \hyphenfixdisabled {
      \catcode`\-=12\relax
    }
    
    
    \cs_new:Npn \hyphenfix_emdash:c {---}
    \cs_new:Npn \hyphenfix_endash:c {--}
    
    \cs_new:Npn \hyphenfix_discardnext:NN #1#2{#1}
    
    \cs_new:Npn \hyphenfix_ignore:c {-}
    
    
    \catcode`\-=\active
    
    
    %Making hyphen an active character throughout a document can lead to unexpected errors, especially if it is being edited by multiple persons. This note command at the beginning of what will be the meaning of `-' will hopefully help diagnose errors resulting from hyphen behaving unexpectedly.
    \catcode`\!=11
    \catcode`\.=11
    
    \let \Note:hyphen_is_an_active_character!_see_hyphenfix.tex! \relax
    
    \cs_new_protected:Npn \hyphenfix_fixhyphen:w{
        \if_mode_math:
            \hp
        \else: \use_i_after_fi:nw {
            \Note:hyphen_is_an_active_character!_see_hyphenfix.tex!
            \futurelet\hyphenfix_nexttok\hyphenfix_i:w
            }
        \fi:
    }
    \catcode`\!=12
    \catcode`\.=12
    
    \cs_new:Npn \hyphenfix_i:w {
        \cs_if_eq:NNTF{\hyphenfix_nexttok}{-}{
            %discard the next `-` token
            \hyphenfix_discardnext:NN{\futurelet\hyphenfix_nexttok\hyphenfix_ii:w}
        }{
            % from package hyphenat
            \hyp
        }
    }
    
    \cs_new:Npn \hyphenfix_ii:w {
        \cs_if_eq:NNTF{\hyphenfix_nexttok}{-}{
            \hyphenfix_discardnext:NN{\hyphenfix_emdash:c}
        }{
            \hyphenfix_endash:c
        }
    }
    
    \cs_new:Npn \hyphenfixenable {
      \catcode`\-=\active
      \let-\hyphenfix_fixhyphen:w
    }
    \cs_new:Npn \hyphenfixdisable {
      \let-\hyphenfix_ignore:c
      \catcode`\-=12\relax
    }
    
    \catcode`\-=12\relax
    
    \ExplSyntaxOff
    

    【讨论】:

    • 抱歉,宏 /futurelet 上似乎出现了一些错误
    • @PeterlitsZo 抱歉,这段代码来自 10 多年前。我已经很久没有使用 LaTeX 了,所以到目前为止,这段代码对我来说已经变得难以理解了。当时它曾经对我有用。我附上了我的代码生成的最后一个版本,也许这会有所帮助。
    【解决方案7】:

    由于 Latex 认为多学科是一个带有首选连字符的单词,您可以指出这是两个单独的单词,例如: 多\hspace{0pt}学科足以解决此问题。

    【讨论】:

    【解决方案8】:

    我在这里回答了类似的问题:LaTeX breaking up too many words

    我说:

    您应该在序言中的某处设置断字惩罚:

    \hyphenpenalty=750
    

    750 的值适合我在信纸(8.5x11 英寸)上使用 12 pt 字体进行两列布局的需要。调整值以满足您的需要。数字越大,出现的断字越少。您可能还想看看 hyphenatpackage,它提供的不仅仅是断字惩罚

    【讨论】:

    • 他问的是如何避免断字,而不是如何定义限制。
    【解决方案9】:

    为了避免在已经连字符的单词中出现连字符,我将不间断空格 ~ 与向后空格 \! 结合使用。例如,命令

    3~\!\!\!\!-~\!\!\!D
    

    在文本中使用,抑制单词 3-D 中的连字符。可能不是最好的解决方案,但它对我有用!

    【讨论】:

    • 这是对所问内容的相反问题的回答。此外,正如试图编辑您的帖子以指出这一点的人所说,有一种更好的方法来做您所做的事情:\mbox{3-D}
    猜你喜欢
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 2019-08-31
    • 2021-10-11
    相关资源
    最近更新 更多