Nonbreaking dash that allows hyphenation
If there is not already a shortcut you can as below, or better as egreg points out in comments
\makeatletter
\defineshorthand[ngerman]{"/}{\mbox{-}\bbl@allowhyphens}
\makeatother
Then use "/
.
\documentclass{article}
\usepackage[german]{babel}
\setlength\textwidth{5cm}
\newcommand\zz{-\nolinebreak\hspace{0pt}}
\begin{document}
\noindent X\dotfill X
aaaa aaaa aa $x$-Koordinaten
aaaa aaaa aaa $x$-Koordinaten
aaaa aaaa aaaa $x$-Koordinaten
aaaa aaaa aaaaa $x$-Koordinaten
===
aaaa aaaa aa $x$\zz Koordinaten
aaaa aaaa aaa $x$\zz Koordinaten
aaaa aaaa aaaa $x$\zz Koordinaten
aaaa aaaa aaaaa $x$\zz Koordinaten
\end{document}
Here's a solution straight from the user guide of the amsmath
package. (To get this to work, the amsmath
package has to be loaded.)
$x$\nobreakdash-\hspace{0pt}Koordinaten
If you find yourself typing this sequence a lot, you may want to create a macro along the following lines:
\newcommand{\nbdx}{$x$\nobreakdash-\hspace{0pt}} % "no-break dash with x"
and type \nbdx Koordinaten
in the body of the text.
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.)