# Typesetting Right-to-Left math

UPDATE:

I've managed to produce something that is aesthetically correct, however with a lot of "hardcoding" and trial and error to manually measure my spaces. Not to mention that I have to write my equation "backwards" (e.g. "^2 b" instead of "b^2", and "0 = s" instead of "s = 0"). Is there a cleaner and more automatic way to do this ? The example then its minimal code follows:

\documentclass{article}
\usepackage[leqno]{amsmath}
\usepackage{graphicx}
\newcommand{\asqrt}[1]{\reflectbox{\ensuremath{\sqrt{\reflectbox{\ensuremath{#1}}}}}}
\newcommand{\asum}[3]{\reflectbox{\ensuremath{\displaystyle\sum\limits_{\reflectbox{\ensuremath{#1}}}^{\reflectbox{\ensuremath{#2}}} \reflectbox{ \ensuremath{#3} }}}}

\usepackage{polyglossia}
\setdefaultlanguage[calendar=gregorian]{arabic}  % from polyglossia
\begin{document}
$$\asqrt{\frac{ ^{\raisebox{3pt}{\mbox{\tiny 2}}}\mbox{ف} } {\mbox{ب} \mbox{4}}}$$
$$\asum{\mbox{\scriptsize 0 = س}}{\hspace{-9pt} \scriptsize \infty}{ ^{\mbox{\small 2}} \mbox{س}}$$
\end{document}


Original question:

I am using XeLaTeX along with polyglossia package to typeset a mathematical article in Arabic. It is working very well for text but not for math.

The text is of course set Right-to-Left. However this is not the case for math, which is by default typeset the other way around. Unfortunately, this is not the only problem with it; I do as a matter of fact, need a "mirrored" version of the integration and summation symbols, and square roots.

1) Is there a ready package for that (I did search but didn't find any) ?

2) Is it possible to build such a package without losing existing features like the align environment of amsmath, or I will also have to re-implement that ?

In particular I wondering how bad is my situation if I try to approach implementing a complete solution for RTL math for Arabic.

EDIT: I know there is the question How to typeset an Arabic paper with the equations in the usual Latin in Latex? but it is talking about Latin equation, not Arabic equations.

EDIT 2:

I can try some tricks like mirrorbox (like this \vec{x} but with arrow from right to left?) but still it doesn't work correctly.

There is several problems:

1. If I do not put the Arabic characters inside a box (mbox) they do not appear nor reserve a vertical space at all.
2. When using the "power" operator "^", I have to put the power before the base. Instead of x^2, I have to write it like ^2 x. However even so, it doesn't look raised because the size of the power mbox wasn't reduced (restricted space problem, it does look raised in the second example).
3. Reversed order for numbers and characters. In the denominator of the attached photo, it should appear as "4 b" but rather rendered as "b 4". To render it right I have to reverse the order in my source.

All these reversals will make the source incomprehensible for complex equations.

The example:

\asqrt{ \frac{ ^{\mbox{2}} \mbox{ف} } { \mbox{4 ب} } }


Where \asqrt is defined as :

\usepackage{graphicx}
\newcommand{\asqrt}[1]{\reflectbox{\ensuremath{\sqrt{\reflectbox{\ensuremath{#1}}}}}}


EDIT 3:

It gets hopeless with something like summation. The assignment below the summation should be reversed. Moreover, the need to use mbox makes the proportions very bad as well.

\newcommand{\asum}[3]{\reflectbox{\ensuremath{\sum\limits_{\reflectbox{\ensuremath{#1}}}^{\reflectbox{\ensuremath{#2}}} \reflectbox{ \ensuremath{#3} }}}}

\asum{\mbox{س = 0}}{\infty}{^{\mbox{2}} \mbox{س}}


-
@Joseph Wright: All our (Egypt's) math textbooks in pre-college (high school and before) are typeset this way, and this is the way we write them in hand writing. In college we used foreign (English) textbooks, there is no college-level Arabic books that typeset RTL math, maybe for the lack of support in LaTeX. (1/2) –  M. Alaggan Jun 14 '11 at 5:58
(2/2) However there is a need for RTL math; it is done manually, for you can check for yourself : iraqacad.org/Lib/yaroub/yaroub2.htm , samehar.wordpress.com/2007/07/14/a13072007 , and there is one example also in ar.wikipedia.org/wiki/… if you look closely, which is done by the square root character in ASCII + an overbar. –  M. Alaggan Jun 14 '11 at 5:58
As a matter of fact, I am staff in a computer science faculty who is trying to make a move for "Arab-izing" our texts (to make it more accessible to students), and this is one major problem I am facing. Maybe no formal Arabic journals right now require RTL math, but this is wrong, and it need to be fixed. –  M. Alaggan Jun 14 '11 at 6:03
XeTeX (and all engines based on TeX--XeT) does not support RTL in math mode, but LuaTeX does. I did some experiments while ago and I got some promising results khaledhosny.org/arabic_math. If you are not in hurry I can prepare something in the next week. I'm planning a math extension of my Amiri font later this year. –  Khaled Hosny Jun 14 '11 at 7:51
@Karl: this Wikipedia article en.wikipedia.org/wiki/Modern_Arabic_mathematical_notation (and the linked w3c page at bottom) give a good overview on different Arabic math notation, but in short there is slight variation between different countries, so it depends on your audience rather than personal preference. BTW √ is a mirrored the its Arabic counterpart not the other way around; it started as a rotated ج (for جذر; root), at least in some theories. –  Khaled Hosny Jun 14 '11 at 14:16

As promised in the comment above, the new version of the XITS font has preliminary RTL support. There is a simple ConTeXt test file in the repository (you need a recent ConTeXt MkIV version) but it shouldn't be hard to port it to LuaLaTeX and unicode-math package.

It is work in progress, not all symbols have been mirrored and support for the proposed Arabic math symbols still lacking.

There is one caveat though, luatex reverses the direction of everything including numbers, so you either want to do something like {\textdir TLT 123} or try my experimental luadirections package (however it doesn't work in math mode yet, at least in ConTeXt, but I'm investigating that.)

-
@Khaled Hosny: Thanks for the effort :) One question though, it appears from the examples is that I still have to write my equations in the source in a reversed way (0 = b, not b = 0), right ? I know this is irrelevant to rendering, but is there a way to make it right ? –  M. Alaggan Jun 18 '11 at 20:15
@Mohammad: No, you enter the equations in the logical order, all the reversing is done by LuaTeX. (it might look so in the source file, but it is just because your browser/text editor is reversing the text, I usually use vim for such editing since it doesn't support BiDi by default.) –  Khaled Hosny Jun 19 '11 at 5:56
@Khaled Hosny: Great! It showed correctly in Emacs. Many thanks :) –  M. Alaggan Jun 20 '11 at 3:04
@Khaled Hosny: I am okay with reversing the directions of numbers. Originally, numbers in Arabic are pronounced from the least significant digit first :) –  M. Alaggan Jun 20 '11 at 3:06
@Khaled Hosny: This behaviour in Arabic is considered BiDi, and it was actually dictated by the Arabs who first gave feedback on supporting Arabic, because they personally do it this way. I don't believe that this is enough to make it the way. It just adds confusion to people and complication to implementation. If anything, I would at least recommend it being controlled by an option to the package :-) –  M. Alaggan Jun 21 '11 at 11:47

The following is not a direct answer to the question, since it is more focused on Unicode than on TeX.But I think it is relevant nevertheless, specially with XeLaTeX, and has probably to be kept in mind if someone want to write a package for Arabic mathematical typesetting.

Unicode has encoded unicode 6.1 ( January 2012) a set of Arabic Mathematical Symbols in for at positions 1EE00..1EEF1 (pdf chart). Some fonts will probably include them.

Some other are encoded since unicode 5.1 and are present in some fonts. They are :

• ؆ U+0606 ARABIC-INDIC CUBE ROOT
• ؇ U+0607 ARABIC-INDIC FOURTH ROOT
• ؈ U+0608 ARABIC RAY
• ؉ U+0609 ARABIC-INDIC PER MILLE SIGN
• ؊ U+060A ARABIC-INDIC PER TEN THOUSAND SIGN
• U+2B30 — U+2B4C a set of reversed arrows for RTL maths. See also the discussion in n3259 (pdf) for the use of reverse vs mirror arrows.

The final proposal to unicode, with some background to the character repertoire is n3799 (pdf), and the original proposals from 2006, which have more details on the specific typographic for Arabic math (like a special aleph to avoid confusion with 1 and so on) is n3085-1 (pdf) and n3086-1(pdf).

Azzeddine Lazrek, from Cadi Ayyad University (Marrakech) is at the origin of these proposals, and has webpage which contains some LaTeX packages. This webpage als an example comparing the typesetting of the same mathematical expression using various typographic traditions (English, French, Moroccan, Western Arabic and Eastern Arabic).

-
+1 for the updated information! I didn't know about the revised proposal until now. –  Khaled Hosny Jun 16 '11 at 12:24