1

TL;DR version: How can I write a bilingual document, where the two languages use different scripts, such that (1) I don't need to manually set the font and language every time the document switches languages, and (2) discretionary and automatic hyphenation still work?

Full question with example code: I am using XeLaTeX to write a highly multilingual document in German and Russian. I use ucharclasses to automatically switch between the Latin and Cyrillic fonts I'm using, and polyglossia's language-switching commands and environments (\begin{otherlanguage}{russian}…\end{otherlanguage}, \foreignlanguage{russian}{…}, and/or \selectlanguage{russian}…\selectlanguage{german}, depending on the amount of Russian text I need to include). Here's a slightly fictionalized MWE that uses only \selectlanguage and two commonly available and highly contrasting fonts:

\documentclass[12pt]{article}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\usepackage{polyglossia}
\setmainlanguage{german}
\setotherlanguage{russian}
\usepackage[Latin, Cyrillic]{ucharclasses}
\newfontfamily\defaultfont{Arial}
\newfontfamily\cyrillicfont[Script=Cyrillic]{Linux Libertine O}
\setDefaultTransitions{\defaultfont}{}
\setTransitionsForLatin{\defaultfont}{}
\setTransitionsForCyrillics{\cyrillicfont}{}

\begin{document}
\selectlanguage{russian}
превысокомногорассмотрительствующий
превысокомногорассмотрительствующему
Тысячевосьмисотвосьмидесятидевятимикрометровый
\selectlanguage{german}
siebenhundertsiebenundsiebzigtausendsiebenhundertfünfundsiebzig
Rindfleischetikettierungsüberwachungsaufgabenübertragungsgesetz
Bundespräsidentenstichwahlwiederholungsverschiebung 
\selectlanguage{russian}
превысокомногорассмотрительствующий
превысокомногорассмотрительствующему
Тысячевосьмисотвосьмидесятидевятимикрометровый
\end{document}

And the output:

Output of the first MWE

I am looking for ways to avoid constant repetition of polyglossia's language-switching commands and environments. So far the least-worst solution I've come up with is to alias the commands with shorter versions (e.g., \newcommand{\R}[1]{\foreignlanguage{russian}{#1}}) but using even these gets a bit cumbersome when I need to switch languages several times in a single sentence.

I thought I could avoid all this annoying markup by using the ucharclasses package to automatically switch the language as well as the font. Example:

\documentclass[12pt]{article}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\usepackage{polyglossia}
\setmainlanguage{german}
\setotherlanguage{russian}
\usepackage[Latin, Cyrillic]{ucharclasses}
\newfontfamily\defaultfont{Arial}
\newfontfamily\cyrillicfont[Script=Cyrillic]{Linux Libertine O}
\setDefaultTransitions{\selectlanguage{german}\defaultfont}{}
\setTransitionsForLatin{\selectlanguage{german}\defaultfont}{}
\setTransitionsForCyrillics{\selectlanguage{russian}\cyrillicfont}{}

\begin{document}
превысокомногорассмотрительствующий
превысокомногорассмотрительствующему
Тысячевосьмисотвосьмидесятидевятимикрометровый
siebenhundertsiebenundsiebzigtausendsiebenhundertfünfundsiebzig
Rindfleischetikettierungsüberwachungsaufgabenübertragungsgesetz
Bundespräsidentenstichwahlwiederholungsverschiebung 
превысокомногорассмотрительствующий
превысокомногорассмотрительствующему
Тысячевосьмисотвосьмидесятидевятимикрометровый
\end{document}

There are two problems with this approach. First, the automatic hyphenation seems to be a lot worse:

Output of the second MWE

Second, there is no easy way of mitigating the first problem by inserting discretionary hyphens. For example, if I replace the line Bundespräsidentenstichwahlwiederholungsverschiebung with Bundesprä\-sidentenstichwahlwiederholungsverschiebung, in hopes of effecting the same hyphenation as in the previous example, then I get the following compilation error:

! Improper discretionary list.
<to be read again> 
                   \xpg@pop@language 
l.20 Bundesprä\-
                 sidentenstichwahlwiederholungsverschiebung

Am I going about this the wrong way, or is it simply not possible to have XeLaTeX automatically set the language based on the script (and have hyphenation and everything else work as expected)? If it's not possible, why not?

  • You are adding quite complicated stuff directly at the begin of words, and this can disturb the hyphenation. You could try to add e.g. a zero space \setTransitionsForLatin{\hyphenrules{german}\defaultfont\hspace{0pt}}{} . This won't help with \-. The transition code is call there too, and it really doesn't work if you have it in the middle of a discretionary. – Ulrike Fischer Jul 18 '17 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.