50

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

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

7 Answers 7

36

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

3
  • 11
    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, 2011 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, 2012 at 13:08
  • 1
    Why not \mbox{-} as seen in mhp's answer?
    – n1000
    Nov 7, 2016 at 22:52
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.

4
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

8
  • @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. Jan 26, 2011 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 ...
    – user2478
    Jan 26, 2011 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? Jan 26, 2011 at 10:32
  • @Hendrik. it is not confusing ...
    – user2478
    Jan 26, 2011 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! Jan 26, 2011 at 10:36
5

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

6
  • 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). Jan 26, 2011 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 Jan 28, 2011 at 9: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). Jan 28, 2011 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, 2011 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, 2011 at 4:45
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

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
2
  • See the answer from morbusg and its discussion. Jul 25, 2011 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? Jan 30, 2012 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

4
  • An almost equivalent way is to load the newunicodechar package and say \newunicodechar{‑}{\mbox{-}}. It's actually a bit more robust.
    – egreg
    Jul 11, 2012 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, 2012 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, 2012 at 15:04
  • @egreg ah, so it's the \newunicodechar, then. Thanks.
    – morbusg
    Jul 11, 2012 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.