0

I am using Latex to do my homework in our native language Hebrew, which is an rtl language. I am trying to migrate to babel with lualatex since I encountered annoying bugs with polyglossia and xelatex (plus, I think the syntax of babel is nicer compared to polyglossia).

I tried to compile a sample document and I found a weird issue. I tried to type an exponent in inline math mode inside an underline command and the exponent showed up on the left side instead of the right side as it should be with math exponents. It seems babel uses the default direction of the document (RTL in this case) instead of the proper direction for math inside an underline. This issue doesn't appear in regular text or in \textbf, \textit, or \emph. Here's a picture:

As you can see, only \underline produces wrong results while every other command works fine. Here's the code to generate this document:

% !TeX spellcheck = he_HE
\documentclass[a4paper, 12pt]{extarticle}

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

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

\babelfont[hebrew]{rm}[ItalicFont=*-Regular, ItalicFeatures={FakeSlant=0.3}]{Assistant}
\babelfont[hebrew]{sf}[ItalicFont=*-Regular, ItalicFeatures={FakeSlant=0.3}]{Assistant}

\AddToHook{begindocument/end}{\selectlanguage{hebrew}}

\usepackage{xparse}
\usepackage{amsmath}
\usepackage[intlimits]{esint}
\usepackage{enumitem}
\usepackage{moreenum}
\usepackage{witharrows}
\usepackage{parskip}
\usepackage{setspace}
\usepackage{physics}
\usepackage{derivative}

\NewDocumentCommand{\C}{}{\mathbb{C}}
\NewDocumentCommand{\Conjugate}{ s m }{
    \IfBooleanTF{#1}{\pqty{#2}}{{#2}}^{\ast}
}

\begin{document}
המכפלה הפנימית הסטנדרטית מעל \(\C^n\):

\textbf{המכפלה הפנימית הסטנדרטית מעל \(\C^n\):}

\textit{המכפלה הפנימית הסטנדרטית מעל \(\C^n\) :}

\emph{המכפלה הפנימית הסטנדרטית מעל \(\C^n\) :}

\underline{המכפלה הפנימית הסטנדרטית מעל \(\C^n\):}
\[ \braket{u}{v} = \sum_{j = 1}^{n} \Conjugate{u}_j v_j \]
\end{document}

Can anyone explain why math expressions don't render in the proper direction in \underline and how to fix this?

1 Answer 1

3

Please provide shorter examples to the demonstrate a problem, it will help people to help you.

The problem can be reduced to the following code:

\documentclass{article}

\usepackage[bidi=basic,hebrew,provide=*]{babel}

\babelfont[hebrew]{rm}{David CLM}

\begin{document}
    \underline{שלום $C^n$}
\end{document}

enter image description here

In section 1.24 of babel's manual it is stated that adjustments for \underline are needed with luatex, and you can use these adjustments with the layout = extras option.

The problem is that a few years ago \underline was robustified, and babel does not take that into account, so the option is broken. Luckily there an easy fix, just replace \bbl@sreplace, which is a command babel use internally to patch commands, but is not aware of robust commands, with \xpatchcmd of the xpatch package:

\documentclass{article}

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

\babelfont[hebrew]{rm}{David CLM}

\usepackage{xpatch}
\makeatletter
\xpatchcmd\underline{$\@@underline}{\bgroup\bbl@nextfake$\@@underline}{}{}
\xpatchcmd\underline{$\relax}{$\egroup\relax}{}{}
\makeatother

\begin{document}
    \underline{שלום $C^n$}
\end{document}

or even witout ptaching, you can redefine \underline:

\documentclass{article}

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

\babelfont[hebrew]{rm}{David CLM}

\makeatletter
\DeclareRobustCommand\underline[1]{%
    \relax
    \ifmmode\@@underline{#1}%
    \else 
    \bgroup\bbl@nextfake$\@@underline{\hbox{#1}}\m@th$\egroup\relax\fi} 
\makeatother

\begin{document}
    \underline{שלום $C^n$}
\end{document}

enter image description here

Note that I added layout = tabular to babel's options, and this is important. If the list of layouts is empty babel does not define \bbl@nextfake, so you will need to use at least one layout.

I have opened a ticket, probably it will be fixed soon.

As a side note, the line \AddToHook{begindocument/end}{\selectlanguage{hebrew}} does not make much sense if you declared hebrew as the main language.

3
  • Unfortunately, the fix you provided causes the direction to reverse in display mode. It fixes underline, but now the important equations in display mode are broken.
    – Shai Avr
    Commented Jul 1, 2023 at 16:01
  • @ShaiAvr, see my edit
    – Udi Fogiel
    Commented Jul 1, 2023 at 16:35
  • Thank you. Works perfectly.
    – Shai Avr
    Commented Jul 1, 2023 at 17:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .