latex3 / babel

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

Referencing equations doesn't use custom tags? #260

Closed ShaiAvr closed 9 months ago

ShaiAvr commented 10 months ago

I use Latex with babel for my assignments in Hebrew and I have this simple code snippet:

\documentclass{article}

\PassOptionsToPackage{no-math}{fontspec}

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

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

\babelfont[hebrew]{rm}{Calibri}
\babelfont[hebrew]{sf}{Calibri}

\usepackage{amsmath}

\begin{document}

\begin{equation}\label{Pythagoras}
a^2 + b^2 = c^2 \tag{\textasteriskcentered}
\end{equation}
משפט פיתגורס \eqref{Pythagoras}.
\end{document}

As you can see, the equation has a custom tag (*) and I reference it later. When compiling this code, the equation indeed shows with the custom tag, but the reference still uses the default tag of (1): Screenshot 2023-08-22 220057 I think it's a bug with babel since I didn't have this problem with babel disabled or with polyglossia. I also get this issue on Overleaf. If someone could provide a fix for this, I'd highly appreciate it.

jbezos commented 10 months ago

As a workaround, try with

\begin{equation}\label{Pythagoras}
a^2 + b^2 = c^2 
\makeatletter\def\@currentlabel{\textasteriskcentered}%
\tag{\textasteriskcentered}
\end{equation}
ShaiAvr commented 10 months ago

@jbezos This workaround indeed works and by wrapping it in a custom command:

\makeatletter
\newcommand*{\mytag}[1]{%
\def\@currentlabel{#1}%
\tag{#1}}
\makeatother

it becomes more usable.

After further experimentation, I found that this issue only occurs in Hebrew (and possibly other non-English languages I can't check?). Doing \selectlanguage{english} or using the \begin{otherlanguage}{english} environment fixes the issue and the references use the correct tags again.

jbezos commented 10 months ago

The problem is the printed form in RTL contexts must be wrapped somehow (with a group) to get the correct direction, and amsmath sets the label exactly when the tag is printed, which means its value is lost when exiting the group.

ShaiAvr commented 1 month ago

@jbezos I wouldn't close this issue as completed since it's still a bug in babel which requires an unintuitive workaround. I faced this bug today and then remembered I opened an issue about it on Github, so I was reminded of the solution. This can easily happen to other Hebrew document writers, and this should be fixed, or at the very least, this solution should be documented. An easy fix is just for babel to redefine \tag with this workaround as I did, but I have no experience in package development, so I don't know how practical or useful this solution is.

jbezos commented 1 month ago

Please, post a new MWE, because the original one works as expected. image

ShaiAvr commented 1 month ago

@jbezos That's weird. The original works as expected on my local machine now, but it gives the wrong result on Overleaf. Both use LuaLatex with TexLive 2023.

jbezos commented 1 month ago

It’s natural it fails in Overleaf: it’s an old version (2023/07/15 v3.92) without the fix.