141

With ~ we can enforce that no line break occures between two words.

But this does not seem to work with special characters. I want LaTeX to enforce not to break the line after the - in (re-)creation.

In my document the line breaks between (re- and )creation, which looks a bit ugly.

How can I achieve this?

3

7 Answers 7

73

If you put it in a mbox, it can't get broken up:

\mbox{(re-)creation}

But that means it can't get broken up anywhere in the word!

3
  • 28
    When I use that, it allows the word to overflow the margins. Is there a way to force it (all) to a new line, if it doesn't fit?
    – Adam_G
    Commented Feb 8, 2015 at 16:59
  • 1
    and what if I want to allow tex to break it like this: (re-)cre- ation Commented Sep 25, 2018 at 3:08
  • 1
    (to answer my own question. extdash below is the correct answer. Use (re\=/)creation Commented Sep 25, 2018 at 3:09
75

Do not reinvent the wheel – there is a package for that

It is called the extdash package and adds multiple commands for new behaviour with dashes.

Important: load this package as the very last package (even after hyperref)

Add the shortcuts option: \usepackage[shortcuts]{extdash} if you want to use \-/ instead of \Hyphdash. More details at https://www.ctan.org/pkg/extdash.

You can then use new dashes to control breaking behaviour of words with dashes.

Standard LaTeX dashes

  • - Standard LaTeX hyphen
  • -- Standard LaTeX en-dash
  • --- Standard LaTeX em-dash

extdash breakable dashes

Words hyphened with these dashes can also be broken at other positions than the dash

  • \-/ hyphen
  • \-- en-dash
  • \--- em-dash

extdash unbreakable dashes

No line breaks possible at the hyphen

  • \=/ hyphen
  • \== en-dash
  • \=== em-dash

Useful for expressions like $x$\=/axis or $x$\=/$y$\=/plane or your question ;)

4
  • 4
    I had to use \Hyphdash* instead of \=/. Which are aliases according to the manual, but the latter simply doesn't work in my case.
    – hbogert
    Commented Aug 21, 2016 at 19:35
  • 2
    @hbogert The documentation states: "the last case is available if you use the package extdash with shortcuts option". You have to include the package like this: \usepackage[shortcuts]{extdash}.
    – mnme
    Commented Aug 24, 2016 at 17:54
  • 1
    @mnme I tried that, didn't work. Using [shortcuts] was also mentioned in the answer.
    – hbogert
    Commented Aug 25, 2016 at 20:11
  • Does this work for oblique dashes? ⸗ Commented Oct 15, 2019 at 1:23
63

The amsmath package provides the \nobreakdash command to insert before the relevant dash:

Sample output

\documentclass{article}

\usepackage{amsmath}

\usepackage[width=1.5cm]{geometry}

\begin{document}

A (re\nobreakdash-)creation.

A (re-)creation.

\end{document}
3
  • This worked great for me.
    – ali14
    Commented Aug 25, 2019 at 5:27
  • do you know how nobreakdash compares to extdash from the first answer?
    – user313032
    Commented Jun 13, 2023 at 20:33
  • @user313032 I have not used or studied extdash, but the reason I personally use \nobreakdash is that it is defined by a package (amsmath) that I already load Commented Jun 15, 2023 at 10:03
19

I am not sure now why (re-\nobreak)creation does not work, but (re\mbox{-}\nobreak)\nobreak\hspace{0pt}creation does, and does not prevent hyphenation of "creation". (Also, (re\mbox{-)}\nobreak\hspace{0pt}creation is maybe less elegant, but also correct.)

Of course, you might want to have a macro for that, for example:

\newcommand{\optprefix}[2]{% optional prefix
  (#1\mbox{-)}\nobreak\hspace{0pt}#2%
}

Notice that this macro allows for hyphenation of the prefix (in this case the prefix is just "re", so it does not make sense in this particular case).

Edit: as egreg pointed out in the comments, (re-\nobreak)creation won't work because the hyphen creates a "feasible breakpoint" (that is, a place in which TeX may break the line), and \nobreak comes after that breakpoint, so does not interfere with it. After \mboxing, the breakpoint lands in the box, and is effectively "hidden" from the paragraph-breaking routine.

Now \nobreak\hspace{0pt} has the effect of creating a (zero-width) "inter-word space", so that what comes after it is a new word for TeX (and hence it may consider hyphenating it). However, \nobreak makes sure that this "zero-width interword space" will not be a feasible breakpoint itself.

Also, as egreg pointed out in his comment, one might consider saying

\newcommand{\optprefix}[1]{(#1\mbox{-})}

so that you can write just \optprefix{re}creation, or even

\newcommand{\optprefix}[1]{(#1\mbox{-})\ignorespaces}

which would also allow \optprefix{re} creation - though I personally wouldn't use that, since it is one word and imho should "look like one word" in the source, too.

6
  • 2
    The "unprotected" hyphen in (re-\nobreak)creation creates a feasible break point after it, so the \nobreak has no effect. There is no break point in (re\mbox{-})creation. If you want to enable hyphenation in the main word, then (re\mbox{-})\nobreak\hspace{0pt}creation is the right thing to do; the \nobreak means that the following (zero) glue can't be taken as a break point, but the scanning for hyphenation can take place in creation that's a "new" word to TeX because it comes after glue.
    – egreg
    Commented Mar 22, 2013 at 0:40
  • 1
    Why not \newcommand{\optprefix}[1]{(#1\mbox{-})} to be called as \optprefix{re}creation? Or even \newcommand{\optprefix}[1]{(#1\mbox{-})\ignorespaces} that would allow \optprefix{re} creation?
    – egreg
    Commented Mar 22, 2013 at 0:45
  • @egreg: thanks. I didn't have my copy of The TeXbook, and frankly speaking, didn't remember these details by heart...
    – mbork
    Commented Mar 22, 2013 at 10:30
  • Maybe you can edit your answer to add the information.
    – egreg
    Commented Mar 22, 2013 at 10:36
  • I'll do it in the afternoon - now I'll have to log out from the computer and take care of my daughter;).
    – mbork
    Commented Mar 22, 2013 at 10:42
13

tl;dr

\newunicodechar{‑}{{\fontencoding{T1}\selectfont\symbol{'177}}\nolinebreak\hspace{0pt}}

Then just use ‑ (U+2011) instead of - (U+002D) before the closing parenthesis. This means you can now write (re‑)creation or (Web‑)Frameworks or even Entwicklungs‑, Test/QS‑, Staging‑ oder Produktivsystem (yes, I got dash-comma working).

If this does not work for you, @dessert reports that a combination of this and Dmitry’s answer works:

\newunicodechar{‑}{\babelhyphen{nobreak}}

Which one to use seems to depend on the version of babel you use, and how you use it.

Backstory

I was looking for this as well, but as a more generic solution, and so far, the answers I found all do not satisfy me. I do have the german Babel package included, but "~ just typeset a double-quote followed by a space for me (and, in fact, I think I prefer double quotes to render as quote characters).

I looked at Unicode, and ‑ U+2011 NON-BREAKING HYPHEN appears to be what I want. Apparently, Xe(La)TeX already supports this, but we use pdflatex (directly in one project, via maven-latex-plugin in another), so that was out.

This pointed me into the correct direction though: I already knew about \newunicodechar so I thought I could just make U+2011 available. My first attempt was…

% does not work
\newunicodechar{‑}{-}

… to just define it to the hyphen, but it does not work like that. So, after reading some answers here, I tried…

% throws an error message
\newunicodechar{‑}{\nobreakdash-\hspace{0pt}}

… but \nobreakdash is not known, apparently. Finally, I tried…

% works for me, except commas
\newunicodechar{‑}{-\nolinebreak\hspace{0pt}}

… and voilà, I can typeset “(Maschinen‑)Benutzernamen” without the ugly separation and the automatic hyphenation makes this into “(Maschinen-)Benut-⏎zernamen” which is just perfect. Even better, when copy/pasting from the PDF, you get an actual - U+002D HYPHEN-MINUS, which makes things like \texttt{<!‑‑} to get <!-- (for XML comments) work.

Update 2016-09-04: Commas

There was a problem though: ‑, would still shove the stray comma onto the next line. The explanation has to do with us still using the - ASCII hyphen-minus in our macro expansion, to which Teχ (un‑)helpfully adds a discretionary break. However, this can be worked around if the current font has a T1 encoding (and not just OT1 or LY1), by temporarily switching to the T1 encoding variant of the current font and outputting the named character 127 (0x7F), which is exactly the same as the standard \hyphenchar 45 (0x2D).

% works for me
\newunicodechar{‑}{{\fontencoding{T1}\selectfont\symbol{'177}}\nolinebreak\hspace{0pt}}

For the (custom TrueType) font I was using, this required some hacking due to a ttf2tfm bug; if you just want to make sure that your font’s T1 encoding has the correct glyph at the correct position, use this (and replace museo with your font name):

\documentclass{article}
\usepackage{fonttable}
\usepackage[T1]{fontenc}

\begin{document}
\thispagestyle{empty}
\pagestyle{empty}
\xfonttable{T1}{museo}{m}{n}
\end{document}

(Note: I merged my answers to several questions in this answer then copied it to these other questions, so don’t wonder if it shows up in three places.)

1
  • +1 for making the correct Unicode character useable! -\nolinebreak\hspace{0pt} did not work for me, but \babelhyphen{nobreak} from Dmitry's answer did, and of course it can be copied just as well.
    – dessert
    Commented Mar 24, 2016 at 15:24
5

If you use babel 3.9 (or newer) package:

\def\={\babelhyphen{nobreak}} % inserts a hyphen without a break after it 
                              % but allows breaks in the rest of the word

$\gamma$\=tocopherol can be found in corn oil, soybean oil, margarine, and dressings.
1
  • 4
    Be careful, this silently overwrites LaTeX's macron function (\={a} producing ā). An at least by plain LaTeX unassigned macro name and a definition with \newcommand is the way to go. However, thank you for bringing \babelhyphen{nobreak} into the discussion, this is a good way. +1 for that!
    – dessert
    Commented Mar 24, 2016 at 15:15
0

Okay so the question was asked more than 7 years ago but I think it would still help someone with the same problem like myself... If the rest of the writing is done and will stay the same, best option is to write this ridiculously easy code: \linebreak (re-)creation. The solutions presented here didn't work for me so I had to come up with this one, which is extremely easy but works just the way I wanted it to.

1
  • 4
    Claims such as "The solutions presented here didn't work for me" are vague and unactionable. What, exactly, didn't "work"? BTW, I'm not the down-voter.
    – Mico
    Commented May 10, 2020 at 4:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .