0

I would like to mix cyrillic and latin characters in my document using XeLaTeX. It compiles, but the cyrillic characters do not show up in the pdf. Also, I would like to use the default LaTeX font for the latin characters and use another font for the cyrillic ones, if necessary.

\documentclass{article}

\usepackage{fontspec}
\usepackage[T1,T2A]{fontenc}

\usepackage[russian,english]{babel}
\begin{document}
  Latin first.

\fontencoding{T2A}\selectfont Математические формулы  
\end{document}
  • Welcome, why are you fiddling with font encodings? Remove everything font related which is not fontspec. – Johannes_B Jul 7 '18 at 5:49
  • Even then the same thing happens. – U Ser Jul 7 '18 at 5:52
  • \usepackage{libertine} for example. But there are dozens of questions around already. – Johannes_B Jul 7 '18 at 5:54
  • I have checked many of them, none of them answer my question. Yes, libertine shows the cyrillic characters, but changes the latin ones which I want to preserve in original. Thanks in any case. – U Ser Jul 7 '18 at 5:58
  • 1
    do not use 8-bit fonts such as T1 or T2A with xelatex. If you use \usepackage[T1,T2A]{fontenc} then all hyphenation will be wrong. – David Carlisle Jul 7 '18 at 7:31
2

You got a polyglossia answer, so here is a babel solution. These features are fairly new (the babel docs say version 3.15).

\documentclass{article}
\usepackage{fontspec}
\usepackage[russian,english]{babel}

\babelfont{rm}[
  Ligatures=TeX,
  SmallCapsFont={Latin Modern Roman Caps}]%
  {Latin Modern Roman}
\babelfont[russian]{rm}[
  Ligatures={Common, TeX},
  Scale=MatchUppercase]%
  {Libertinus Serif}

\begin{document}
Mathematical formulas

\begin{otherlanguage}{russian}
Математические формулы
\end{otherlanguage}

Mathematical formulae

\foreignlanguage{russian}{Математические формулы}
\end{document}

enter image description here

Note the difference in the language-switching interface (\begin{otherlanguage}{<language>}...\end{otherlanguage} and \foreignlanguage{<language>} vs \begin{<language>}...\end{<language>} and \text<language>).

There is also no \setmainfont here, even the default language is set up with \babelfont.


Maybe a few words to what is going on here. As mentioned by David Carlisle in the comments, fontenc (and its encoding switches) should not be used with XeLaTeX or LuaLaTeX. But even if you remove it you don't see what you expect. The font your document uses with the setup from the question does not have characters for Cyrillic, so these characters are just discarded.

The prettiest solution would probably be to pick a font that supports all scripts you need (so at least Cyrillic and Latin) and then use the same font for the entire document. That makes the appearance more uniform and harmonic.

Davislor's answer and this one here both show a way to select the font depending on the selected language in the document. It would apparently also be possible to try to change font depending on Unicode blocks, see How can I use ucharclasses to change the font for a special script and then restore to what it was before?, In LuaTex is it possible to change font/language according to the script/glyphs used?, XeTeX, change font usage when meeting greek characters (Sabon Linotype / Sabon Greek), Change XeTeX fonts automatically depending on Unicode blocks.

  • You don't like the Common ligatures? ;) – Davislor Jul 7 '18 at 8:20
  • @Davislor That just snuck in from somewhere. I hadn't given it much though, but I do like my --s to turn into en-dashes, so I would probably keep it. – moewe Jul 7 '18 at 8:25
  • 1
    Ligatures={Common, TeX} works, as I'm sure you know. Or {Common, Rare, Historic, TeX} if the font supports it. – Davislor Jul 7 '18 at 8:27
  • @Davislor I didn't actually, thank you. But I'm wondering what I'm missing by declaring only Ligatures=TeX. A short test with the most common ligatures seemed to give the same results as Ligatures={Common, TeX}. – moewe Jul 7 '18 at 8:45
  • 1
    @Davislor If I understand correctly, babel actually handles Script and Language itself, so that should not be needed (and is discouraged) when using \babelfont. I used Language=Default so silence the warning about Russian not being available with Libertinus, but I figured it would be better to remove it. – moewe Jul 7 '18 at 8:52
3

Use polyglossia to get multilingual text, and set \russianfont.

Use \begin{russian} for long passages in Russian and \textrussian{} for short ones. If the document is primarily in Russian, \setdefaultlanguage{russian}.

\documentclass[varwidth, preview]{standalone}
\usepackage{fontspec}
\usepackage{polyglossia}

\setmainfont{Latin Modern Roman}
\defaultfontfeatures{Scale=MatchUppercase}
\newfontfamily\russianfont{Libertinus Serif}[
  Script=Cyrillic,
  Language=Russian]

\setdefaultlanguage{english}
\setotherlanguage{russian}

\begin{document}

Latin first.  \textrussian{Математические формулы}

\end{document}
  • I get an error when processing the above file: `fontspec warning: "language-not-exist" * Language 'Russian' not available for font 'Libertinus Serif' with script * 'Cyrillic'. * 'Default' language used instead' – Paulo Ney Feb 21 at 12:06
  • @PauloNey That warning message is harmless, but you can suppress it by deleting Language=Russian within \newfontfamily\russianfont. There are, however, some fonts for which selecting Russian or Bulgarian matters. – Davislor Feb 22 at 4:18

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.