[Tex/LaTex] Mix cyrillic characters and latin in XeLaTeX

cyrillicfont-encodingsxetex

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}

Best Answer

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.

Related Question