latex3 / babel

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

The cancel package doesn't work sometimes in Hebrew #279

Closed jbezos closed 4 months ago

jbezos commented 5 months ago

See https://tex.stackexchange.com/questions/708472/the-cancel-package-doesnt-work-sometimes-in-hebrew.

\documentclass[a4paper, 12pt]{article}

\PassOptionsToPackage{no-math}{fontspec}

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

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

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

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

\usepackage[hyperref, table, x11names]{xcolor}
\usepackage{amsmath}
\usepackage[thicklines]{cancel}

% Cancel settings
\renewcommand{\CancelColor}{\color{red}}

\begin{document}
\[ \cancel{x} + 1 - \cancel{x} = 1 \]
\[ \cancel{
    (\nabla{\psi}^{\ast})
    \cdot
    (\nabla\psi)
} \]
\end{document}
Udi-Fogiel commented 5 months ago

This following naive patch seems to work, but I did not think much about it:

diff --git a/babel.dtx b/babel.dtx
index ebfea61..859ddb9 100644
--- a/babel.dtx
+++ b/babel.dtx
@@ -23296,6 +23296,18 @@ local ranges = Babel.ranges

 local DIR = node.id('dir')
 local GLYPH = node.id('glyph')
+local tenln = font.id('tenln')
+local tenlnw = font.id('tenlnw')
+local tencirc = font.id('tencirc')
+local tencircw = font.id('tencircw')
+
+local function glyph_not_picture_font(node)
+   local id = node.id
+   if id ~= GLYPH then
+       return false
+   else
+       local font = node.font
+       return font ~= tenln and font ~= tenlnw
+           and font ~= tencirc and font ~= tencircw
+   end
+end

 local function insert_implicit(head, state, outer)
   local new_state = state
@@ -23384,7 +23396,7 @@ function Babel.bidi(head, ispar, hdir)
     -- current one is not added until we start processing the neutrals.

     -- three cases: glyph, dir, otherwise
-    if item.id == GLYPH
+    if glyph_not_picture_font(item)
        or (item.id == 7 and item.subtype == 2) then

       local d_font = nil
@@ -23513,7 +23525,7 @@ function Babel.bidi(head, ispar, hdir)
         temp = 'on'     -- W6
       end
       for e = first_et, #nodes do
-        if nodes[e][1].id == GLYPH then nodes[e][2] = temp end
+        if glyph_not_picture_font(nodes[e][1]) then nodes[e][2] = temp end
       end
       first_et = nil
       has_en = false
@@ -23553,7 +23565,7 @@ function Babel.bidi(head, ispar, hdir)
       temp = 'on'     -- W6
     end
     for e = first_et, #nodes do
-      if nodes[e][1].id == GLYPH then nodes[e][2] = temp end
+      if glyph_not_picture_font(nodes[e][1]) then nodes[e][2] = temp end
     end
   end

@@ -23589,7 +23601,7 @@ function Babel.bidi(head, ispar, hdir)
       for r = first_on, q - 1 do
         nodes[r][2] = temp
         item = nodes[r][1]    -- MIRRORING
-        if Babel.mirroring_enabled and item.id == GLYPH
+        if Babel.mirroring_enabled and glyph_not_picture_font(item)
              and temp == 'r' and characters[item.char] then
           local font_mode = ''
           if item.font > 0 and font.fonts[item.font].properties then
jbezos commented 5 months ago

@Udi-Fogiel Thanks. I'll try to take a look when I have a little time.

jbezos commented 4 months ago

@Udi-Fogiel After investigating, I realized \cancel doesn’t use the picture environment and builds the lines and vectors directly. Anyway, clearly mirroring must be disabled with ‘symbol’ fonts, and I’m even wondering if OML and OMS fonts should be included, too (or even U and OMX). For the moment, I’ll deal, following your suggestion, with the picture fonts.

Udi-Fogiel commented 4 months ago

@jbezos

and I’m even wondering if OML and OMS fonts should be included, too (or even U and OMX). For the moment, I’ll deal, following your suggestion, with the picture fonts.

Maybe it will be useful to check for fonts with script=math as well? might help with the interaction between \hbox and equations.