Module converts ASCII math to Mathml

## Overview

The asciitomathml converts ASCII math to MathML. See http://mathcs.chapman.edu/~jipsen/mathml/asciimath.html for more details. As an example, asciitomathml converts the string x^2 to:

<math xmlns="http://www.w3.org/1998/Math/MathML">
<mstyle>
<msup>
<mi>x</mi>
<mn>2</mn>
</msup>
</mstyle>
[/itex]

## Installation

Install asciitomathml in the normal way:

python setup.py install

Or, for pip:

pip install asciitomathml

### Installation for Python 2

Use version .88 if you need asciimathml for Python 2.7::

pip install asciitomathml=.88

## Use

The following creates etree from a string:

import asciitomathml.asciitomathml
the_string = 'x^2'
math_obj =  asciitomathml.asciitomathml.AsciiMathML()
math_obj.parse_string(the_string)

In order to get the tree, use the math_tree method:

math_tree = math_obj.get_tree() # math_tree is an etree object

Instead, if you want an XML string, use the to_xml_string method:

xml_string = math_obj.to_xml_string() # xml_string a binary string

The xml_string will have type ‘bytes’. If you will to have a type of ‘str’ pass an encoding of type “unicode”:

xml_string = math_obj.to_xml_string(encoding='unicode')

The string will be encoded as UTF-8.

If you pass an encoding other than utf8 to this method, the string will start with the standard XML encoding, in accordance with XML standards:

<?xml version='1.0' encoding='utf8'?>

If you are incorporating the string into an XML document, and don’t want the encoding string, you should probably use the get_tree method and incorporate the resulting object into your etree document. Likewise, by not passing any encoding to this method, the returned string will be encoded as ASCII and should not include the encoding part of the string. However, if for whatever reason you need a tree without the encoding, pass the no_encoding_string option to the to_xml_string method:

xml_string = math_obj.to_xml_string(encoding="utf8", no_encoding_string = True)

### Math style

You can pass any attributes to the <msstyle> that are allowed. Use the mstyle option to pass a dictionary when creating the method:

math_obj =  asciitomathml.asciitomathml.AsciiMathML(mstyle={'displaystyle':'true'})

The most useful attribute is probably displaystyle. In general, set this attribute to true if you will put the equation by itself, in block. Otherwise, don’t set this value at all, or set it to false. The consortium for mathml explains it this way:

For an instance of MathML embedded in a textual data format (such as HTML) in “display” mode, i.e. in place of a paragraph, displaystyle = “true” and scriptlevel = “0” for the outermost expression of the embedded MathML; if the MathML is embedded in “inline” mode, i.e. in place of a character, displaystyle = “false” and scriptlevel = “0” for the outermost expression. See Chapter 7 The MathML Interface for further discussion of the distinction between “display” and “inline” embedding of MathML and how this can be specified in particular instances. In general, a MathML renderer may determine these initial values in whatever manner is appropriate for the location and context of the specific instance of MathML it is rendering, or if it has no way to determine this, based on the way it is most likely to be used; as a last resort it is suggested that it use the most generic values displaystyle = “”true”” and scriptlevel = “”0””.

http://www.w3.org/TR/MathML2/chapter3.html#presm.mstyle

## Scripts

I have included two scripts as examples. These scripts show the capability of the libarary. Since they must read text from a file, form paragraphs, and distinguish between math and non math markup, they are not meant as tools for extensive conversion of text to HTM or FO. For such conversions, see:

http://docutils.sourceforge.net/

Specifically, see the sandbox/docbook directory, which features extensive stylesheets and instructions for converting text to docbook, and then to HTML or FO.

In order to use the scripts, type:

python scripts/asciimath2fo.py <file.txt>

or:

python scripts/asciimath2html.py <file.txt>

The scripts convert anything between “” and “” to mathml; otherwise, the scripts just copy the text verbatim. See the examples in the example directory. For a quick start, try:

python scripts/asciimath2html.py examples/linear_regression.txt > linear.xhtml

and then open linear.xhtml in a browser that can handle mathml, such as Firefox.

## Test

To test the library, type:

python test/test_all.py

## Project details

Uploaded source