Using mathspec’s \setallmainfonts with Russian in polyglossia causes hang during compilation

I’ve just run into a problem with the combination of mathspec’s \setallmainfonts command and polyglossia with language set to Russian. I’m using MiKTeX 2.9 and compiling with XeLaTeX and when I use these two together, compilation just hangs and doesn’t complete.

I’ve found that there’s a difference in the log file between when Russian and other languages are loaded. In the case of Russian there is one more line where it loads something like babel shorthands which is not present while loading other languages. Maybe that might have something to do with the problem.

The hang occurs only when I actually use some math commands in the document, either between \$ or using equation enviroment.

This is a minimal example:

\documentclass[12pt]{article}
\usepackage{mathspec}
\usepackage{xltxtra}
\setallmainfonts[Mapping=tex-text, Numbers={Lining,Proportional}]{Linux Libertine O}

\usepackage{polyglossia}
\setdefaultlanguage{russian}

\begin{document}
$$f(x)=\sigma$$
\end{document}


I’m using mathspec and \setallmainfonts because I want to change the math font (\setmathsfont causes the hang as well). So removing them is not an option.

-
Would you please add a minimal example? –  egreg Sep 18 '11 at 20:06
OK. I’ve actually found out that the problem is a bit somewhere else so I’ve rewritten the question a bit and I’ve added a minimal example. –  josefec Sep 18 '11 at 20:20

You observation about shorthands is correct. Loading mathspec redefines \sigma to "σ, makes " active and \lets a certain control sequence to this character. Loading polyglossia with the russian option makes " active again and overwrites the previous definition of ". mathspec also sets up " to be active in math mode. However, using it in math mode invokes the unexpecting polyglossia definition of ", which enters an infinite loop.

Simply swapping the two packages doesn't work because of fontspec option clash. What we can do is restore the mathspec's meaning of " after russian has been loaded. It has to be done \AtBeginDocument, because polyglossia fiddles with " at that time, too.

\documentclass{article}

\usepackage{mathspec}
\setallmainfonts[Mapping=tex-text, Numbers={Lining,Proportional}]{Linux Libertine O}

\usepackage{polyglossia}
\setdefaultlanguage{russian}

\makeatletter
\begingroup
\catcode\"=\active
\AtBeginDocument{\let"=\eu@active@quote}
\endgroup
\makeatother

\begin{document}
$$f(x)=\sigma$$
\end{document}


In my opinion, both packages are at fault: they changes catcodes and definitions of active characters without explicit user's request.

EDIT: Polyglossia bug reported as issue #28.

-
Babel and Polyglossia at least declare in their documentation the activation of the characters, while mathspec uses " as math active without telling the user. There is, however a bug in Polyglossia, since the character is activated also without specifying that shorthands are enabled. –  egreg Sep 20 '11 at 16:39
Great answer. Thank you very much. :) –  josefec Sep 26 '11 at 21:20

try this setting:

\setmathfont{Latin Modern Math}
\setmainfont[Mapping=tex-text, Numbers={Lining,Proportional}]{Linux Libertine O}


Use any other math font, if Latin Modern Math is not available

-
One thing is that as far as I know you must specify (Digits, Latin, Greek) when using \setmathfont, then it might work. Nevertheless, I wouldn’t like to change the font. I want to use Linux Libertine for math. It works in other languages than Russian so I hope I should be able to make it work with Russian too. –  josefec Sep 18 '11 at 20:54
Libertine has no math! –  Herbert Sep 18 '11 at 20:56
As you can read in the mathspec documentation, it can change only the font used for letters and digits, not mathematical operators. That means you can use any ordinary font with this option. And I’ve been using it this way for quite a long time. Only now I’ve discovered this problem with Russian. –  josefec Sep 18 '11 at 21:01
sure, it is no problem to use Libertine for letters or digits, but you are using \sigma`. Then tell your system from which font it should be used –  Herbert Sep 18 '11 at 21:07
@Herbert — mathspec can change Latin letters, Greek letters, and digits only (albeit with suboptimal spacing in many cases). So \sigma is okay. –  Will Robertson Sep 18 '11 at 22:12
show 1 more comment