我有同样的问题。我使用连字符加上以下宏:
\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
连字符的命令,在单词的其余部分启用单词中断。这是一个通用的解决方案,它使所有包含显式连字符的单词正常连字符。
请注意,它会变成一个不能完全扩展的宏,因此请在加载其他可能不会是宏的-
包后尝试包含它-
编辑:这是我的第二个版本,第一个版本在 a{
或}
连字符后不太健壮。这个不是,但与第一个版本不同的是-
,这个版本不是完全可扩展的。
编辑 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