how to enforce correct hyphenation of “(near-)uniform” as “(near-)” + “uniform” instead of “(near-” + “)uniform”?

how to enforce correct hyphenation of (near-)uniform as (near-) uniform instead of (near- )uniform?

You can use \discretionary. It takes three arguments:

\discretionary{at line end}{at next line begin}{if not hyphened}


In you case all arguments must be empty. I suggest define a new command for that, like this.

\documentclass{article}

\newcommand{\myhyph}{\discretionary{}{}{}}

\begin{document}
\begin{minipage}{1.5cm}
(near-)\myhyph uniform
\end{minipage}
\end{document}


The {minipage} is only used to get a narrow line in this example.

With

\newcommand{\nbhyph}{\mbox{-}}


You can also define a non breakable hyphen:

(near\nbhyph)\myhyph uniform


I’m sure on can use active characters to make the macros shorter.

Note that babel with (n)german defines several short hands for hyphenations. See the manual on page 81.

You may warp this in new macros to get a short access:

[Update: As egreg said it’s maybe easier to define \newcommand{\sh}[1]{(#1\mbox{-})\discretionary{}{}{}}. I changed it]

\documentclass{article}

\newcommand{\sh}[1]{% s = special, h = hyphen ...
(#1\mbox{-})\discretionary{}{}{}%
}

\begin{document}
\begin{minipage}{1cm}
\sh{near}uniform
\end{minipage}

\end{document}

• is there also a way to tell LaTeX to get this right automatically, i.e. without changing the text near "(near-)uniform"? – Walter May 30 '12 at 9:02
• You should not call it a 'dash': "You can also define a non breakable dash:" since its a hyphen. – yo' May 30 '12 at 9:03
• @Walter: I added a solution with command, but I guess you can’t tell TeX to do this automatically … – Tobi May 30 '12 at 9:06
• I don't see any contradiction in 'non-breakable hyphen'. But I see a problem in calling - 'dash' while 'dash' is -- or ---. – yo' May 30 '12 at 9:21
• And for example, in 2-year-old dog you use hyphens and it's not at the line break... – yo' May 30 '12 at 9:23

You could use the german shorthands:

\documentclass{article}
\usepackage[ngerman,english]{babel}

\useshorthands{"}

• I'd rather suggest (near"~)""uni"-form. "~ to prevent hyphenation immediately after the dash, and "- to allow hyphenation for the rest of word. – lockstep May 30 '12 at 8:52
• @lockstep The "- in uniform is not necessary. But I add the "~. – Ulrike Fischer May 30 '12 at 9:02
• Adding \declare@shorthand{ngerman}{")}{\mbox{-})\hskip\z@skip} allows for (near")uniform, which might be more appealing. – egreg May 30 '12 at 9:37