# align* environment left-aligned with LuaTeX and RTL math direction

When using LuaLaTeX, with the math direction set right-to-left, the contents of an align* environment is left-aligned instead of centered. Why does this happens? How can I fix it?

\documentclass{article}
\usepackage{amsmath}
\luatexmathdir TRT
\begin{document}
$$1+2=3$$

\begin{align}
1+2&=3\nonumber\\
4+5&=6
\end{align}

\begin{align*}
1+2&=3\\
4+5&=6
\end{align*}
\end{document}


Update: A similar problem, but this time LTR math:

\documentclass{article}
\usepackage{amsmath}
\luatexpagedir TRT
\luatexbodydir TRT
\begin{document}
$$1+2=3$$

\begin{align}
1+2&=3\nonumber\\
4+5&=6
\end{align}

\begin{align*}
1+2&=3\\
4+5&=6
\end{align*}
\end{document}


I was only interested in the second case as I never really typeset RTL math or need to implement it; it was good if \pagedir and \bodydir primitives had no effect in math mode (I am not sure to call that a bug or a feature). Anyway, I guess I am better off to not use \bodydir and \pagedir primitives. They are more problematic than being useful.

Another issue exits with multline environment used together with fleqn option:

\documentclass[fleqn]{article}
\usepackage{amsmath,lipsum}
\luatexpagedir TRT
\luatexbodydir TRT
\begin{document}
\lipsum[1]
$$1+2=3$$
\begin{multline}
a+b+c+d+e+f\\
+i+j+k+l+m+n
\end{multline}
\lipsum[1]
\end{document}


This produces wrong margin; to fix it we simply use \z@skip for \tabskip instead \@mathmargin:

\documentclass[fleqn]{article}
\usepackage{amsmath,lipsum}
\luatexpagedir TRT
\luatexbodydir TRT
\makeatletter
\def\multline@#1{%
\Let@
\chardef\dspbrk@context\z@
\restore@math@cr
\let\tag\tag@in@align
\global\tag@false \global\let\raise@tag\@empty
\mmeasure@{#1}%
\let\tag\gobble@tag \let\label\@gobble
%    \tabskip \if@fleqn \@mathmargin \else \z@skip \fi
\tabskip \z@skip
\totwidth@\displaywidth
\if@fleqn
\fi
\halign\bgroup
\hbox to\totwidth@{%
\if@fleqn
\hskip \@centering \relax
\else
\hfil
\fi
\strut@
\$\m@th\displaystyle{}##\endmultline@math
\hfil
}%
\crcr
\if@fleqn
\hskip-\@mathmargin
\def\multline@indent{\hskip\@mathmargin}% put it back
\else
\hfilneg
\def\multline@indent{\hskip\multlinegap}%
\fi
\iftagsleft@
\iftag@
\begingroup
\ifshifttag@
\rlap{\vbox{%
\normalbaselines
\hbox{%
\strut@
\make@display@tag
}%
\vbox to\lineht@{}%
\raise@tag
}}%
\multline@indent
\else
\setbox\z@\hbox{\make@display@tag}%
\ifdim\dimen@<\multlinetaggap
\dimen@\multlinetaggap
\fi
\box\z@ \hskip\dimen@\relax
\fi
\endgroup
\else
\multline@indent
\fi
\else
\multline@indent
\fi
#1%
}
\makeatother
\begin{document}
\lipsum[1]
$$1+2=3$$
\begin{multline}
a+b+c+d+e+f\\
+i+j+k+l+m+n
\end{multline}
\lipsum[1]
\end{document}


This solution puts \place@tag for align* environment where \iftag@ is false. This way all as far as I tested work fine.

\documentclass{article}
\usepackage{amsmath}
\luatexmathdir TRT
\makeatletter
\def\math@cr@@@align{%
\ifst@rred\nonumber\fi
\if@eqnsw \global\tag@true \fi
\omit
\kern-\alignsep@
%  \iftag@
%    \setboxz@h{\@lign\strut@{\make@display@tag}}%
%    \place@tag
%  \fi
\iftag@
\setboxz@h{\@lign\strut@{\make@display@tag}}%
\place@tag
\else
\place@tag
\fi
\ifst@rred\else\global\@eqnswtrue\fi
\global\lineht@\z@
\cr
}
\makeatother
\begin{document}
$$1+2=3$$

\begin{align}
1+2&=3\nonumber\\
4+5&=6
\end{align}

\begin{align*}
1+2&=3\\
4+5&=6
\end{align*}
\end{document}

• Can you please add a description of the solution? – egreg Sep 20 '13 at 9:15
• @egreg: Looks like he copied a definition from some distribution source files, then commented-out a few lines. An explanation would be nice, as would be some sort of assurance this commenting-out doesn't break other use cases (I would guess it does...) – einpoklum Sep 20 '13 at 19:43
• the code for \def\math@cr@@@align is pulled directly from amsmath.sty, and the commented \if block replaced by the \if block below it. a problem, also present in the original: the parentheses on the tags are facing outward, and a little experimentation shows that parens in the math itself are also; this indicates that they (and probably other fences) aren't handled properly. however, amsmath has, to the best of my knowledge, never really been tested in a right-to-left environment, and many other adjustments would be needed to use it there reliably. – barbara beeton Sep 20 '13 at 20:26
• @barbarabeeton: The problem with parentheses is not amsmath problem; it is natural that in a right to left mode, the parentheses on the tags are facing outward. I have been testing not only amsmath but also breqn, empheq and many other math packages and I have adapted them for right to left (see my bidi package). For instance see this output of empheq produced by xepersian (in Persian, text is bidi and math is LTR): parsilatex.com/joomla/attachments/article/114/align-boxed.pdf – user22486 Sep 21 '13 at 3:34
• @VafaKhalighi -- thanks for the information. while i agree that it's not something that amsmath should be "fixing", i do think it's important to note that there hasn't been testing at ams of r-to-l use. i will definitely look at the source you've linked. and if you find anything in amsmath that's a show-stopper for r-to-l processing (our list of bugs is long), please get in touch off-line. – barbara beeton Sep 21 '13 at 11:54