13

If babel doesn't know how to hyphenate a word, I can inform it of good line breaks with \babelhyphenation:

\documentclass{article}
\usepackage[nynorsk]{babel}
    \babelhyphenation[nynorsk]{fram-halds-skulen}
\begin{document}
framhaldsskulen
\end{document}

But how do I do this with polyglossia? The documentation doesn't say anything about it. \babelhyphenation obviously doesn't work:

\documentclass{article}
\usepackage{polyglossia}
    \setdefaultlanguage{nynorsk}
    \babelhyphenation[nynorsk]{fram-halds-skulen}
\begin{document}
framhaldsskulen
\end{document}

! Undefined control sequence.
l.4 ^^I\babelhyphenation
                        [nynorsk]{fram-halds-skulen}
? 
Sverre
  • 20,729
  • 2
    I would assume you can use \hyphenation{fram-halds-skulen} as long as the right language is in scope at that point – David Carlisle Nov 20 '14 at 22:30
  • @DavidCarlisle Right, I only tried \hyphenation[nynorsk]{fram-halds-skulen}, which didn't work. Then how will polyglossia understand what language the hyphenation is for if I just set \hyphenation{fram-halds-skulen}? – Sverre Nov 20 '14 at 22:33
  • 1
    it's a tex primitive it is the same mechanism as tex knows which hyphenation table to use for text at that point. – David Carlisle Nov 20 '14 at 22:35

1 Answers1

13

As usual, in the example the lines are overfull on purpose, so as to force hyphenation.

\documentclass{article}

\usepackage{polyglossia}
\setmainlanguage{nynorsk}

\begin{hyphenrules}{nynorsk}
\hyphenation{fram-halds-skulen}
\end{hyphenrules}

\begin{document}

\parbox{0pt}{\hspace{0pt}framhaldsskulen}

\end{document}

enter image description here

Use the same trick of \begin{hyphenrules} if you have different languages in your document and want to add some hyphenation rules for single words.


A possibly better interface:

\documentclass{article}

\usepackage{polyglossia}

\makeatletter
\newcommand\sethyphenation[3][]{%
  \begingroup
  \ifcsundef{#2@loaded}
    {\xpg@nogloss{#2}}
    {\setkeys{#2}{#1}\xpg@set@language{#2}%
     \hyphenation{#3}}%
  \endgroup
}
\@onlypreamble\sethyphenation
\makeatother


\setmainlanguage{nynorsk}
\setotherlanguage[variant=british]{english}

\sethyphenation{nynorsk}{fram-halds-skulen}

\sethyphenation{english}{tes-t-word}

\begin{document}

\parbox{0pt}{\hspace{0pt}framhaldsskulen testword}

\selectlanguage{english}

\parbox{0pt}{\hspace{0pt}framhaldsskulen testword}

\end{document}

The \sethyphenation command accepts also an optional argument such as variant=british, but at the moment it wouldn't do much good, because the same option to \selectlanguage doesn't seem to have any effect.

enter image description here

egreg
  • 1,121,712
  • Ah! \begin{hyphenrules}! – Sverre Nov 20 '14 at 22:36
  • @Sverre Yes, babel 3.9 uses a similar trick, but defines the \babelhyphenation command; there's no corresponding command in polyglossia, currently. – egreg Nov 20 '14 at 22:37
  • 1
    Just an extra note regarding variants, it is not quite clear what language argument one should pass to hypenrules. In the case of \setdefaultlanguage[variant=british]{english} it turns out one needs \begin{hyphenrules}{ukenglish}..., which I discovered by checking lua's messages in the log file. – Andrew Swann Nov 27 '14 at 19:12
  • @AndrewSwann Why? \begin{hyphenrules}{english} will choose the British hyphenation patterns, with that declaration for \setmainlanguage (which should be used instead of \setdefaultlanguage, although they're synonyms). – egreg Nov 27 '14 at 20:49
  • Unfortunately, this does not happen for a \begin{hyphenrules}{english} in the preamble with lualatex. You new code example throws the error ! LuaTeX error ...e/2014/texmf-dist/tex/luatex/hyph-utf8/luatex-hyphen.lua:60: l uatex-hyphen: \language0 should be dumped in the format at the l.29 \selectlanguage{english} too – Andrew Swann Nov 28 '14 at 12:14
  • @AndrewSwann I'd say that is a bug in LuaLaTeX/polyglossia (either or both); you solve it by saying \setotherlanguage{english} before \setotherlanguage[variant=british]{english} – egreg Nov 28 '14 at 13:13
  • 1
    If the word to be hyphenated contains a hyphen itself (such as “non-degenerate”), how would one write the \hyphenation command for it? – Ruud Apr 28 '15 at 09:02
  • 3
    @RuudvA You don't. By rule, TeX breaks words with explicit hyphens only at those hyphens. – egreg Apr 28 '15 at 09:17
  • Then why won't this simple code work, with different hyphenrules for the two languages? \documentclass{article} \usepackage{polyglossia} \setdefaultlanguage{english} \setotherlanguage{german} \begin{hyphenrules}{english} \hyphenation{Au-ße-ro-rd-en-tl-ic-hk-eit} \end{hyphenrules} \begin{hyphenrules}{german} \hyphenation{Auß-ero-rde-nt-lic-hke-it} \end{hyphenrules} \begin{document} \parbox{0pt}{\hspace{0pt}Außerordentlichkeit \textgerman{Außerordentlichkeit}} \end{document} – Sverre Jun 02 '19 at 14:37
  • I realized it's for the same reason as mentioned above with ukenglish. The hyphenrules need the names from babel, so swapping german with ngerman will do the trick. – Sverre Jun 02 '19 at 15:01