latex3 / babel

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

eqref is misaligned when placed above equal sign in align and gather environments #301

Open ShaiAvr opened 2 weeks ago

ShaiAvr commented 2 weeks ago

I write my documents in Hebrew and I noticed a weird issue. If I try to put a reference above an equal sign in the align or gather environments, it's not aligned correctly. This problem only happens in Hebrew. Switching the current language to English fixes it:

Screenshot 2024-06-12 180509 The code for this example:

\documentclass{article}

\PassOptionsToPackage{no-math}{fontspec}

\usepackage[bidi=basic, layout=tabular]{babel}

\babelprovide[main, import, alph=letters, Alph=letters]{hebrew}
\babelprovide[import]{english}

% Font setup
\babelfont[hebrew]{rm}[Renderer=HarfBuzz]{Assistant}
\babelfont[hebrew]{sf}[Renderer=HarfBuzz]{Assistant}

\usepackage{amsmath}

\begin{document}
\begin{equation}\label{eq:he-pythagoras}
    a^2 + b^2 = c^2
\end{equation}
לפי משוואה
\eqref{eq:he-pythagoras}
נסיק כי
\begin{align*}
c^4 \overset{\scriptscriptstyle\eqref{eq:he-pythagoras}}{=} (a^2 + b^2)^2
\end{align*}
נמשיך

\selectlanguage{english}
\begin{equation}\label{eq:en-pythagoras}
    a^2 + b^2 = c^2
\end{equation}
According to equation \eqref{eq:en-pythagoras}, we can conclude that:
\begin{align*}
    c^4 \overset{\scriptscriptstyle\eqref{eq:en-pythagoras}}{=} (a^2 + b^2)^2
\end{align*}
Let's continue
\end{document}

When I was working on Overleaf, I would wrap the align/gather environments with the \babelsublr command to fix this, but for a weird reason, this workaround didn't work when I tried to transition to a local compiler and caused the reference to break. I asked this on tex.stackexchange and got a working workaround:

\documentclass{article}

\PassOptionsToPackage{no-math}{fontspec}

\usepackage[bidi=basic, layout=tabular]{babel}

\babelprovide[main, import, alph=letters, Alph=letters]{hebrew}
\babelprovide[import]{english}

% Font setup
\babelfont[hebrew]{rm}[Renderer=HarfBuzz]{Assistant}
\babelfont[hebrew]{sf}[Renderer=HarfBuzz]{Assistant}

\usepackage{amsmath}

\makeatletter
\NewDocumentCommand{\exref}{ m }{%
    \let\bbl@ams@lap\relax%
    \eqref{#1}%
}%
\makeatother

\begin{document}
\begin{equation}\label{eq:he-pythagoras}
    a^2 + b^2 = c^2
\end{equation}
לפי משוואה
\exref{eq:he-pythagoras}
נסיק כי
\begin{align*}
c^4 \overset{\scriptscriptstyle\exref{eq:he-pythagoras}}{=} (a^2 + b^2)^2
\end{align*}
נמשיך

\selectlanguage{english}
\begin{equation}\label{eq:en-pythagoras}
    a^2 + b^2 = c^2
\end{equation}
According to equation \exref{eq:en-pythagoras}, we can conclude that:
\begin{align*}
    c^4 \overset{\scriptscriptstyle\exref{eq:en-pythagoras}}{=} (a^2 + b^2)^2
\end{align*}
Let's continue
\end{document}

which produces:

Screenshot 2024-06-12 181122 According to Javier's answer to my question, it looks like babel patches an internal macro in some amsmath environments to correctly align the tag. \eqref uses this macro in its original form in the text, but in the context of my example the modified form is the active one.

In other words, it seems to be a bug with babel and this should be fixed.