# hyphenation next to a parenthesis

I have some very special hyphenation puzzles involving optional hyphens next to parentheses.

• `(semi)group` should be hyphenated as `(semi-)`/`group`
• `di[rected ]graph` should be hyphenated as `di-`/`[rected ]graph` or `di[rec-`/`ted ]graph` or <`di[rected]`/`graph` or `di[rected]-`/`graph` - I can't make up my mind> (whichever hyphenation leads to the most even inter-word spacing, with LaTeX choosing among all three possibilities)

How can I achieve such hyphenation?

Note: Anyone tackling this problem in the most general form (esp for the details mentioned in the @egreg answer) might want to set `\righthyphenmin=2`.

-

A more generic strategy that doesn't require changing internal tables.

Define two commands:

``````\newcommand{\semigroup}{(semi\discretionary{-)}{}{)}group}
\newcommand{\digraph}{di\-[rec\-ted~]graph}
``````

For the second one the usual `\-` suffices. For the first one the more powerful `\discretionary` is needed:

``````\discretionary{<prebreak>}{<postbreak>}{<nobreak>}
``````

If a line break (for hyphenation) takes place at the discretionary item, the `<prebreak>` text will be at the end of the line and the `<postbreak>` text at the start of the next line; otherwise `<nobreak>` will be used.

Don't forget to type `\semigroup{}` if a space follows.

In case you change your mind about `\digraph` it will be easy to change its definition than hunting through the document for it.

Another definition for `\digraph` allowing hyphenation after the closing bracket might be

``````\newcommand{\digraph}{di\-[rec\-ted\discretionary{]-}{}{\kern.3em]}graph}
``````

(there can't be flexible spaces in the `<nobreak>` text).

I don't think that infrequent "words" such as "(pseudo)scientific" (the full "pseudoscientific" is normally written without a hyphen) warrant a special treatment: it's easier to decide about hyphenation when the text is finished. However, you could make up your personal command:

``````\makeatletter
\newcommand{\myallowhy}{\nobreak\hskip\z@skip}
\makeatother
\newcommand{\spword}[1]{(#1\discretionary{-)}{}{)}\myallowhy}
``````

and input it as

``````\spword{pseudo}scientific
``````

The "pseudo" part would never be hyphenated. If that's desired, just change the definition into

``````\makeatletter
\newcommand{\myallowhy}{\nobreak\hskip\z@skip}
\makeatother
\newcommand{\spword}[1]{(#1\myallowhy\discretionary{-)}{}{)}\myallowhy}
``````

In order to do the same (i.e. retain all hyphenation possibilities) for "(computer-)aided" (hyphenation: com·put·er and aid·ed) with a hard hyphen, one can define

``````\newcommand{\spwordH}[1]{(#1\myallowhy\discretionary{-)}{}{-)}\myallowhy}
``````

Note: The definition of `\myallowhy` is modelled on Babel's `\allowhyphens` that can't be used here, because it's a no-op when the T1 encoding is in force.

Since `\nobreak` is used after `\makeatletter` its use is justified.

-
I am impressed! –  Lover of Structure Feb 3 '13 at 10:15
As an explanation for others, about my addendum re "(computer-)aided", which has a hard hyphen: I've experimented with `-\nolinebreak)\hskip0pt` and `\hyp\nolinebreak)\hskip0pt` (with `\hyp` from the `hyphenat` package) but ended up with linebreaks before the ")" in some cases. I don't know why. But your `\discretionary{-)}{}{-)}` with your `\myallowhy` seems to work. –  Lover of Structure Feb 6 '13 at 0:30
Could you explain your remark that "[s]ince `\nobreak` is used after `\makeatletter` its use is justified."? –  Lover of Structure Jun 16 '13 at 11:29
@LoverofStructure People using `\makeatletter` on their own initiative should know what they're doing. In particular that `\nobreak` should be issued when it's possible to know what mode it will appear in. –  egreg Jun 16 '13 at 12:21
I would say this is not about `\makeatletter` per se but more about you having a concrete justification (namely, knowledge of the mode not being v-mode) here. –  Lover of Structure Jun 17 '13 at 12:35
``````\lccode`\(`\(
\lccode`\)`\)
\lccode`\[`\[
\lccode`\]`\]

\hyphenation{(semi-)group di-[rec-ted}

\showhyphens{(semi)group di[rected ]graph}

\bye
``````

shows the possible hyphenation points as

``````[] \tenrm (semi-)group di-[rec-ted ]graph
``````
-
did you miss one: `di[rected]-/graph` ? –  barbara beeton Feb 1 '13 at 15:41
@barbarabeeton I didn't miss it, I just didn't do it, the OP had the space inside the bracket which means doing anything else would need a macro and explicit discretionary at that point rather than a simple hyphenation table entry. –  David Carlisle Feb 1 '13 at 17:46
Do your first four commands have side effects? –  Lover of Structure Feb 2 '13 at 8:20
@LoverofStructure yes they make ()[] letters for the hyphenation algorithm so (foobar) will be looked up as the 8 letter word (foobar) not the 6 letter word foobar, this can affect hyphenation all over. But if you restrict it to areas just containing technical constructs it's probably ok as natural language hyphenation patterns are not always appropriate there anyway. –  David Carlisle Feb 2 '13 at 10:18
@LoverofStructure thanks for the edit:-) –  David Carlisle Feb 3 '13 at 10:37