# Avoid hyphenation in “2-D”

How can I avoid hyphenation in words such as "2-D"? \hyphenation{} is not working since the dash is not a letter.

-
this forum has another answer which works for me: \nobreakdash-\hspace{0pt}. Is this 'cleaner' than introducing a box? –  craq Nov 15 '13 at 16:25

Write it inside a box, like this: \mbox{2-D}

-
It's better to use \mbox and I have corrected the answer; try using \hbox{2-D} as the first object in a paragraph and you'll see why \mbox is preferable. –  egreg Jul 15 '11 at 9:00
To avoid the problem you mention I'd use $\hbox{2-D}$ instead. That way the solution works with plain TeX too. –  Mafra Oct 19 '12 at 13:08

To expand on Herbert's answer, here's a list of babel languages that support "~ for an unbreakable hyphen:

danish, dutch, german and ngerman, icelandic, norsk, swedish, bulgarian, slovak, russian and ukrainian.

-
Would there be a way to use the nonbreaking hyphen "~ in, say english? –  Seamus Jan 26 '11 at 14:48
@Seamus: Well, of course that's possible by making " active, but that might have other bad side effects. –  Hendrik Vogt Jan 26 '11 at 15:04
@Seamus: \addto\extrasenglish{\languageshorthands{ngerman}} –  Herbert Jan 26 '11 at 15:12
–  doncherry Jul 11 '12 at 14:44
\usepackage{xspace}
\newcommand\twoD{\mbox{2-D}\xspace}


\twoD and 2"~D ...: the latter with babel if the language supports "~

And an example with several languages and extended shorthands

\documentclass[french,english,ngerman]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\textwidth=5cm
\parindent=0pt

\begin{document}
\rule{\linewidth}{1pt}
\selectlanguage{english}

foo bar foo bar foo foobar and-another word

foo bar foo bar foo foobar and"~another word

\selectlanguage{french}
foo bar foo bar foo foobar and"~another word

\selectlanguage{english}\shorthandoff{"}
foo bar foo bar foo foobar and"~another word

\end{document}


-
@Herbert: I don't understand this answer. Are you saying that 3"~D works always, but 2"~D only with babel? I tried it, and only got the "~ working with german. –  Hendrik Vogt Jan 26 '11 at 10:20
@Hendrik: it is an example ... 7"~D, 1"~D,... I don't know what babel defines for every language. At least it works for ngerman/german ... –  Herbert Jan 26 '11 at 10:27
@Herbert: Still I think your answer is very confusing. Can you please make it clearer that you're suggesting two completely different solutions, and indicate what is needed to make them work? –  Hendrik Vogt Jan 26 '11 at 10:32
@Hendrik. it is not confusing ... –  Herbert Jan 26 '11 at 10:33
@Herbert: It took me 5 minutes to figure out what you mean and what is needed to make "~ work. And I only managed using the knowledge that you're German! –  Hendrik Vogt Jan 26 '11 at 10:36

With Xe(La)TeX, at least, you could also use the "non-breaking hyphen" (U+2011), given a font which contains the glyph.

-
Unfortunately not: XeTeX doesn't enforce Unicode properties such as non-breakable by default. There have been several discussions about characters like this on the XeTeX mailing-list, but none of them has lead to an improvement of the situation (for the moment). –  Arthur Reutenauer Jan 26 '11 at 12:23
@Arthur: should it not work anyway? U+2011 is not an <explicit hyphen> so unless there is an actual pattern for U+2011, there should not be any hyphenation –  Taco Hoekwater Jan 28 '11 at 9:21
Yes indeed, in that particular case; but I wanted to point out that you can't expect from XeTeX to behave the right way out of the box with these characters: you also run the risk that the font you use doesn't have that character, for example (it's apparently quite common for the companion character U+2010 -- but I only speak from hearsay here). –  Arthur Reutenauer Jan 28 '11 at 14:32
Why do I get flak for this? Like @Taco pointed out, there is no way 2‑D (you can copy that if you don't know how to produce it) is going to get hyphenated, because there are no patterns for 2<<unicode-char-for-non-breaking-hyphen>>d in any language! –  morbusg Jan 29 '11 at 22:03
@morbusg: I'm not sure why it was voted down. Arthur's comment about it not appearing in a font was a reasonable one, but this seems like a good answer to me. I've upvoted it. –  TH. Jan 30 '11 at 4:45

Maybe this general method help.

First, define a macro who insert a non-breaking hyphen after some (math) stuff, but allowed hyphenation in the word after :

\usepackage{xspace}
\newcommand{\tiret}{\nobreakdash-\hspace{0pt}}


Then define the macro twoD :

\newcommand{\twoD}{$$2$$\tiret D\xspace}


the text-mode version works too :

\newcommand{\twoD}{2\tiret D\xspace}


The advantage of this method is if you want a hyphen follow by a long word, by ex. 2-cyclotetrabenzene.

P.S. I apologize for my poor english

-

If your editor supports Unicode and the font used by it includes the non-breaking hyphen (this is, e.g., the case for Emacs under current Debian Linux) you can try the following:

Load the inputenc or inputenx package:

\usepackage[utf8]{inputenc}


Define a macro yielding a non-breaking hyphen, e.g.:

\newcommand*{\nobreakhyphen}{\mbox{-}}


or:

\newcommand*{\nobreakhyphen}{\nobreak\mbox{-}\nobreak\hskip0pt}


Bind the corresponding Unicode character to \nobreakhyphen:

\DeclareUnicodeCharacter{2011}{\nobreakhyphen}


Then, whenever you type the Unicode character U+2011 (using an adequate input method) you get a non-breaking hyphen, e.g.:

2‑D

-
See the answer from morbusg and its discussion. –  Martin Schröder Jul 25 '11 at 21:15
@mhp: I tried both forms of \nobreakhyphen shown above, and both worked. Cool. But then I tried the second form (the one with \nobreak) without the \mbox, and it didn't work anymore, which puzzled me. So what does the \nobreak actually add here? –  Todd Lehman Jan 30 '12 at 19:13

Using XeLaTeX, and if you can input your non-breaking hyphen (U+2011) directly, the following elegant solution is available (courtesy of the XeTeX archives, post by Roland Kuhn, 2010.11.01):

The non-breaking hyphen can also be made active and defined to yield “\hbox{-}” (the box prevents the discretionary after the ASCII hyphen from escaping, \nobreak does not help here):

\catcode‑=\active
\def‑{\hbox{-}}


You put the above two lines in your preamble, and then in your source you simply use the non-breaking hyphen as needed. Working like a charm here (TeXShop+MacTeX 2011), with big thanks to Roland.

Servus, J

-
An almost equivalent way is to load the newunicodechar package and say \newunicodechar{‑}{\mbox{-}}. It's actually a bit more robust. –  egreg Jul 11 '12 at 13:48
If by robust @egreg means the \mbox, then it's the same to stick a \long before the \def, and to have a \leavevmode before the \hbox (that is, if a par begins with a -; highly unlikely, but still). Although I'm not sure what good is the \long doing since the input is going into a \hbox. If you stick input starting with \vbox, it doesn't matter whether it's \long or not. –  morbusg Jul 11 '12 at 14:57
@morbusg No: the robustness I refer to would be obtained by prefixing \def with \protected. \long is relevant only for macros with arguments (and gives no robustness against expansions). –  egreg Jul 11 '12 at 15:04
@egreg ah, so it's the \newunicodechar, then. Thanks. –  morbusg Jul 11 '12 at 15:11