docutils.utils.math.latex2mathml module

Convert LaTex maths code into presentational MathML.

This module is provisional: the API is not settled and may change with any minor Docutils version.

class math(*children, **attributes)[source]

Bases: object

Base class for MathML elements and root of MathML trees.

nchildren = None

Expected number of children or None

parent = None

Parent node in MathML DOM tree.

_level = 0
xml_entities = {38: '&', 60: '<', 62: '>', 8289: '⁡'}
_boolstrings = {False: 'false', True: 'true'}

String representation of boolean MathML attribute values.

html_tagname = 'span'

Tag name for HTML representation.

__init__(*children, **attributes)[source]

Set up node with children and attributes.

Attributes are downcased: Use CLASS to set “class” value. >>> math(mn(3), CLASS=’test’) math(mn(3), class=’test’) >>> math(CLASS=’test’).toprettyxml() ‘<math class=”test”>

</math>’

get(*args, **kwargs)[source]
full()[source]

Return boolean indicating whether children may be appended.

append(child)[source]

Append child and return self or first non-full parent.

If self is full, go up the tree and return first non-full node or None.

extend(children)[source]
close()[source]

Close element and return first non-full parent or None.

toprettyxml()[source]

Return XML representation of self as string.

_xml(level=0)[source]
xml_starttag()[source]
_xml_body(level=0)[source]
is_block()[source]

Return true, if self or a parent has display='block'.

class mtable(*children, **attributes)[source]

Bases: math

class mrow(*children, **attributes)[source]

Bases: math

Group sub-expressions as a horizontal row.

close()[source]

Close element and return first non-full parent or None.

Remove <mrow>, if it is single child and the parent infers an mrow or if it has only one child element.

class MathRowSchema(*children, **attributes)[source]

Bases: math

Base class for elements treating content as a single inferred mrow.

class mtr(*children, **attributes)[source]

Bases: MathRowSchema

class mtd(*children, **attributes)[source]

Bases: MathRowSchema

class menclose(*children, **attributes)[source]

Bases: MathRowSchema

nchildren = 1

Expected number of children or None

class mphantom(*children, **attributes)[source]

Bases: MathRowSchema

nchildren = 1

Expected number of children or None

class msqrt(*children, **attributes)[source]

Bases: MathRowSchema

nchildren = 1

Expected number of children or None

class mstyle(*children, **attributes)[source]

Bases: MathRowSchema

nchildren = 1

Expected number of children or None

class MathToken(data, **attributes)[source]

Bases: math

Token Element: contains textual data instead of children.

Base class for mo, mi, and mn.

nchildren = 0

Expected number of children or None

_xml_body(level=0)[source]
class mtext(data, **attributes)[source]

Bases: MathToken

class mi(data, **attributes)[source]

Bases: MathToken

class mo(data, **attributes)[source]

Bases: MathToken

class mn(data, **attributes)[source]

Bases: MathToken

class MathSchema(*children, **kwargs)[source]

Bases: math

Base class for schemata expecting 2 or more children.

The special attribute switch indicates that the last two child elements are in reversed order and must be switched before XML-export.

nchildren = 2

Expected number of children or None

append(child)[source]

Append child and return self or first non-full parent.

If self is full, go up the tree and return first non-full node or None.

class msub(*children, **kwargs)[source]

Bases: MathSchema

class msup(*children, **kwargs)[source]

Bases: MathSchema

class msubsup(*children, **kwargs)[source]

Bases: MathSchema

nchildren = 3

Expected number of children or None

class munder(*children, **kwargs)[source]

Bases: msub

class mover(*children, **kwargs)[source]

Bases: msup

class munderover(*children, **kwargs)[source]

Bases: msubsup

class mroot(*children, **kwargs)[source]

Bases: MathSchema

nchildren = 2

Expected number of children or None

class mfrac(*children, **attributes)[source]

Bases: math

nchildren = 2

Expected number of children or None

class mspace(*children, **attributes)[source]

Bases: math

nchildren = 0

Expected number of children or None

tex_cmdname(string)[source]

Return leading TeX command name and remainder of string.

>>> tex_cmdname('mymacro2') # up to first non-letter
('mymacro', '2')
>>> tex_cmdname('name 2') # strip trailing whitespace
('name', '2')
>>> tex_cmdname('_2') # single non-letter character
('_', '2')
tex_number(string)[source]

Return leading number literal and remainder of string.

>>> tex_number('123.4')
('123.4', '')
tex_token(string)[source]

Return first simple TeX token and remainder of string.

>>> tex_token('\command{without argument}')
('\command', '{without argument}')
>>> tex_token('or first character')
('o', 'r first character')
tex_group(string)[source]

Return first TeX group or token and remainder of string.

>>> tex_group('{first group} returned without brackets')
('first group', ' returned without brackets')
tex_token_or_group(string)[source]

Return first TeX group or token and remainder of string.

>>> tex_token_or_group('\command{without argument}')
('\command', '{without argument}')
>>> tex_token_or_group('first character')
('f', 'irst character')
>>> tex_token_or_group(' also whitespace')
(' ', 'also whitespace')
>>> tex_token_or_group('{first group} keep rest')
('first group', ' keep rest')
tex_optarg(string)[source]

Return optional argument and remainder.

>>> tex_optarg('[optional argument] returned without brackets')
('optional argument', ' returned without brackets')
>>> tex_optarg('{empty string, if there is no optional arg}')
('', '{empty string, if there is no optional arg}')
parse_latex_math(node, string)[source]

Append MathML conversion of string to node and return it.

>>> parse_latex_math(math(), r'lpha')
math(mi('α'))
>>> parse_latex_math(mrow(), r'x_{n}')
mrow(msub(mi('x'), mi('n')))
handle_cmd(name, node, string)[source]

Process LaTeX command name followed by string.

Append result to node. If needed, parse string for command argument. Return new current node and remainder of string:

>>> handle_cmd('hbar', math(), r'
rac’)

(math(mi(‘ℏ’)), ‘ frac’) >>> handle_cmd(‘hspace’, math(), r’{1ex} (x)’) (math(mspace(width=’1ex’)), ‘ (x)’)

handle_script_or_limit(node, c, limits='')[source]

Append script or limit element to node.

begin_environment(node, string)[source]
end_environment(node, string)[source]
tex_equation_columns(rows)[source]
align_attributes(rows)[source]
tex2mathml(tex_math, inline=True)[source]

Return string with MathML code corresponding to tex_math.

Set inline to False for displayed math.