latex3 / babel

The babel system for LaTeX, LuaLaTeX and XeLaTeX
LaTeX Project Public License v1.3c
125 stars 34 forks source link

Wrong place for equation number in align envionment (with bidi=basic option) #159

Closed seloumi closed 2 years ago

seloumi commented 2 years ago

For this example equation numbering is outside text area

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage[bidi=basic]{babel}
\babelprovide[import,main,mapdigits]{arabic}
\usepackage{amsmath}
\babelfont{rm}{Amiri}

\fboxsep=0pt
\parindent=0pt

\begin{document}

\fbox{\begin{minipage}{.8\linewidth}
\begin{align}
f(x)&=1+\dfrac{1}{x+2}\\
    &=1+k(x+2) 
\end{align}
\end{minipage}}

\begin{align}
f(x)&=1+\dfrac{1}{x+2}\\
    &=1+k(x+2) 
\end{align}

{
\selectlanguage{nil}

\fbox{\begin{minipage}{.8\linewidth}
\begin{align}
f(x)&=1+\dfrac{1}{x+2}\\
    &=1+k(x+2) 
\end{align}
\end{minipage}}

}

\end{document}
seloumi commented 2 years ago

It appears that the problem is coming from \rlap and \llap in RTL context, if we switch the role of the two commands in \place@tag command we obtain correct result

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage[bidi=basic]{babel}
\babelprovide[import,main,mapdigits]{arabic}
\usepackage{amsmath}
\babelfont{rm}{Amiri}

\makeatletter
\def\place@tag{%
    \iftagsleft@
    \ifcase\bbl@thetextdir
      \else
      \let\rlap\llap
      \fi
        \kern-\tagshift@
        \if1\shift@tag\row@\relax
            \rlap{\vbox{%
                \normalbaselines
                \boxz@
                \vbox to\lineht@{}%
                \raise@tag
            }}%
        \else
            \rlap{\boxz@}%
        \fi
        \kern\displaywidth@
    \else
      \ifcase\bbl@thetextdir
      \else
      \let\llap\rlap
      \fi
        \kern-\tagshift@
        \if1\shift@tag\row@\relax
            \llap{\vtop{%
                \raise@tag
                \normalbaselines
                \setbox\@ne\null
                \dp\@ne\lineht@
                \box\@ne
                \boxz@
            }}%
        \else
            \llap{\boxz@}%
        \fi
    \fi
}
\makeatother

\fboxsep=0pt
\parindent=0pt

\begin{document}

\fbox{\begin{minipage}{.8\linewidth}
\begin{align}
f(x)&=1+\dfrac{1}{x+2}\\
    &=1+k(x+2) 
\end{align}
\end{minipage}}

\begin{align}
f(x)&=1+\dfrac{1}{x+2}\\
    &=1+k(x+2) 
\end{align}

{
\selectlanguage{nil}

\fbox{\begin{minipage}{.8\linewidth}
\begin{align}
f(x)&=1+\dfrac{1}{x+2}\\
    &=1+k(x+2) 
\end{align}
\end{minipage}}

}

\end{document}
jbezos commented 2 years ago

This is certainly the problem. I’m investigating possible solutions (in addition to yours, which is on the right track), as well as other possible cases where \llap and \rlap are problematic (eg, multline).

jbezos commented 2 years ago

There are some tentative fixes in release 3.68/3.69 (see https://latex3.github.io/babel/news/whats-new-in-babel-3.68.html), but sadly a more or less general solution doesn’t seem possible without effectively rewritting amsmath. My aim is to get right the following test:

\documentclass{article}

\usepackage[english, bidi=basic]{babel}
\babelprovide[main, mapdigits]{arabic}
\babelfont{rm}{FreeSerif}
\usepackage{amsmath}
\usepackage{showframe}

\def\theequation{\arabic{section}\babelsublr{.}\arabic{equation}}

\begin{document}

\begin{equation}
x^2 \quad (\mbox{إثنان three أربعة})
\end{equation}

\begin{gather}
\text{إثنان three أربعة} =
\begin{cases}
-1   &  x < 0 \\
 0   &  x = 0 \\
 1   &  x > 0
\end{cases}
\end{gather}

\begin{multline} %\tag{2}
a^2\quad (\hbox{إثنان three أربعة}) \\
= b^2
\end{multline}

\end{document}

equation is simple, but when we enter in math mode luatex resets all directions to \mathdir, and more elaborated math environments are based on a complex combination of math and hboxes. So, in all my attempts so far when the math stuff gets fixed, the text is broken (and vice versa).

ar-ams-babel

jbezos commented 2 years ago

This particular problem is now fixed, as well as many other misplaced tags and bag alignments, so I’m closing it.