如何让LaTeX连接包含短划线的单词?

Gyo*_*yom 102 latex hyphenation

在一个LaTeX文档我写,我得到一个溢出的横向盒因为"多学科",它发生在一个行的末尾被渲染的警告.

我可以通过改变它来摆脱这个特殊的警告multi-discipli\-nary,但同样的问题将在其他地方发生,因为这个词在本文中被大量使用.

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

在一个已包含破折号的单词中,我需要使用什么咒语来获得正确的缩进?

奖金问题:我自己哪里可以找到这个问题的答案?

小智 59

问题(正如KennyTM所指出的那样)是LaTeX不会在其中用短划线连字.幸运的是,有一个标准的包(ncctools的一部分)可以解决这个问题extdash.这定义了新的连字符和短划线命令,它们不会中断连字符,并且可以允许或阻止连字符/短划线处的换行符.我更喜欢将它与shortcuts选项一起使用,所以我可以使用,例如,\-/而不是\Hyphdash.这是你想要的:

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

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

(旁白:芝加哥风格手册建议删除附加词缀的连字符,如'multi',除非没有它,这个词含糊不清或无法理解.)

  • @Jan-PhilipGehrcke:这些警告也可以使用“\texorpdfstring”(它为进入章节标题的字符串提供条件编译)来修复。我将用途隐藏在语义宏中(这里可能是“\multiciplin”)。是的,它并不完美。 (3认同)

ken*_*ytm 51

来自http://www.tex.ac.uk/cgi-bin/texfaq2html?label=nohyph:

TeX不会对已经被连字符的单词进行连字.例如,(漫画)英文姓Smyth-Postlethwaite不会连字符,这可能很麻烦.这是正确的英文排版样式(对于其他语言可能不正确),但如果需要,可以用\hyph命令替换名称中的连字符,定义

hyphenat

不是常见问题解答通常推荐的那种...... 连字符包定义了一组这样的命令(用于在各种标点字符处引入连字符点).


或者您可以\newcommand将命令扩展为multi-discipli\-nary(使用Search + Replace All替换现有单词).


Zou*_*pen 21

我使用包hyphenat然后写复合词,如芬兰语单词Internet-yhteys(Eng.Internet connection)as Internet\hyp yhteys.看起来很傻,但似乎是我找到的最优雅的方式.


rud*_*ker 6

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

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

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

  • 用 `-\-` 替换 `-` 不会改变输出,至少使用 overleaf.com 中的默认设置(可能是 pdflatex,但 overleaf 实际上没有说)。 (2认同)

Jan*_*nis 5

我有同样的问题。我使用连字符加上以下宏:

\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
Run Code Online (Sandbox Code Playgroud)

请注意,这使用了 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
Run Code Online (Sandbox Code Playgroud)


小智 5

multi\hskip0pt-\hskip0pt disciplinary
Run Code Online (Sandbox Code Playgroud)

你可以例如定义像

\def\:{\hskip0pt}
Run Code Online (Sandbox Code Playgroud)

然后写

multi\:-\:disciplinary
Run Code Online (Sandbox Code Playgroud)

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