SF.net SVN: docutils:[9534 ] trunk/docutils

milde--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9534
          http://sourceforge.net/p/docutils/code/9534
Author:   milde
Date:     2024-02-01 13:03:52 +0000 (Thu, 01 Feb 2024)
Log Message:
-----------
LaTeX to MathML: Fix "mathvariant" handling for numbers.

Conversion of \mathtt{0.12} did not use monospaced digits.

Do a per-character lookup/replacement with
"alphanumical mathematical chars" for the "text" of `<mn>`.

While identifiers are usually put in separate `<mi>` elements
and more than one letter in a `<mi>` indicates a function name
that should not change fonts,
numbers in `<mn>` consisting of several digits should be
changed according to the given "mathvariant".

Modified Paths:
--------------
    trunk/docutils/docutils/utils/math/latex2mathml.py
    trunk/docutils/test/functional/expected/mathematics_mathml.html
    trunk/docutils/test/test_utils/test_math/test_tex2mathml_extern.py

Modified: trunk/docutils/docutils/utils/math/latex2mathml.py
===================================================================
--- trunk/docutils/docutils/utils/math/latex2mathml.py	2024-02-01 13:03:41 UTC (rev 9533)
+++ trunk/docutils/docutils/utils/math/latex2mathml.py	2024-02-01 13:03:52 UTC (rev 9534)
@@ -988,12 +988,11 @@
     if name == 'mathscr':
         attributes['class'] = 'mathscr'
     arg, string = tex_token_or_group(string)
-    # Shortcut for text arg like \mathrm{out}
-    # with more than one letter, <mi> defaults to "normal" font
+    # Shortcut for text arg like \mathrm{out} with more than one letter:
     if name == 'mathrm' and arg.isalpha() and len(arg) > 1:
-        node = node.append(mi(arg))
+        node = node.append(mi(arg))  # <mi> defaults to "normal" font
         return node, string
-    # Parse as <style> node children
+    # Parse into an <mrow>
     container = mrow(**attributes)
     node.append(container)
     parse_latex_math(container, arg)
@@ -1000,10 +999,14 @@
     a2ch = getattr(mathalphabet2unichar,
                    name.replace('mathscr', 'mathcal'), {})
     for subnode in container.iter():
-        if isinstance(subnode, (mi, mn)):
+        if isinstance(subnode, mn):
+            # a number may consist of more than one digit
+            subnode.text = ''.join(a2ch.get(ch, ch) for ch in subnode.text)
+        elif isinstance(subnode, mi):
+            # don't convert multi-letter identifiers (functions)
             subnode.text = a2ch.get(subnode.text, subnode.text)
-        if isinstance(subnode, mi) and name == 'mathrm' and subnode.text.isalpha():
-            subnode.set('mathvariant', 'normal')
+            if name == 'mathrm' and subnode.text.isalpha():
+                subnode.set('mathvariant', 'normal')
     return container.close(), string
 
 # >>> handle_math_alphabet('mathrm', math(), '\\alpha')

Modified: trunk/docutils/test/functional/expected/mathematics_mathml.html
===================================================================
--- trunk/docutils/test/functional/expected/mathematics_mathml.html	2024-02-01 13:03:41 UTC (rev 9533)
+++ trunk/docutils/test/functional/expected/mathematics_mathml.html	2024-02-01 13:03:52 UTC (rev 9534)
@@ -1751,7 +1751,7 @@
 <tr><td><p><span class="docutils literal">\mathtt</span></p></td>
 <td><p><span class="docutils literal">\mathtt{0.12}</span></p></td>
 <td><p><math xmlns="http://www.w3.org/1998/Math/MathML">
-  <mn>0.12</mn>
+  <mn>𝟶.𝟷𝟸</mn>
 </math></p></td>
 </tr>
 </tbody>

Modified: trunk/docutils/test/test_utils/test_math/test_tex2mathml_extern.py
===================================================================
--- trunk/docutils/test/test_utils/test_math/test_tex2mathml_extern.py	2024-02-01 13:03:41 UTC (rev 9533)
+++ trunk/docutils/test/test_utils/test_math/test_tex2mathml_extern.py	2024-02-01 13:03:52 UTC (rev 9534)
@@ -21,7 +21,7 @@
 
 # Testing the conversion with external converters requires additional
 # ressources (latexml, blahtexml, ttm, pandoc) and is SLOW
-# (ca. 27.467s without testing "latexml", much slower with "latexml").
+# (ca. 27s without testing "latexml" and 13 min with "latexml").
 # Therefore, tests are skipped unless run as stand-alone module:
 
 if __name__ != '__main__':

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



_______________________________________________
Docutils-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/docutils-checkins
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.