belerico / py_asciimath Goto Github PK
View Code? Open in Web Editor NEWA simple converter from ASCIIMath to LaTeX or MathML and from MathML to LaTeX
License: MIT License
A simple converter from ASCIIMath to LaTeX or MathML and from MathML to LaTeX
License: MIT License
py_asciimath.exe "\[a=60{}^\circ\ ]" from latex to asciimath
py_asciimath.exe "\[a=45{}^\circ \]" from latex to asciimath
py_asciimath.exe "\[\bar{N}t=m{{v}{0}}\sin \alpha \]" from latex to asciimath
py_asciimath.exe "\[\bar{f}t=\mu \bar{N}t=\mu m{{v}{0}}\sin \alpha \approx 0.6m{{v}{0}}\]" from latex to asciimath
py_asciimath.exe "\[\alpha ={{45}^{{}^\circ }},\mu =0.5\]" from latex to asciimath
py_asciimath.exe "\[\bar{N}t=m{{v}{0}}\sin \alpha \]" from latex to asciimath
py_asciimath.exe "\[\bar{f}t=\mu m{{v}{0}}\sin \alpha =m{{v}{0}}\cos \alpha -mv\]" from latex to asciimath
py_asciimath.exe "\[\alpha ={{30}^{{}^\circ }}\]" from latex to asciimath
looks like they dont work.
When running this code:
from py_asciimath.translator.translator import (
ASCIIMath2MathML,
ASCIIMath2Tex,
MathML2Tex,
Tex2ASCIIMath
)
latex_input = r"\frac{3}{2^4}"
tex2asciimath = Tex2ASCIIMath(log=False, inplace=True)
parsed = tex2asciimath.translate(latex_input, from_file=False, pprint=False,)
print(parsed)
prints this:
frac(3)((2)^(4))
when it's meant to print:
(3)/(2^4)
Is there something wrong with how the LaTeX equation is being input or is it a bug?
The XSLT to translate from MathML to LaTeX is very outdated, so it would be nice to refactor it to some LaTeX math standard
For example,
AsciiMath formula: {{E}_{{{{{x}'}}_{1}}}}={{E}_{0}}\cos \omega t
actually is the same as : {E_{x'_1}}=E_0\cos \omega t
too many braces '{' and '}' are no need.
I installed py_asciimath, update lxml for my python-3.8.1.amd64 on Windows 10 64 bits
then I try to run the code bellowing, which is actually the code on https://github.com/belerico/py_asciimath, but I get
ASCIIMath to MathML
WARNING:root:No connection available...
WARNING:root:No XML declaration with 'encoding' attribute set: default encoding to None
WARNING:root:The XML encoding is None: default to UTF-8
WARNING:root:No DTD declaration found: set to local mathml2 DTD
Traceback (most recent call last):
File "a.py", line 12, in <module>
parsed = asciimath2mathml.translate(
File "E:\_PRG_BAK\py_asciimath-master\py_asciimath\translator\translator.py", line 336, in translate
return super(ASCIIMath2MathML, self).translate(
File "E:\_PRG_BAK\py_asciimath-master\py_asciimath\translator\translator.py", line 136, in translate
return super(LarkTranslator, self).translate(
File "E:\_PRG_BAK\py_asciimath-master\py_asciimath\translator\translator.py", line 65, in translate
exp = self._translate(exp, **kwargs)
File "E:\_PRG_BAK\py_asciimath-master\py_asciimath\translator\translator.py", line 266, in _translate
parsed = MathMLParser.parse(
File "E:\_PRG_BAK\py_asciimath-master\py_asciimath\parser\parser.py", line 297, in parse
return lxml.etree.fromstring(xml, mathml_parser)
File "src\lxml\etree.pyx", line 3236, in lxml.etree.fromstring
File "src\lxml\parser.pxi", line 1876, in lxml.etree._parseMemoryDocument
File "src\lxml\parser.pxi", line 1764, in lxml.etree._parseDoc
File "src\lxml\parser.pxi", line 1127, in lxml.etree._BaseParser._parseDoc
File "src\lxml\parser.pxi", line 601, in lxml.etree._ParserContext._handleParseResultDoc
File "src\lxml\parser.pxi", line 711, in lxml.etree._handleParseResult
File "src\lxml\parser.pxi", line 640, in lxml.etree._raiseParseError
File "<string>", line 1
lxml.etree.XMLSyntaxError: failed to load external entity "NULL", line 1, column 128
the code is
from py_asciimath.translator.translator import (
ASCIIMath2MathML,
ASCIIMath2Tex,
MathML2Tex,
Tex2ASCIIMath
)
if __name__ == "__main__":
print("ASCIIMath to MathML")
asciimath2mathml = ASCIIMath2MathML(log=False, inplace=True)
parsed = asciimath2mathml.translate(
r"e^x > 0 forall x in RR",
displaystyle=True,
dtd="mathml2",
dtd_validation=True,
from_file=False,
output="string",
network=True,
pprint=False,
to_file=None,
xml_declaration=True,
xml_pprint=True,
)
print(parsed, "\n\nMathML to LaTeX")
mathml2tex = MathML2Tex()
parsed = mathml2tex.translate(parsed, network=False, from_file=False,)
print(parsed, "\n\nASCIIMath to LaTeX")
asciimath2tex = ASCIIMath2Tex(log=False, inplace=True)
parsed = asciimath2tex.translate(
r"e^x > 0 forall x in RR",
displaystyle=True,
from_file=False,
pprint=False,
)
print(parsed, "\n\nLaTeX to ASCIIMath")
tex2asciimath = Tex2ASCIIMath(log=False, inplace=True)
parsed = tex2asciimath.translate(
parsed,
from_file=False,
pprint=False,
)
print(parsed)
I think it's not proper to config log level DEBUG inside many inner modules:
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.DEBUG)
It makes this package unusable in interactive mode, unless the user understand how to deal with the logging
stuff.
My suggestion is to put the logging.basicConfig
only inside your entry point scripts / functions.
when running unit test
parser = ASCIIMath2Tex(log=False)
s = parser.translate("f(x)/g(x)")
self.assertEqual(s,r"$\frac{f(x)}{g(x)}$")
it outputs
======================================================================
FAIL: test_asciimath2tex_ok_10 (__main__.TestASCIIMath2Tex.test_asciimath2tex_ok_10)
----------------------------------------------------------------------
Traceback (most recent call last):
File "e:\Folder\py_asciimath\tests\test_ASCIIMath2Tex.py", line 119, in test_asciimath2tex_ok_10
self.assertEqual(s,r"$\frac{f(x)}{g(x)}$")
AssertionError: '$f \\frac{x}{g} \\left(x\\right)$' != '$\\frac{f(x)}{g(x)}$'
- $f \frac{x}{g} \left(x\right)$
+ $\frac{f(x)}{g(x)}$
Mathjax, who renders asciimath formula, will treat f(x)/g(x)
to the view
for example, py_asciimath "[\bar{N}t=m{{v}_{0}}\sin \alpha]" from latex to asciimath , it doesnt work.
It will be useful to translate also to MathML or Mathematica languages.
py_asciimath/py_asciimath/asciimath.py
Line 149 in e15edf0
⊗
instead of ×
in this line.A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.