|
From: <mi...@us...> - 2021-05-20 12:24:26
|
Revision: 8754
http://sourceforge.net/p/docutils/code/8754
Author: milde
Date: 2021-05-20 12:24:25 +0000 (Thu, 20 May 2021)
Log Message:
-----------
MathML fix multi-digit number literals. Support optional arg for \sqrt.
Place consecutive digits and comma/dot in a common <mn> element.
Support the optinal argument to specify a degree of a radical.
Modified Paths:
--------------
trunk/docutils/docs/user/mathematics.txt
trunk/docutils/docutils/utils/math/latex2mathml.py
Modified: trunk/docutils/docs/user/mathematics.txt
===================================================================
--- trunk/docutils/docs/user/mathematics.txt 2021-05-20 12:24:07 UTC (rev 8753)
+++ trunk/docutils/docs/user/mathematics.txt 2021-05-20 12:24:25 UTC (rev 8754)
@@ -180,18 +180,15 @@
---------------------
Unless you indicate otherwise, delimiters in math formulas remain at the
standard size regardless of the height of the enclosed material. To get
-adaptable sizes, use ``\left`` and ``\right`` prefixes.
+adaptable sizes, use ``\left`` and ``\right`` prefixes, for example
+`g(A,B,Y) = f \left(A,B,X=h^{[X]}(Y)\right)` or
-.. math::
+.. math:: a_n = \left(\frac{1}{2}\right)^n
- g(A,B,Y) = f \left(A,B,X=h^{[X]}(Y)\right)
-
Use ``.`` for "empty" delimiters:
-.. math::
+.. math:: A = \left . \frac{1}{1-n}\, \right |_{n=0}^\infty
- A = \left . \frac{1}{1-n}\, \right |_{n=0}^\infty
-
The following symbols extend when used with ``\left`` and ``\right``:
Pairing delimiters
@@ -225,10 +222,9 @@
The use of ``|`` and ``\|`` for pairs of vertical bars may produce
incorrect spacing, e.g., ``|k|=|-k|`` produces `|k| = |−k|` and
-``|\sin(x)|`` produces `|\sin(x)|`. The pairing delimiters, e.g.
-`\lvert -k\rvert`
-and `\lvert\sin(x)\rvert`, prevent this problem (in LaTeX and
-MathJax).
+``|\sin(x)|`` produces `|\sin(x)|`. The pairing delimiters, e.g.
+`\lvert -k\rvert` and `\lvert\sin(x)\rvert`, prevent this problem
+(in LaTeX and MathJax).
Vertical Arrows
~~~~~~~~~~~~~~~
@@ -437,12 +433,12 @@
`\nleftarrow` ``\nleftarrow`` `\nrightarrow` ``\nrightarrow``
`\nLeftarrow` ``\nLeftarrow`` `\nRightarrow` ``\nRightarrow``
`\nleftrightarrow` ``\nleftrightarrow`` `\nLeftrightarrow` ``\nLeftrightarrow``
- `\nwarrow` ``\nwarrow`` `\nearrow` ``\nearrow``
- `\swarrow` ``\swarrow`` `\searrow` ``\searrow``
+ `\nwarrow` ``\nwarrow`` `\nearrow` ``\nearrow``
+ `\swarrow` ``\swarrow`` `\searrow` ``\searrow``
`\twoheadleftarrow` ``\twoheadleftarrow`` `\twoheadrightarrow` ``\twoheadrightarrow``
`\upharpoonleft` ``\upharpoonleft`` `\upharpoonright` ``\upharpoonright``
`\downharpoonleft` ``\downharpoonleft`` `\downharpoonright` ``\downharpoonright``
- `\upuparrows` ``\upuparrows`` `\downdownarrows` ``\downdownarrows``
+ `\upuparrows` ``\upuparrows`` `\downdownarrows` ``\downdownarrows``
====================== ======================== ===================== =======================
@@ -561,7 +557,7 @@
A \xleftarrow{n+\mu-1} B \xrightarrow[T]{n\pm i-1} C
-results in
+results in
.. math:: A \xleftarrow{n+\mu-1} B \xrightarrow[T]{n\pm i-1} C
@@ -624,13 +620,13 @@
.. class:: colwidths-auto
- ===================== ============================== ============================
- command example result
- ===================== ============================== ============================
- ``\sqrt`` ``\sqrt{x^2-1}`` `\sqrt{x^2-1}`
- ``\frac`` ``\frac{1}{2}`` `\frac{1}{2}`
- ``\left``, ``\right`` ``\left(\frac{1}{2}\right)^n`` `\left(\frac{1}{2}\right)^n`
- ===================== ============================== ============================
+ ========= ==================== ==================
+ command example result
+ ========= ==================== ==================
+ ``\sqrt`` ``\sqrt{x^2-1}`` `\sqrt{x^2-1}`
+ .. ``\sqrt[3n]{x^2-1}`` `\sqrt[3n]{x^2-1}`
+ ``\frac`` ``\frac{1}{1-x}`` `\frac{1}{1-x}`
+ ========= ==================== ==================
Text
====
@@ -648,8 +644,6 @@
Currently, math in text is not supported by LaTeX2MathML.
-
-
ToDo
====
@@ -657,10 +651,9 @@
* Math inside text: ``n - 1 \text{if $n$ is odd}``.
* Remove circular refs.
-* Decimal numbers.
-* Implement ``\circledS``? (in short-math-guide.pdf but not in
- the Unicode `Mathematical Character Repertoire`__)
-* ``\DeclareMathOperator``
+* Implement ``\circledS``? Ⓢ (and other characters
+ in the short-math-guide.pdf but not in the
+ Unicode `Mathematical Character Repertoire`__)
__ http://www.unicode.org/reports/tr25/
Modified: trunk/docutils/docutils/utils/math/latex2mathml.py
===================================================================
--- trunk/docutils/docutils/utils/math/latex2mathml.py 2021-05-20 12:24:07 UTC (rev 8753)
+++ trunk/docutils/docutils/utils/math/latex2mathml.py 2021-05-20 12:24:25 UTC (rev 8754)
@@ -109,12 +109,12 @@
'nsubseteqq': u'\u2AC5\u0338', # ⫅̸
'nsupseteqq': u'\u2AC6\u0338', # ⫆̸
# pairing delimiters
- 'lvert': '|',
+ 'lvert': u'|',
'lVert': u'\u2016', # ‖
- 'rvert': '|',
+ 'rvert': u'|',
'rVert': u'\u2016',
# use <mo> to allow "movablelimits" attribute
- 'lim': u'lim',
+ 'lim': u'lim',
})
# special cases
@@ -140,6 +140,7 @@
'iint', 'int', 'oiint', 'oint', 'ointctrclockwise',
'prod', 'sqint', 'sum', 'varointclockwise')
] + ['lim']
+ # TODO: 'sup', 'inf', 'max', 'min
# pre-composed characters for negated symbols
# see https://www.w3.org/TR/xml-entity-names/#combining
@@ -460,7 +461,7 @@
# >>> msubsup(mi('base'), mi('super'), mi('sub'), switch=True)
# msubsup(mi('base'), mi('sub'), mi('super'))
-class mroot(math):
+class mroot(MathScriptOrLimit):
nchildren = 2
class mfrac(math):
@@ -503,6 +504,27 @@
# >>> tex_cmdname('') # empty string
# ('', '')
+def tex_number(string):
+ """Return first number literal and remainder of `string`.
+
+ >>> tex_number('123.4')
+ ('123.4', '')
+
+ """
+ m = re.match(r'([0-9.,]+)(.*)', string)
+ if m is None:
+ return '', string
+ return m.group(1), m.group(2)
+
+# Test:
+#
+# >>> tex_number(' 123.4')
+# ('', ' 123.4')
+# >>> tex_number('23,400')
+# ('23,400', '')
+# >>> tex_number('1 000.4')
+# ('1', ' 000.4')
+
def tex_token(string):
"""Return first simple TeX token and remainder of `string`.
@@ -579,6 +601,8 @@
# SyntaxError: Could not extract optional argument from '[group with [nested group]]'
+
+
def parse_latex_math(node, string):
"""Append MathML conversion of `string` to `node`.
@@ -618,7 +642,8 @@
elif c.isalpha():
node = node.append(mi(c))
elif c.isdigit():
- node = node.append(mn(c))
+ number, string = tex_number(string)
+ node = node.append(mn(c+number))
elif c in "/()[]|":
node = node.append(mo(c, stretchy='false'))
# use dedicated mathematical operator characters
@@ -636,6 +661,8 @@
# math(xmlns='http://www.w3.org/1998/Math/MathML')
# >>> parse_latex_math(math(), ' \\sqrt{ \\alpha}')
# math(msqrt(mi('α')))
+# >>> parse_latex_math(math(), '23.4x')
+# math(mn('23.4'), mi('x'))
# >>> parse_latex_math(math(), '\\sqrt 2 \\ne 3')
# math(msqrt(mn('2')), mo('≠'), mn('3'))
# >>> parse_latex_math(math(), '\\sqrt{2 + 3} < 3')
@@ -800,11 +827,18 @@
# ==================================
if name == 'sqrt':
- new_node = msqrt()
- if string.startswith('{'): # argument is a group
- string = string[1:] # mrow implied, skip opening bracket
- else: # no group, enclose only one element
- new_node.nchildren = 1
+ # TODO: optional arg -> <mroot> <mn>2</mn> <mn>3</mn> </mroot>
+ degree, string = tex_optarg(string)
+ if degree:
+ indexnode = mrow()
+ parse_latex_math(indexnode, degree)
+ new_node = mroot(indexnode, switch=True)
+ else:
+ new_node = msqrt()
+ if string.startswith('{'): # argument is a group
+ string = string[1:] # mrow implied, skip opening bracket
+ else: # no group, enclose only one element
+ new_node.nchildren = 1
node.append(new_node)
return new_node, string
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|