How do I get LaTeX to hyphenate a word that contains a dash?
Asked Answered
P

10

133

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?

Psychodynamics answered 3/2, 2010 at 16:2 Comment(8)
Why is it a problem for the word to be broken at the hard hypen? Isn't "multi-\newline dimensional" ok?Assignment
Bonus answer: A google search of "latex hyphenation" yielded all kinds of useful results, including the latex wiki book which has the answer: en.wikibooks.org/wiki/LaTeX/Formatting#HyphenationAdorable
@mica: no it does not. I've read all those pages years ago. @geoff: it would be ok, indeed, if latex did hyphen there. but in this example it would cause an "underfull hbox", and latex chooses not to hyphenate at all.Psychodynamics
can you backslash the first - in the \hyphenation command?Rois
See also tex.stackexchange.com/q/2706/1340Gomes
This question appears to be off-topic because it is about Latex, and should be migrated to tex.stackexchange.comSubtonic
Thanks for your comment @DrewSteen. Obviously I agree. But in my defence there was no such thing as tex.SX back when I asked this questions :-)Psychodynamics
Oh, I totally understand. This is actually an automated comment related to stackexchange's migration system.Subtonic
B
58

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).

Backbreaking answered 3/2, 2010 at 16:17 Comment(1)
Thanks a lot. As I suspected, there is no "perfect" answer. Because I'm not the only author in that document, I don't really want to force the others to use a \newcommand everywhere (that's why I was seeking something based on \hyphenation). I guess I'll just keep it ''as is'' by default, and add explicit hyphens manually when Latex complains about overfull hboxes.Psychodynamics
C
80

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.)

Cowherd answered 27/12, 2011 at 20:48 Comment(2)
This is great, but is yields a hyperref warning when I use it in a chapter string: Package hyperref Warning: Token not allowed in a PDF string. There, the other proposed solution (\def\hyph{-\penalty0\hskip0pt\relax}) works.Pros
@Jan-PhilipGehrcke: those warnings can also be fixed using \texorpdfstring (which provides conditional compilation for strings that go into chapter titles). I hide uses inside semantic macros (here, probably \multidisciplinary). Yes, it's not perfect.Gomes
B
58

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).

Backbreaking answered 3/2, 2010 at 16:17 Comment(1)
Thanks a lot. As I suspected, there is no "perfect" answer. Because I'm not the only author in that document, I don't really want to force the others to use a \newcommand everywhere (that's why I was seeking something based on \hyphenation). I guess I'll just keep it ''as is'' by default, and add explicit hyphens manually when Latex complains about overfull hboxes.Psychodynamics
T
28

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.

Trachoma answered 15/12, 2010 at 20:11 Comment(0)
V
6

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.

Vorous answered 17/9, 2015 at 15:57 Comment(1)
Replacing - with -\- doesn't change the output, at least with the default settings in overleaf.com (probably pdflatex, but overleaf doesn't actually say).Andreeandrei
Z
5

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
Zenaidazenana answered 8/8, 2011 at 11:5 Comment(2)
Sorry but look like some error raise on macro /futureletKythera
@PeterlitsZo Sorry, this code is from more than 10 years ago. It has been a long time since I have used LaTeX, so by now this code has become mostly incomprehensible to me. It used to work for me back at the time. I've attached the last version that my code grew into, maybe that helps.Zenaidazenana
P
5
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.

Pozzuoli answered 10/11, 2011 at 5:56 Comment(1)
Should be multi-\hskip0pt disciplinary. Like in you MWE, it will hypenate too much. Just set \setlength{\textwidth}{0.1cm} in order to try that.Spiegel
G
1

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.

Giustino answered 17/11, 2022 at 9:29 Comment(0)
P
-1

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.

Porta answered 27/2, 2022 at 17:4 Comment(1)
Isn't this basically the same as https://mcmap.net/q/172392/-how-do-i-get-latex-to-hyphenate-a-word-that-contains-a-dash ?Holton
A
-2

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

Adorable answered 3/2, 2010 at 16:31 Comment(1)
He is asking how to avoid hyphenating, not about how to define limits.Pentagram
S
-3

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!

Saltatorial answered 12/5, 2010 at 22:32 Comment(1)
This is answering the opposite question of what was asked. Also, as the person who tried to edit your post to point that out said, there's a much better way to do what you did: \mbox{3-D}.Lahr

© 2022 - 2024 — McMap. All rights reserved.