如何让 LaTeX 对包含破折号的单词进行连字符?

发布于 2024-08-19 23:58:17 字数 360 浏览 11 评论 0原文

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

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

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

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

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

In a LaTeX document I'm writing, I get an overfull hbox warning because of the word "multi-disciplinary", which happens to be rendered at the end of a line.

I can get rid of this particular warning by changing it into multi-discipli\-nary, but the same problem will happen elsewhere, since this word is used a lot in the paper.

I'd like to use the \hyphenation{} command instead, but obviously my tentative \hyphenation{multi-disci-pli-na-ry} does not work, because it does not understand the first dash correctly.

What incantation do I need to get correct indentation in a word that already contains a dash?

Bonus question: Where could I have found the answer to that question myself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

负佳期 2024-08-26 23:58:17

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

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

为了防止连字符被破坏,请使用 multi\=/ciplient

(旁白:《芝加哥风格手册》 建议删除连接“multi”等词缀的连字符,除非如果没有连字符,该词会含糊不清或难以理解。)

The problem (as KennyTM noted) is that LaTeX won't hyphenate words with dashes in them. Luckily, there's a standard package (part of ncctools) that addresses that very problem, called extdash. This defines new hyphen and dash commands that do not disrupt hyphenation, and which can allow or prevent line breaks at the hyphen/dash. I prefer to use it with the shortcuts option, so I can use, e.g., \-/ rather than \Hyphdash. Here's what you want:

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

To prevent breaking at that hyphen, use multi\=/disciplinary

(Aside: The Chicago Manual of Style advises dropping the hyphens attaching affixes like 'multi', unless the word is ambiguous or unintelligible without it.)

━╋う一瞬間旳綻放 2024-08-26 23:58:17

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

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

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

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


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

From https://texfaq.org/FAQ-nohyph:

TeX won’t hyphenate a word that’s already been hyphenated. For
example, the (caricature) English surname Smyth-Postlethwaite wouldn’t
hyphenate, which could be troublesome. This is correct English
typesetting style (it may not be correct for other languages), but if
needs must, you can replace the hyphen in the name with a \hyph
command, defined

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

This is not the sort of thing this FAQ would ordinarily recommend…
The hyphenat package defines a bundle of such commands (for
introducing hyphenation points at various punctuation characters).


Or you could \newcommand a command that expands to multi-discipli\-nary (use Search + Replace All to replace existing words).

酒几许 2024-08-26 23:58:17

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

I use package hyphenat and then write compound words like Finnish word Internet-yhteys (Eng. Internet connection) as Internet\hyp yhteys. Looks goofy but seems to be the most elegant way I've found.

浅黛梨妆こ 2024-08-26 23:58:17

正如 kennytm 所解释的,多学科不会用连字符连接。但是,multi-\-subject 具有与 multilingual 相同的连字符机会。

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

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

multi-disciplinary will not be hyphenated, as explained by kennytm. But multi-\-disciplinary has the same hyphenation opportunities that multidisciplinary has.

I admit that I don't know why this works. It is different from the behaviour described here (emphasis mine):

The command \- inserts a discretionary hyphen into a word. This also becomes the only point where hyphenation is allowed in this word.

岁月打碎记忆 2024-08-26 23:58:17
multi\hskip0pt-\hskip0pt disciplinary

例如,您可以定义 like

\def\:{\hskip0pt}

,然后编写

multi\:-\:disciplinary

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

multi\hskip0pt-\hskip0pt disciplinary

You can e.g. define like

\def\:{\hskip0pt}

and then write

multi\:-\:disciplinary

Note that the babel Russian language package has its own set of dashes that do not prohibit hyphenation, "~ (double quotation+tilde) for example.

郁金香雨 2024-08-26 23:58:17

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

\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 命令,从而在单词的其余部分中启用断词。这是一个通用的解决方案,可以使所有包含显式连字符的单词正常连字符。

请注意,- 会成为不可完全扩展的宏,因此请在加载其他可能不希望 - 成为宏的包后尝试包含此宏

编辑:< /strong> 这是我的第二个版本,当 {} 后跟连字符时,第一个版本的稳健性较差。这个不是,但与第一个版本不同,此版本中的 - 不是完全可扩展的。

编辑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

I had the same problem. I use hyphenat plus the following macro:

\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

Note that this uses the expl3 package from latex3.

It makes the - an active character that scans forward to see if it is followed by more dashes. If so, it stays a -, to make sure -- and --- keep working. If not, it becomes the \hyp command from hyphenat, enabling word breaks in the rest of the word. This is a generic solution that makes all words that contain explicit hyphens hyphenate normally.

Note that - becomes a macro that is not fully expandable, so try to include this after loading other packages that may not expect - to be a macro

Edit: This is my second version, the first version was less robust when a { or } followed a hyphen. This one is not, but unlike the first version the - in this version is not fully expandable.

Edit 2: My module for fixing this ended up growing into the following. As I no longer use Latex and it was over 10 years ago that I wrote this, I have no idea if the following still works. Caveat emptor!

\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
木緿 2024-08-26 23:58:17

西里尔语的 babel 包定义了命令 "= ,应该使用它来代替破折号:所以你可以写 по"=своєму 并且它会正确地连字符该单词。

但是,对于英语,未定义命令 "=
您可以按如下方式定义它,

\useshorthands{"}
\defineshorthand{"=}{\penalty1000\discretionary{-}{}{-}\penalty10000\hskip0pt}

然后 pseudo"=metric 将被正确连字符。

实际上,这是这里的答案:
https://tex.stackexchange.com/questions/ 99334/用双三破折号连接单词连字符
建议使用组合 "|

使用 "= 的一个优点是,当您同时使用西里尔语和英语时,它现在可以在多语言文档中工作,并且与仅包含西里尔字母的文档兼容。

The babel package for Cyrillic defines the command "= which should be used instead of dash: so you can write по"=своєму and it will correctly hyphenate that word.

However, for English language that command "= is not defined.
You can define it as follows

\useshorthands{"}
\defineshorthand{"=}{\penalty1000\discretionary{-}{}{-}\penalty10000\hskip0pt}

Then pseudo"=metric will be correctly hyphenated.

Actually, this is the answer from here:
https://tex.stackexchange.com/questions/99334/hyphenation-of-words-connected-with-a-double-triple-dash
where it is propose to use the combination "|.

An advantage of usage "= is that it now works in multi-language documents when you have both Cyrillic and English, and is compatible with documents having Cyrillic only.

踏月而来 2024-08-26 23:58:17

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

Since Latex thinks multi-disciplinary is a single word with preferred hyphenation, you can indicate that these are two separate words,e.g.:
multi-\hspace{0pt}disciplinary is sufficient to resolve this.

岁吢 2024-08-26 23:58:17

我在这里回答了类似的问题: LaTeX 分解了太多单词

我说:

你应该在序言中的某处设置连字符惩罚:

\hyphenpenalty=750

750 的值适合我对 12 pt 字体的信纸(8.5x11 英寸)上的两列布局的需求。调整该值以满足您的需要。数字越大,连字符越少。您可能还想看看 hyphenatpackage,它提供的不仅仅是连字符惩罚

I answered something similar here: LaTeX breaking up too many words

I said:

you should set a hyphenation penalty somewhere in your preamble:

\hyphenpenalty=750

The value of 750 suited my needs for a two column layout on letter paper (8.5x11 in) with a 12 pt font. Adjust the value to suit your needs. The higher the number, the less hyphenation will occur. You may also want to have a look at the hyphenatpackage, it provides a bit more than just hyphenation penalty

燕归巢 2024-08-26 23:58:17

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

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

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

To avoid hyphenation in already hyphenated word I used non-breaking space ~ in combination with backward space \!. For example, command

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

used in the text, suppress hyphenation in word 3-D. Probably not the best solution, but it worked for me!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文