# Hebrew text in math mode

I'm trying to insert hebrew text into a formula, but the word order gets reversed. I can only get the correct output by manually inverting both word-order and surrounding spaces.

I'm using XeLaTeX and polyglossia for bidirectional text. Oddly, I remember that I used to get the correct behaviour in the past.

Here is an example document (definition of the prime-counting function):

\documentclass{article}

\usepackage{amsmath}
\usepackage{polyglossia}
\setmainlanguage{hebrew}
\newfontfamily{\hebrewfont}{David CLM}

\begin{document}

$\pi(n) = \#\{p \text{ מספר ראשוני שאינו גדול מ־} n\}$

$\pi(n) = \#\{\text{$p$מספר ראשוני שאינו גדול מ־$n$}\}$

$\pi(n) = \#\{\text{\texthebrew{$p$מספר ראשוני שאינו גדול מ־$n$}}\}$

$\pi(n) = \#\{n \text{מ־ גדול שאינו ראשוני מספר } p\}$

\end{document}


and this is the result I get when I compile it: The first three options show different ways to write the same code with identical, and wrong, result. Only the last one looks as intended, but its code is completely nonsensical.

EDIT: I've got some more information. xetex -version on my computer says

XeTeX 3.14159265-2.6-0.99996 (TeX Live 2016/Debian)
kpathsea version 6.2.2
Copyright 2016 SIL International, Jonathan Kew and Khaled Hosny.
There is NO warranty.  Redistribution of this software is
covered by the terms of both the XeTeX copyright and
the Lesser GNU General Public License.
named COPYING and the XeTeX source.
Primary author of XeTeX: Jonathan Kew.
Compiled with ICU version 57.1; using 57.1
Compiled with zlib version 1.2.8; using 1.2.8
Compiled with FreeType2 version 2.6.3; using 2.6.3
Compiled with Graphite2 version 1.3.9; using 1.3.9
Compiled with HarfBuzz version 1.4.2; using 1.4.2
Compiled with libpng version 1.6.28; using 1.6.28
Compiled with poppler version 0.48.0
Compiled with fontconfig version 2.11.0; using 2.11.0


I also compiled the file above on another machine. This was the result: Here the second and third lines display correctly. Running xetex -version on that machine gives

XeTeX 3.14159265-2.6-0.99992 (TeX Live 2015/dev/Debian)
kpathsea version 6.2.1dev
Copyright 2014 SIL International, Jonathan Kew and Khaled Hosny.
There is NO warranty.  Redistribution of this software is
covered by the terms of both the XeTeX copyright and
the Lesser GNU General Public License.
named COPYING and the XeTeX source.
Primary author of XeTeX: Jonathan Kew.
Compiled with ICU version 52.1; using 52.1
Compiled with zlib version 1.2.8; using 1.2.8
Compiled with FreeType2 version 2.5.3; using 2.5.3
Compiled with Graphite2 version 1.2.4; using 1.3.6
Compiled with HarfBuzz version 0.9.35; using 0.9.35
Compiled with libpng version 1.6.13; using 1.6.13
Compiled with poppler version 0.26.5
Compiled with fontconfig version 2.11.0; using 2.11.0


So this really did use to work correctly, and at some point a bug was introduced. I'm hoping the problem is in either polyglossia or bidi, so I'll make that I have up-to-date versions of those packages and try again.

• Not really an answer given your edit, but my system gives the same output as your first example. I can get what you want by using this: $\pi(n) = \#\{\text{\RL{$p$מספר ראשוני שאינו גדול מ־$n$}}\}$ (i.e., using \RL instead of \texthebrew). – David Purton May 29 '17 at 14:14

I'm not sure whether this is a bug in bidi or polyglossia, but the problem lies in the definition of \textdef@ from amstext-xetex-bidi.def.

The current definition looks like:

\def\textdef@#1#2#3{\hbox{{%
\everymath{#1}%
\let\f@size#2\selectfont
\if@nonlatin\beginR\fi#3\if@nonlatin\endR\fi}}}


The problem is that \if@nonlatin seems to be always false no matter what. Since math mode is set LTR, the \beginR is never run and so you get LTR text even though the font is correctly set.

A better test seems to be to use \if@RTL.

\documentclass{article}

\usepackage{amsmath}
\usepackage{polyglossia}
\setmainlanguage{hebrew}
\setotherlanguage{english}
\newfontfamily{\hebrewfont}{Linux Libertine O}[Script=Hebrew]

\makeatletter
\def\textdef@#1#2#3{\hbox{{%
\everymath{#1}%
\let\f@size#2\selectfont
\if@RTL\beginR\fi#3\if@RTL\endR\fi}}}
\makeatother

\pagestyle{empty}
\begin{document}

$\pi(n) = \#\{\text{$p$מספר ראשוני שאינו גדול מ־$n$}\}$

\bigskip

% blame google translate if translation is wrong...
\begin{english}
$\pi(n) = \#\{\text{$p$Primary number not greater than$n$}\}$
\end{english}

\end{document}