模块将ASCII数学转换为Mathml
项目描述
概述
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> </math>
安装
按正常方式安装asciitomathml
python setup.py install
或者,对于pip
pip install asciitomathml
Python 2的安装
- 如果您需要为Python 2.7安装asciimathml,请使用版本.88:
pip install asciitomathml=.88
使用
以下示例创建从字符串的etree
import asciitomathml.asciitomathml the_string = 'x^2' math_obj = asciitomathml.asciitomathml.AsciiMathML() math_obj.parse_string(the_string)
为了获取树,请使用 math_tree 方法
math_tree = math_obj.get_tree() # math_tree is an etree object
相反,如果您想得到XML字符串,请使用 to_xml_string 方法
xml_string = math_obj.to_xml_string() # xml_string a binary string
xml_string将具有类型‘bytes’。如果您要将类型更改为‘str’,请传递类型为“unicode”的编码
xml_string = math_obj.to_xml_string(encoding='unicode')
字符串将被编码为UTF-8。
如果您向此方法传递除utf8之外的编码,字符串将开始于标准的XML编码,符合XML标准
<?xml version='1.0' encoding='utf8'?>
如果您将字符串合并到XML文档中,并且不想包含编码字符串,则可能需要使用get_tree方法,并将结果对象合并到您的etree文档中。同样,如果不向此方法传递任何编码,则返回的字符串将编码为ASCII,不应包含字符串的编码部分。但是,如果出于某种原因需要没有编码的树,请将no_encoding_string选项传递给to_xml_string方法
xml_string = math_obj.to_xml_string(encoding="utf8", no_encoding_string = True)
数学样式
您可以将任何属性传递给 <msstyle>。在创建方法时使用 mstyle 选项传递一个 字典。
math_obj = asciitomathml.asciitomathml.AsciiMathML(mstyle={'displaystyle':'true'})
最有用的属性可能是displaystyle。通常,如果您要将方程独立显示(即块状),请将此属性设置为true。否则,不要设置此值,或将其设置为false。MathML联盟这样解释:
对于嵌入在文本数据格式(如HTML)中并以“display”模式显示的MathML实例,即作为段落的替代,嵌入的MathML的外部表达式displaystyle = “true”和scriptlevel = “0”;如果MathML以“inline”模式嵌入,即作为字符的替代,则displaystyle = “false”和scriptlevel = “0”。有关“display”和“inline”嵌入MathML之间的区别以及如何在特定实例中指定此内容的进一步讨论,请参阅第7章MathML界面。通常,MathML渲染器可以根据它在渲染特定MathML实例的位置和上下文中适当的任何方式确定这些初始值;如果没有方法可以确定这一点,则基于它最可能被使用的方式;最后,建议它使用最通用的值displaystyle = “true”和scriptlevel = “0”。
脚本
我包含了两段脚本作为示例。这些脚本展示了库的能力。由于它们必须从文件中读取文本,形成段落,并在数学和非数学标记之间进行区分,因此它们不是将文本广泛转换为HTM或FO的工具。有关此类转换,请参阅
http://docutils.sourceforge.net/
特别是,请参阅sandbox/docbook目录,其中包含将文本转换为docbook的广泛样式表和说明,然后转换为HTML或FO。
要使用这些脚本,请键入
python scripts/asciimath2fo.py <file.txt>
或
python scripts/asciimath2html.py <file.txt>
这些脚本将“`”和“`”之间的任何内容转换为mathml;否则,脚本将直接复制文本。请参阅示例目录中的示例。为了快速开始,请尝试
python scripts/asciimath2html.py examples/linear_regression.txt > linear.xhtml
然后在能够处理mathml的浏览器中打开linear.xhtml,例如Firefox。
测试
要测试库,请键入
python test/test_all.py
项目详情
asciitomathml-1.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 656e43ac8b2d3b719cd23efeac854086e7b93fe7dbd646925bdffaab1b36adfc |
|
MD5 | 05853e800dd26e5b76927f59084774cf |
|
BLAKE2b-256 | 88f8686b5bbf6742abb65610f788b5efbfd7dab10811163a3a9f73f8bc883ea9 |