50

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

doncherry
  • 54,637
ltxsun
  • 875

7 Answers7

36

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

N.N.
  • 36,163
Mafra
  • 1,615
18

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.

Hendrik Vogt
  • 37,935
7
\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}
\addto\extrasenglish{\languageshorthands{ngerman}}
\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}

enter image description here

  • @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 ... –  Jan 26 '11 at 10:27
  • 7
    @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 ... –  Jan 26 '11 at 10:33
  • 4
    @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
  • @Herbert: Maybe the upvotes to my comments convince you? – Hendrik Vogt Jan 26 '11 at 13:43
  • @Herbert: Thanks, now that makes a lot more sense. – Hendrik Vogt Jan 26 '11 at 14:15
  • @Herbert: Thanks also for the example code! Now I see how it works. – Hendrik Vogt Jan 26 '11 at 15:59
5

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

morbusg
  • 25,490
  • 4
  • 81
  • 162
  • 11
    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
  • 1
    @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 09:21
  • 1
    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 04:45
  • @morbusg: I didn't downvote your answer, but I didn't upvote it either, as it doesn't seem to me like useful advice in practice (because of the "given a font which contains the glyph" caveat -- although it is of course perfectly logically correct!) – Arthur Reutenauer Apr 08 '11 at 10:33
4

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
mhp
  • 41
  • 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
4

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
4

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

PHL
  • 7,555