# Problem with babel and fontspec

I've just had a problem on using babel with lualatex (or xelatex as well) and fontspec in the context of latin and cyrillic alphabets. Here is a minimal example:

\documentclass[11pt]{article}

\usepackage{fontspec}
\setmainfont{Heuristica}%
\usepackage[english,russian]{babel}
\newcommand*\English{\selectlanguage{english}}

\begin{document}

\textup{\Russian Мороз и солнце, день чудесный}

\English\textit{The sea is calm to-night.}
\end{document}


And here's the resulting .pdf:

As one can see, the font used for the English part of the text is Computer Modern, not Heuristica, and there's not even italic where it's supposed to be italic.

However, if I change the order of the languages ([russian, english], everything is fine:

If I compile with pdflatex, modifying accordingly the preamble and using the type 1 versions of the fonts instead of the opentype, everything is fine whatever the order of the languages. I tried with some other fonts, same problem. Is this a bug or do I miss something?

-
poetrytex is irrelevant. –  egreg Jul 6 at 14:03
@egreg: True, I simply took my answer to a question. I''ll simplify my question. –  Bernard Jul 6 at 14:07
Load fontspec after babel. But note that the fontspec manual says that babel 'is not really supported' and recommends polyglossia (page 4). Nonetheless, it works if you change the order of loading. –  cfr Jul 6 at 14:12
@cfr: Didn't think of that, though I knew the trick for the French language, but the reason is active characters. Works lke a charm! Is this documented somewhere? If you make your comment an answer, I'll upvote it. –  Bernard Jul 6 at 14:20
@Bernard Done though it is really superseded by egreg's answer... –  cfr Jul 6 at 14:36

egreg's answer now explains why this works which I didn't understand when I posted it as a comment.

If you load fontspec after babel, then it all works:

\documentclass[11pt]{article}

\usepackage[british,russian]{babel}
\newcommand*\English{\selectlanguage{british}}
\usepackage{fontspec}
\setmainfont{Heuristica}%
\usepackage{poetrytex}

\begin{document}

\English
\begin{poem}{Title}{Author\\2014}
\textup{\Russian Мороз и солнце, день чудесный}\\
\textit{The sea is calm to-night.}
\end{poem}

\end{document}


But note that the fontspec manual describes babel as 'not really supported' and recommends polyglossia (page 4). I assume they mean for languages supported by the latter which is not all of them but does include Russian.

-

The relevant part from babel.def is

\AtEndOfPackage{\edef\latinencoding{\cf@encoding}}
\AtBeginDocument{%
\gdef\latinencoding{OT1}%
\ifx\cf@encoding\bbl@t@one
\xdef\latinencoding{\bbl@t@one}%
\else
\fi
}


Let's see what happens: at the end of the loading of babel, the instruction

\edef\latinencoding{\cf@encoding}


is performed; since fontspec recognizes being called by XeLaTeX, it sets \cf@encoding to EU1. This might be needed from packages loaded later on.

Then, at begin document, \latinencoding is set temporarily to OT1 and then the value of \cf@encoding is examined again, because it might have changed. If it is T1, then \latinencoding is changed to T1; the same is done if t1enc.def has been loaded. Since in your case \cf@encoding is EU1 and t1enc has not been loaded, nothing happens and \latinencoding keeps its value OT1.

Indeed, you get warnings from LaTeX such as

LaTeX Font Warning: Font shape OT1/Heuristica(0)/m/n' undefined
(Font)              using OT1/cmr/m/n' instead on input line 11.


that is issued when \English is done, because babel executes \edef\encodingdefault{\latinencoding}, in order to be able to use Latin characters.

This doesn't happen if, more correctly, fontspec is loaded after babel, because fontspec (actually fontspec-xetex.sty) does

\tl_set_eq:NN \cyrillicencoding \g_fontspec_encoding_tl
\tl_set_eq:NN \latinencoding    \g_fontspec_encoding_tl
\AtBeginDocument
{
\tl_set_eq:NN \cyrillicencoding \g_fontspec_encoding_tl
\tl_set_eq:NN \latinencoding    \g_fontspec_encoding_tl
}


and \g_fontspec_encoding_tl is EU1, so the faulty redefinition of \latinencoding to OT1 is overridden.

-
Crystal clear explanation, thanks! Of course with polyglossia, I didn't have the problem. –  Bernard Jul 6 at 14:24
I know I should (and would) have upvoted your answer, but I had promised @cfr since his solved my problem. –  Bernard Jul 6 at 14:41
@Bernard She wrote her comment while I was analyzing the problem, knowing for sure that fontspec has to be loaded after babel. As somebody in chat would say, I tend to write overlong answers. ;-) You can accept just one answer, but upvote as many as you want; not that it would really count for my reputation, unless you do it tomorrow. ;-) –  egreg Jul 6 at 14:52
I also tend to check the ins and outs of a question before answering:). Comment aside, I'm afraid I mixed up upvoting and accepting an answer (not yet very familiar with this system…) –  Bernard Jul 6 at 15:10