kr.motd.maven.sphinx.dist.docutils.utils.math.latex2mathml.py Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sphinx-maven-plugin Show documentation
Show all versions of sphinx-maven-plugin Show documentation
Maven plugin that creates the site with Sphinx
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# :Id: $Id: latex2mathml.py 7668 2013-06-04 12:46:30Z milde $
# :Copyright: © 2010 Günter Milde.
# Based on rst2mathml.py from the latex_math sandbox project
# © 2005 Jens Jørgen Mortensen
# :License: Released under the terms of the `2-Clause BSD license`_, in short:
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.
# This file is offered as-is, without any warranty.
#
# .. _2-Clause BSD license: http://www.spdx.org/licenses/BSD-2-Clause
"""Convert LaTex math code into presentational MathML"""
# Based on the `latex_math` sandbox project by Jens Jørgen Mortensen
import docutils.utils.math.tex2unichar as tex2unichar
# TeX spacing combining
over = {'acute': u'\u00B4', # u'\u0301',
'bar': u'\u00AF', # u'\u0304',
'breve': u'\u02D8', # u'\u0306',
'check': u'\u02C7', # u'\u030C',
'dot': u'\u02D9', # u'\u0307',
'ddot': u'\u00A8', # u'\u0308',
'dddot': u'\u20DB',
'grave': u'`', # u'\u0300',
'hat': u'^', # u'\u0302',
'mathring': u'\u02DA', # u'\u030A',
'overleftrightarrow': u'\u20e1',
# 'overline': # u'\u0305',
'tilde': u'\u02DC', # u'\u0303',
'vec': u'\u20D7'}
Greek = { # Capital Greek letters: (upright in TeX style)
'Phi':u'\u03a6', 'Xi':u'\u039e', 'Sigma':u'\u03a3',
'Psi':u'\u03a8', 'Delta':u'\u0394', 'Theta':u'\u0398',
'Upsilon':u'\u03d2', 'Pi':u'\u03a0', 'Omega':u'\u03a9',
'Gamma':u'\u0393', 'Lambda':u'\u039b'}
letters = tex2unichar.mathalpha
special = tex2unichar.mathbin # Binary symbols
special.update(tex2unichar.mathrel) # Relation symbols, arrow symbols
special.update(tex2unichar.mathord) # Miscellaneous symbols
special.update(tex2unichar.mathop) # Variable-sized symbols
special.update(tex2unichar.mathopen) # Braces
special.update(tex2unichar.mathclose) # Braces
special.update(tex2unichar.mathfence)
sumintprod = ''.join([special[symbol] for symbol in
['sum', 'int', 'oint', 'prod']])
functions = ['arccos', 'arcsin', 'arctan', 'arg', 'cos', 'cosh',
'cot', 'coth', 'csc', 'deg', 'det', 'dim',
'exp', 'gcd', 'hom', 'inf', 'ker', 'lg',
'lim', 'liminf', 'limsup', 'ln', 'log', 'max',
'min', 'Pr', 'sec', 'sin', 'sinh', 'sup',
'tan', 'tanh',
'injlim', 'varinjlim', 'varlimsup',
'projlim', 'varliminf', 'varprojlim']
mathbb = {
'A': u'\U0001D538',
'B': u'\U0001D539',
'C': u'\u2102',
'D': u'\U0001D53B',
'E': u'\U0001D53C',
'F': u'\U0001D53D',
'G': u'\U0001D53E',
'H': u'\u210D',
'I': u'\U0001D540',
'J': u'\U0001D541',
'K': u'\U0001D542',
'L': u'\U0001D543',
'M': u'\U0001D544',
'N': u'\u2115',
'O': u'\U0001D546',
'P': u'\u2119',
'Q': u'\u211A',
'R': u'\u211D',
'S': u'\U0001D54A',
'T': u'\U0001D54B',
'U': u'\U0001D54C',
'V': u'\U0001D54D',
'W': u'\U0001D54E',
'X': u'\U0001D54F',
'Y': u'\U0001D550',
'Z': u'\u2124',
}
mathscr = {
'A': u'\U0001D49C',
'B': u'\u212C', # bernoulli function
'C': u'\U0001D49E',
'D': u'\U0001D49F',
'E': u'\u2130',
'F': u'\u2131',
'G': u'\U0001D4A2',
'H': u'\u210B', # hamiltonian
'I': u'\u2110',
'J': u'\U0001D4A5',
'K': u'\U0001D4A6',
'L': u'\u2112', # lagrangian
'M': u'\u2133', # physics m-matrix
'N': u'\U0001D4A9',
'O': u'\U0001D4AA',
'P': u'\U0001D4AB',
'Q': u'\U0001D4AC',
'R': u'\u211B',
'S': u'\U0001D4AE',
'T': u'\U0001D4AF',
'U': u'\U0001D4B0',
'V': u'\U0001D4B1',
'W': u'\U0001D4B2',
'X': u'\U0001D4B3',
'Y': u'\U0001D4B4',
'Z': u'\U0001D4B5',
'a': u'\U0001D4B6',
'b': u'\U0001D4B7',
'c': u'\U0001D4B8',
'd': u'\U0001D4B9',
'e': u'\u212F',
'f': u'\U0001D4BB',
'g': u'\u210A',
'h': u'\U0001D4BD',
'i': u'\U0001D4BE',
'j': u'\U0001D4BF',
'k': u'\U0001D4C0',
'l': u'\U0001D4C1',
'm': u'\U0001D4C2',
'n': u'\U0001D4C3',
'o': u'\u2134', # order of
'p': u'\U0001D4C5',
'q': u'\U0001D4C6',
'r': u'\U0001D4C7',
's': u'\U0001D4C8',
't': u'\U0001D4C9',
'u': u'\U0001D4CA',
'v': u'\U0001D4CB',
'w': u'\U0001D4CC',
'x': u'\U0001D4CD',
'y': u'\U0001D4CE',
'z': u'\U0001D4CF',
}
negatables = {'=': u'\u2260',
'\in': u'\u2209',
'\equiv': u'\u2262'}
# LaTeX to MathML translation stuff:
class math:
"""Base class for MathML elements."""
nchildren = 1000000
"""Required number of children"""
def __init__(self, children=None, inline=None):
"""math([children]) -> MathML element
children can be one child or a list of children."""
self.children = []
if children is not None:
if type(children) is list:
for child in children:
self.append(child)
else:
# Only one child:
self.append(children)
if inline is not None:
self.inline = inline
def __repr__(self):
if hasattr(self, 'children'):
return self.__class__.__name__ + '(%s)' % \
','.join([repr(child) for child in self.children])
else:
return self.__class__.__name__
def full(self):
"""Room for more children?"""
return len(self.children) >= self.nchildren
def append(self, child):
"""append(child) -> element
Appends child and returns self if self is not full or first
non-full parent."""
assert not self.full()
self.children.append(child)
child.parent = self
node = self
while node.full():
node = node.parent
return node
def delete_child(self):
"""delete_child() -> child
Delete last child and return it."""
child = self.children[-1]
del self.children[-1]
return child
def close(self):
"""close() -> parent
Close element and return first non-full element."""
parent = self.parent
while parent.full():
parent = parent.parent
return parent
def xml(self):
"""xml() -> xml-string"""
return self.xml_start() + self.xml_body() + self.xml_end()
def xml_start(self):
if not hasattr(self, 'inline'):
return ['<%s>' % self.__class__.__name__]
xmlns = 'http://www.w3.org/1998/Math/MathML'
if self.inline:
return ['