34

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:

Looks good

\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
\setmainfont[Script=Arabic,Scale=1.8,Mapping=arabicdigits]{Scheherazade}
\begin{document}
\begin{equation}
\asqrt{\frac{ ^{\raisebox{3pt}{\mbox{\tiny 2}}}\mbox{ف} } {\mbox{ب} \mbox{4}}}
\end{equation}
\begin{equation}
\asum{\mbox{\scriptsize 0 = س}}{\hspace{-9pt} \scriptsize \infty}{ ^{\mbox{\small 2}} \mbox{س}}
\end{equation}
\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}}}}}}

Example of incorrect behaviour

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{س}}

Another bad example

  • Are you already using the bidi package? I don't know much about RTL typesetting, but if anything will do what you want, it's that package. – Alan Munn Jun 14 '11 at 03:53
  • @Alan Munn: I've looked at their documentation (http://mirrors.ircam.fr/pub/CTAN/macros/latex/contrib/bidi/bidi.pdf). As far as I can see from the definition, the closer they get to math is just in numbering the equations and theorems. There seem to be nothing about actually typesetting RTL math. – Mohammad Alaggan Jun 14 '11 at 04:01
  • Least of all, there is nothing in there about an RTL square root operator (a mirrored one). – Mohammad Alaggan Jun 14 '11 at 04:04
  • @Mohammad: Can you point to publications where maths is typeset in this way? I've not come across maths being typeset RTL. – Joseph Wright Jun 14 '11 at 05:20
  • 6
    @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) – Mohammad Alaggan Jun 14 '11 at 05:58
  • 2
    (2/2) However there is a need for RTL math; it is done manually, for you can check for yourself : http://www.iraqacad.org/Lib/yaroub/yaroub2.htm , http://samehar.wordpress.com/2007/07/14/a13072007/ , and there is one example also in http://ar.wikipedia.org/wiki/%D8%AC%D8%B0%D8%B1_%D8%AA%D8%B1%D8%A8%D9%8A%D8%B9%D9%8A if you look closely, which is done by the square root character in ASCII + an overbar. – Mohammad Alaggan Jun 14 '11 at 05:58
  • 2
    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. – Mohammad Alaggan Jun 14 '11 at 06:03
  • 8
    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 http://www.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. – خالد حسني Jun 14 '11 at 07:51
  • @Khaled Hosny: This is great news :) ! I am not in a hurry and I'll be most eagerly looking forward for your reply :) – Mohammad Alaggan Jun 14 '11 at 08:03
  • @Khaled Hosny: I think that isn't the correct way. The root symbol is not a letter, but the summation and integration symbols are letters (Greek Sigma and script Latin S). And since Arabic math does not use any Latin or Greek letters for variables, it should not use them for operators too. The best solution would be to have Arabic symbols for summation and integration. Also, what do you think about mirroring Arabic letters? Is it acceptable? Because, if mirroring Arabic letters for some reason is not acceptable, for sake of correctness mirroring Latin or Greek will be incorrect too. – Karl Karlsson Jun 14 '11 at 12:44
  • @Karl Karlsson: I have seen an Arabic summation operator, composed of the first two characters of the word "total" in Arabic, namely "مج". That was in my Statistics course in High School. There are Arabic letters for differentials as well. However I've never seen an Arabic counter part for the Integral operator. – Mohammad Alaggan Jun 14 '11 at 12:51
  • @Mohammad ALAGGAN: Personally, I have nothing against mirroring. But you know, there are people on both sides that insist on tradition. And using all Arabic, I think, will look better, will be easier to read, and will not confuse the readers. And since Khaled is making a new font, it's better to invent new things for the future, not take them from the past. I also think, that's an opportunity for improving the math notation. What about making a symbol that is both sum and integral? Many mathematicians will like it. And than the idea can go to the Latin math too. :) – Karl Karlsson Jun 14 '11 at 13:08
  • 4
    @Karl: this Wikipedia article http://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. – خالد حسني Jun 14 '11 at 14:16
  • @Khaled Hosny: I know about that, but it's a wrong concept. Mirroring is not just visual. When you mirror a counterclockwise contour integral it becomes clockwise. When you mirror the angle sign of a right coordinate system it becomes angle sign of a left coordinate system. It's not just a mirrored notation, it's a new math. This causes the confusion I wrote about earlier. Mirroring is not that trivial. Thats why I wrote about symbols that are not mirrored. – Karl Karlsson Jun 14 '11 at 17:14
  • MathMagic equation editor (www.mathmagic.com) promised to support some templates for RTL equations, such as reversed Square Root template, although they did not announce the schedule. More requests will definitely urge them to support more RTL features with priority so your help is appreciated. –  Jan 08 '12 at 03:51

2 Answers2

24

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.)

mandatory sample :)

  • @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 ? – Mohammad 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.) – خالد حسني Jun 19 '11 at 05:56
  • @Khaled Hosny: Great! It showed correctly in Emacs. Many thanks :) – Mohammad Alaggan Jun 20 '11 at 03:04
  • @Khaled Hosny: I am okay with reversing the directions of numbers. Originally, numbers in Arabic are pronounced from the least significant digit first :) – Mohammad Alaggan Jun 20 '11 at 03:06
  • 123 = ثلاثة وعشرون ومائة. I can write it in the logical order 3 - 2 - 1. – Mohammad Alaggan Jun 20 '11 at 03:06
  • @Mohammed: I know, but computers treat numbers as left-to-right so we do enter them in the reverse way (when I first learnt using computer this has always confused me). Anyway, I have now some lua code that keeps numbers LTR, it just needs some polish and I'll see if I can make some arabic-math package (or integrate it into unicode-math). – خالد حسني Jun 20 '11 at 06:36
  • 1
    @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 :-) – Mohammad Alaggan Jun 21 '11 at 11:47
  • Hey @KhaledHosny, what happened to the test files? The link does not seem to work anymore. – Bored Comedy Oct 27 '20 at 01:47
  • @KhaledHosny , where is the code?please – Mamoun Mohammed Nov 23 '20 at 22:37
  • 1
    @MamounMohammed I believe this is the source code he was talking about: https://github.com/khaledhosny/context-bidi. I have no idea how I would use that with LuaLaTeX and ConTeXt though. If you managed to do so, it would be nice if you shared your process. – Bored Comedy Apr 30 '21 at 15:56
12

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).