Package: asciitomathml
OverviewThe asciitomathml converts ASCII math to MathML. Seehttp://mathcs.chapman.edu/~jipsen/mathml/asciimath.htmlfor more details. As an example, asciitomathml converts the stringx^2to:<math xmlns="http://www.w3.org/1998/Math/MathML">
 <mstyle>
  <msup>
   <mi>x</mi>
   <mn>2</mn>
  </msup>
 </mstyle>
</math>InstallationInstall asciitomathml in the normal way:python setup.py installOr, for pip:pip install asciitomathmlInstallation for Python 2Use version .88 if you need asciimathml for Python 2.7::pip install asciitomathml=.88UseThe 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 themath_treemethod:math_tree = math_obj.get_tree() # math_tree is an etree objectInstead, if you want an XML string, use theto_xml_stringmethod:xml_string = math_obj.to_xml_string() # xml_string a binary stringThe 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 styleYou can pass any attributes to the<msstyle>that are allowed. Use themstyleoption to pass adictionarywhen creating the method:math_obj =  asciitomathml.asciitomathml.AsciiMathML(mstyle={'displaystyle':'true'})The most useful attribute is probablydisplaystyle. In general, set this attribute totrueif you will put the equation by itself, in block. Otherwise, don’t set this value at all,
or set it tofalse.  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.mstyleScriptsI 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.xhtmland then openlinear.xhtmlin a browser that can handle mathml, such as Firefox.TestTo test the library, type:python test/test_all.py