19

The babel package provides useful dash variants, e.g. "~ for a non-breaking dash that forbids hyphenation in the parts, and "= for a breaking dash that allows hyphenation in the parts.

However, "words" like

$x$-Koordinaten

do not quite fit; we would not want to have the dash breaking, but we would want to allow hyphenation of "Koordinaten". Of course,

$x$"~Ko\-or\-di\-na\-ten

works but has to be specified locally, which we do not want to do either (favoring a global \hyphenation{...} setting).

Is it possible to achieve the above in a form that does not force us to specify hyphenation locally?

19

enter image description here

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}
  • 1
    @Raphael tex adds penalties that allow breaking after an explicit hyphen, the mbpx stops that. – David Carlisle Oct 8 '13 at 18:32
  • @egreg and David, "/-solution is pretty nice, I used it for long time untill came to the project where xelatex is used. Babel-based solutions does not work there. What would you suggest for this case? – Artem Pelenitsyn Jul 28 '15 at 13:19
10

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.

  • What is the difference between \nobreakdash and \nolinebreak (as David uses), aside from the fact that \nobreakdash does not seem to work in a babel shorthand? – Raphael Oct 8 '13 at 18:21
  • I'd say that the \nobreakdash method is independent of, rather than in conflict with, the babel shorthands. Assuming you've loaded babel with an option such as ngerman, LaTeX should have no problem finding breakpoints inside the word "Koordinaten". – Mico Oct 8 '13 at 18:24
  • 2
    @Raphael \nobreakdash- essentially does \sbox0{-}\unhbox0 which is mostly equivalent to the \mbox{-} method because it just removes the boxing (and the hyphen character does not add a discretionary item any more). – egreg Oct 8 '13 at 23:09
  • @Mico Thank you! all other sollutions didn't worked. Maybe this solution will be found easyer if I add: cvlist and currvita package. – alex Nov 8 '15 at 17:56
5

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.)

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.