# amsmath - bidi - siunitx possible bug?

MWE:

\documentclass{article}
\usepackage[english]{babel}

\usepackage{fontspec}
\usepackage[detect-all]{siunitx}
\usepackage{amsmath}
\usepackage[math-style=ISO,bold-style=ISO]{unicode-math}

\usepackage{bidi}

\setmainfont{MinionPro-Regular.otf}
\setmathfont{xits-math.otf}
\setmonofont{Monaco}

\newcommand*{\problematic}{\SI{2.8}{\milli\joule\per\square\milli\metre}}

\begin{document}\noindent

\huge\problematic\par

\end{document}


Output:

If either amsmath or bidi is removed the output is ok.

amsmath commented:

-

Edit: Updated information based on Vafa's answer

As detailed below, there seems to be something up with bidi's modification of the \text command (as it typesets in RTL even if the document text is in LTR). However, there probably should be some active precaution taken for RTL typesetting in siunitx as well (even if \text did not have this odd behaviour, there would still be an issue inside a 'true' RTL block). I will add the appropriate code to siunitx for the next release.

The problem here is not with siunitx nor (really) with amsmath (I think), but is with how bidi sets out to modify the \text command. You can see the issue with example

\documentclass{article}
\usepackage{amstext}
\usepackage{amsmath}
\usepackage{bidi}
\begin{document}
\text{Jk}
\end{document}


(The same shows up with siunitx as it loads amstext for the \text command.)

What seems to be happening is that if only amstext is loaded, bidi leaves the \text command unmodified, and so standard behaviour is seen. However, bidi does check for amsmath, and if it is loaded modifies \text to have definition

\text #1->\@@text {\if@Latin \else \beginR \fi #1\if@Latin \else \endR \fi }


where \@@text is the original \text command. If you trace through, it seems that \if@Latin is set incorrectly here. So

\documentclass{article}
\usepackage{amstext}
\usepackage{amsmath}
\usepackage{bidi}
\csname @Latintrue\endcsname
\begin{document}
\text{Jk}
\end{document}


seems to sort things out.

-

More of a workaround than a fix but:

siunitx and bidi seem to be fighting over \everymath.

I commented out most of the font usage as I didn't have the fonts and the problem showed up without them.

\documentclass{article}
\usepackage[english]{babel}

\usepackage{fontspec}
\usepackage[detect-all]{siunitx}

\usepackage{amsmath}

%\usepackage[math-style=ISO,bold-style=ISO]{unicode-math}

\usepackage{bidi}

%\setmainfont{MinionPro-Regular.otf}
%\setmathfont{xits-math.otf}
%\setmonofont{Monaco}

\newcommand*{\problematic}{\SI{2.8}{\milli\joule\per\square\milli\metre}}

\begin{document}\noindent

\huge\problematic\par

\def\nobidi{\let\beginR\relax\let\endR\relax}

\huge{\nobidi\problematic}\par

\end{document}


I feel I must reference

http://xkcd.com/1137/

-
what about amsmath? it'll work without that as well. – rowman Nov 26 '12 at 14:50
No you need that to trigger the bidi clash. amsmath, siuintx and bidi all trample on the \every.... token lists but I ran out of time seeing exactly where the clash is. – David Carlisle Nov 26 '12 at 14:56
Please see the @Vafa answer. – rowman Nov 27 '12 at 9:16

This supposed to be a comment not an answer but since it is long, I could not submit it as a comment. David Carslie stated that

siunitx and bidi seem to be fighting over \everymath

bidi does not work with \everymath.

Joseph Wright said:

The problem here is not with siunitx nor (really) with amsmath (I think), but is with how bidi sets out to modify the \text command.

bidi needs to modify \text command the way it does for its RTL puprposes. It is siunitx problem which is not bidi-aware.

He also said:

What seems to be happening is that if only amstext is loaded, bidi leaves the \text command unmodified, and so standard behaviour is seen.

I admit that bidi should check for amstext not amsmath but that does not make any difference here.

He also said:

However, bidi does check for amsmath, and if it is loaded modifies \text to have definition

\text #1->\@@text {\if@Latin \else \beginR \fi #1\if@Latin \else \endR
\fi }


where \@@text is the original \text command. If you trace through, it seems that \if@Latin is set incorrectly here.

\if@Latin is only a conditional that bidi package provides so that packages like polyglossia set it to false for non-RTL languages and set it to true for RTL languages. xepersian makes extensive use of this conditional. On the other hand, as I said above \text needs to be modified for RTL purposes so that it typesets text RTL in math mode.

He also said:

where \@@text is the original \text command. If you trace through, it seems that \if@Latin is set incorrectly here. So

\documentclass{article}
\usepackage{amstext}
\usepackage{amsmath}
\usepackage{bidi}
\csname @Latintrue\endcsname
\begin{document}
\text{Jk}
\end{document}


You are always making the conditional \if@LAtin true which is not the purpose of bidi package. For making it more clear, lets look at the following example:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xepersian}
\begin{document}
$1+2=3\qquad\text{این یک فرمول است.}$
\end{document}


As you can see, the text comes out RTL as it should. Let's see how \if@Latin works:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xepersian}
\begin{document}
$1+2=3\qquad\text{این یک فرمول است.}$

\begin{latin}
$1+2=3\qquad\text{This is an equation.}$
\end{latin}
\end{document}


So now what is wrong with your example:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xepersian}
\csname @Latintrue\endcsname
\begin{document}
$1+2=3\qquad\text{این یک فرمول است.}$

\end{document}


The text comes out LTR which is obviously wrong. Hence the problem is not bidi; the problem is siunitx which is not bidi-aware.

Conclusion: siunitx needs to always output LTR SI units by using LTR \text because even if it uses RTL \text in RTL mode, the output would be RTL which is not right.

-
@Vafa I'm quite happy to add detect for bidi and whatever switch is needed to force LTR mode (if that is correct: I have no idea how unit printing is handled in RTL languages!). However, in my MWE you'll see that there has been no request for RTL typesetting, and so 'normal' text is LTR, but the \text content is still coming out RTL, which I think is wrong. – Joseph Wright Nov 27 '12 at 7:03
@Vafa One obvious question for me, as a non-expert, is how exactly the output should be ordered in RTL mode. Do I force the number to be on the left of the unit, or only alter the ordering within the unit, or something else? – Joseph Wright Nov 27 '12 at 7:24
@JosephWright, As a persian speaker I know that unit printing should be always done in LTR mode. The number is always on the left, the unit with LTR ordering on the right side. – rowman Nov 27 '12 at 9:13
@rowman OK, that makes life relatively easy then. I just need to work out which is the appropriate command and insert it at the start of the internal part of the \SI and \si commands. – Joseph Wright Nov 27 '12 at 9:15
@VafaKhalighi Thanks for confirmation on both the LTR part and the LuaTeX side of things. I may have to deal with the more general case (RTL but no bidi) at some stage, but for the moment am happy to cover just XeTeX. – Joseph Wright Nov 27 '12 at 11:58