|
From: <mi...@us...> - 2022-11-04 10:55:08
|
Revision: 9213
http://sourceforge.net/p/docutils/code/9213
Author: milde
Date: 2022-11-04 10:55:05 +0000 (Fri, 04 Nov 2022)
Log Message:
-----------
MathML: support "mod" notation for modulo operation/arithmetic.
TeX to HTML/css support still missing, cf. [feature-requests:#93].
Modified Paths:
--------------
trunk/docutils/HISTORY.txt
trunk/docutils/docs/ref/rst/mathematics.txt
trunk/docutils/docutils/utils/math/latex2mathml.py
Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt 2022-11-04 10:54:51 UTC (rev 9212)
+++ trunk/docutils/HISTORY.txt 2022-11-04 10:55:05 UTC (rev 9213)
@@ -52,7 +52,10 @@
- Fix previous_sibling() method that led to invalid HTML in some cases
(cf. patch #195).
+* docutils/utils/math/latex2mathml.py
+ - Support "mod" notation for modulo operation/modulus arithmetic.
+
* test/coverage.sh
Removed. Use the coverage.py_ project instead,
Modified: trunk/docutils/docs/ref/rst/mathematics.txt
===================================================================
--- trunk/docutils/docs/ref/rst/mathematics.txt 2022-11-04 10:54:51 UTC (rev 9212)
+++ trunk/docutils/docs/ref/rst/mathematics.txt 2022-11-04 10:55:05 UTC (rev 9213)
@@ -10,7 +10,7 @@
:abstract: Docutils supports mathematical content with a `"math"
directive`__ and `role`__. The input format is *LaTeX math
- syntax*\ [#math-syntax]_ with support for Unicode symbols.
+ syntax*\ [#math-syntax]_ with support for literal Unicode symbols.
.. sectnum::
.. contents::
@@ -313,6 +313,7 @@
`\mho` ``\mho`` `\eth` ``\eth`` `\Bbbk` ``\Bbbk``
============= =============== ========== ============ ========== ============ =========== =============
+
Mathematical Alphabets
----------------------
@@ -414,6 +415,7 @@
.. [#] Punctuation (not ratio):
Compare spacing in `a\colon b\to c` to `a:b = c`.
+
Relation symbols
----------------
@@ -534,6 +536,7 @@
and ``\varsupsetneqq`` are not supported by LateX2MathML, as there is no
corresponding Unicode character.
+
Variable-sized operators
------------------------
.. class:: colwidths-auto
@@ -552,6 +555,7 @@
\int_0^1f(x)\,dx \qquad
\prod_{i=1}^{10} b_i \ldots
+
Notations
=========
@@ -560,6 +564,7 @@
See `Accents and embellishments`_.
+
Extensible arrows
-----------------
@@ -574,6 +579,7 @@
.. math:: A \xleftarrow{n+\mu-1} B \xrightarrow[T]{n\pm i-1} C
+
Affixing symbols to other symbols
---------------------------------
@@ -620,6 +626,7 @@
\phantom{-}1 & x>0
\end{cases}
+
Spacing commands
----------------
@@ -659,6 +666,31 @@
width or height of the argument. They are not supported with `math_output`_
MathML.
+
+Modular arithmetic and modulo operation
+---------------------------------------
+
+The commands ``\bmod``, ``\pmod``, ``\mod``, and ``\pod`` deal with the
+special spacing conventions of the “mod” notation. [#]_
+
+.. class:: colwidths-auto
+
+ ========= =========================== =========================
+ command example result
+ ========= =========================== =========================
+ ``\bmod`` ``\gcd(n,m \bmod n)`` `\gcd(n,m \bmod n)`
+ ``\pmod`` ``x\equiv y \pmod b`` `x\equiv y \pmod b`
+ ``\mod`` ``x\equiv y \mod c`` `x\equiv y \mod c`
+ ``\pod`` ``x\equiv y \pod d`` `x\equiv y \pod d`
+ .. ``\operatorname{mod}(m,n)`` `\operatorname{mod}(m,n)`
+ ========= =========================== =========================
+
+.. [#] Currently `not supported`__ by the "HTML" math_output_ option
+ of the HTML writer.
+
+__ https://sourceforge.net/p/docutils/feature-requests/93/
+
+
Roots
-----
@@ -672,6 +704,7 @@
.. ``\sqrt\frac{1}{2}`` `\sqrt\frac{1}{2}`
========= ==================== ==================
+
Boxed formulas
--------------
@@ -680,7 +713,6 @@
.. math:: \boxed{\eta \leq C(\delta(\eta) +\Lambda_M(0,\delta))}
-
Fractions and related constructions
===================================
@@ -782,6 +814,7 @@
`\left(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\right)`
vs. `\Bigl(\begin{smallmatrix} a & b \\ c & d \end{smallmatrix}\Bigr)`.
+
Text
====
@@ -803,25 +836,7 @@
.. TODO ignore {}, handle text-mode commands
-
-``\mod`` and its relatives
---------------------------
-Commands ``\mod``, ``\bmod``, ``\pmod``, ``\pod`` deal with the special
-spacing conventions of “mod” notation.
-``\mod`` and ``\pod`` are variants of ``\pmod`` preferred by some
-authors; ``\mod`` omits the parentheses, whereas ``\pod`` omits the
-“mod” and retains the parentheses.
-
-TODO:
- Currently not supported by the "HTML" and "MathML" math_output_
- options of the HTML writer (cf. `feature-request #93
- <https://sourceforge.net/p/docutils/feature-requests/93/>`__).
-
-.. \gcd(n,m\bmod n) ;\quad x\equiv y\pmod b
- ;\quad x\equiv y\mod c ;\quad x\equiv y\pod d
-
-
Integrals and sums
==================
@@ -834,6 +849,7 @@
move to index positions: `\lim_{n\to\infty} \sum_1^n \frac{1}{n}`.
+
Altering the placement of limits
--------------------------------
@@ -903,7 +919,6 @@
Tests
-----
-
Font changes
~~~~~~~~~~~~
@@ -959,6 +974,7 @@
.. math:: \text{das ist ein {toller} text (unescaped \{ and \} is
ignored by LaTeX)}
+
Big delimiters and symbols
~~~~~~~~~~~~~~~~~~~~~~~~~~
Compare automatic sizing with fixed sizes:
Modified: trunk/docutils/docutils/utils/math/latex2mathml.py
===================================================================
--- trunk/docutils/docutils/utils/math/latex2mathml.py 2022-11-04 10:54:51 UTC (rev 9212)
+++ trunk/docutils/docutils/utils/math/latex2mathml.py 2022-11-04 10:55:05 UTC (rev 9213)
@@ -73,7 +73,16 @@
# Function with limits: 'lim', 'sup', 'inf', 'max', 'min':
# use <mo> to allow "movablelimits" attribute (see below).
+# modulo operator/arithmetic
+modulo_functions = {
+ # cmdname: (binary, named, parentheses, padding)
+ 'bmod': (True, True, False, '0.278em'), # a mod n
+ 'pmod': (False, True, True, '0.444em'), # a (mod n)
+ 'mod': (False, True, False, '0.667em'), # a mod n
+ 'pod': (False, False, True, '0.444em'), # a (n)
+ }
+
# math font selection -> <mi mathvariant=...> or <mstyle mathvariant=...>
math_alphabets = {
# 'cmdname': 'mathvariant value' # package
@@ -433,6 +442,16 @@
xml.extend(['\n', ' ' * level])
return xml
+ def is_block(self):
+ """Return true, if `self` or a parent has ``display='block'``."""
+ try:
+ return self['display'] == 'block'
+ except KeyError:
+ try:
+ return self.parent.is_block()
+ except AttributeError:
+ return False
+
# >>> n2 = math(mn(2))
# >>> n2
# math(mn(2))
@@ -442,13 +461,23 @@
# 1
# >>> eq3 = math(id='eq3', display='block')
# >>> eq3
-# math(display='block', id='eq3')
+# math(id='eq3', display='block')
# >>> eq3.toprettyxml()
-# '<math display="block" id="eq3">\n</math>'
+# '<math id="eq3" display="block">\n</math>'
# >>> len(eq3)
# 0
# >>> math(CLASS='bold').xml_starttag()
# '<math class="bold">'
+# >>> n2.is_block()
+# False
+# >>> node = n2.append(mrow())
+# >>> node.is_block()
+# False
+# >>> eq3.is_block()
+# True
+# >>> node = eq3.append(mrow())
+# >>> node.is_block()
+# True
class mtable(math): pass
@@ -945,6 +974,26 @@
node = node.append(mo('\u2061')) # ⁡
return node, string
+ if name in modulo_functions:
+ (binary, named, parentheses, padding) = modulo_functions[name]
+ if binary:
+ node = node.append(mo('mod', lspace=padding, rspace=padding))
+ return node, string
+ # left padding
+ if node.is_block():
+ padding = '1em'
+ node = node.append(mspace(width=padding))
+ if parentheses:
+ node = node.append(mo('(', stretchy=False))
+ if named:
+ node = node.append(mi('mod'))
+ node = node.append(mspace(width='0.333em'))
+ arg, string = tex_token_or_group(string)
+ node = parse_latex_math(node, arg)
+ if parentheses:
+ node = node.append(mo(')', stretchy=False))
+ return node, string
+
if name in math_alphabets:
if name == 'boldsymbol':
attributes = {'class': 'boldsymbol'}
@@ -1212,7 +1261,7 @@
# >>> handle_cmd('mathrm', math(), '{out} = 3')
# (math(mi('out', mathvariant='normal')), ' = 3')
# >>> handle_cmd('overline', math(), '{981}')
-# (mover(mo('¯', accent=True), switch=True, accent=False), '{981}')
+# (mover(mo('_', accent=True), switch=True, accent=False), '{981}')
# >>> handle_cmd('bar', math(), '{x}')
# (mover(mo('ˉ', stretchy=False), switch=True), '{x}')
# >>> handle_cmd('xleftarrow', math(), r'[\alpha]{10}')
@@ -1366,7 +1415,7 @@
# </math>
# >>> print(tex2mathml(r'a & b \\ c & d', inline=False))
# <math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
-# <mtable class="align" columnalign="right left" columnspacing="0" displaystyle="true">
+# <mtable class="align" displaystyle="true" columnalign="right left" columnspacing="0">
# <mtr>
# <mtd>
# <mi>a</mi>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|