Skip to content

Commit

Permalink
Add latex2asciimath tests
Browse files Browse the repository at this point in the history
  • Loading branch information
belerico committed Apr 21, 2020
1 parent 326ce7d commit 98713fb
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ $ {\displaystyle {e}^{x}>0\forall x\in \mathbb{R} }$
ASCIIMath to LaTeX
INFO:Translating...
\[e^{x} > 0 \forall x \in \mathbb{R}\]
\[{e}^{x} > 0 \forall x \in \mathbb{R}\]
LaTeX to ASCIIMath
INFO:Translating...
Expand Down
1 change: 1 addition & 0 deletions py_asciimath/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def get_symbols_for(symbol_group, lang_to): # pragma: no cover
"\\sqrt": {"asciimath": "sqrt", "mathml": "<msqrt>{}</msqrt>"},
"\\text": {"asciimath": "text", "mathml": "<mtext>{}</mtext>"},
"\\textrm": {"asciimath": "text", "mathml": "<mtext>{}</mtext>"},
"\\mathrm": {"asciimath": "text", "mathml": "<mtext>{}</mtext>"},
"\\underbrace": {
"asciimath": ["ubrace", "underbrace"],
"mathml": "<munder>{}<mo>&#x23DF;</mo></munder>",
Expand Down
9 changes: 6 additions & 3 deletions py_asciimath/transformer/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,20 @@ def const(self, items):
def q_str(self, items):
return items

def _get_row(self, items, sep="&"):
def _get_row(self, items, sep="&", mat=False):
s = ""
for i in items:
if i == sep:
i = ","
s = s + i
return "[" + s + "]"
if mat:
return "{:" + s + ":}"
else:
return "[" + s + "]"

@log
def exp_mat(self, items):
return self._get_row(items, sep="\\\\")
return self._get_row(items, sep="\\\\", mat=True)

@log
def row_mat(self, items):
Expand Down
116 changes: 116 additions & 0 deletions tests/test_Tex2ASCIIMath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import unittest

from py_asciimath.translator.translator import Tex2ASCIIMath


class TestTex2ASCIIMath(unittest.TestCase):
def setUp(self):
self.maxDiff = None

def test_tex2asciimath_ok_1(self):
s = Tex2ASCIIMath(
inplace=True, parser="lalr", lexer="contextual",
).translate(
r"$\left[\begin{matrix}"
r"\int x \mathrm{dx} \\ \log \left(x + 1\right)"
r"\end{matrix}\right]$",
)
self.assertEqual(s, "[{:[int x text(d x)],[log (x + 1)]:}]")

# def test_tex2asciimath_ok_2(self):
# s = Tex2ASCIIMath(
# inplace=True, parser="lalr", lexer="contextual",
# ).translate(
# "((1,2))int sin{x^2}/4pidxroot(5)(x_1^2+x_2^2)", displaystyle=True
# )
# self.assertEqual(
# s,
# r"\[\left(\left(1 , 2\right)\right) \int \sin "
# r"\frac{{x}^{2}}{4} \pi \mathrm{dx} \sqrt[5]{{x}_{1}^{2} + {x}_{2}^{2}}\]",
# )

# def test_tex2asciimath_ok_3(self):
# s = Tex2ASCIIMath(
# inplace=True, parser="lalr", lexer="contextual",
# ).translate("lim_(N->oo) sum_(i=0)^N int_0^1 f(x)dx 3.14")
# self.assertEqual(
# s,
# r"${\lim}_{N \to \infty} {\sum}_{i = 0}^{N} "
# r"{\int}_{0}^{1} f \left(x\right) \mathrm{dx} 3.14$",
# )

# def test_tex2asciimath_ok_4(self):
# s = Tex2ASCIIMath(
# inplace=True, parser="lalr", lexer="contextual",
# ).translate(
# """uuu_{2(x+1)=1)^{n}
# min{
# 2x|x^{y+2} in bbb(N) wedge arccos root(3}(frac{1}{3x}) < i
# rarr Omega < b, 5=x
# }"""
# )
# self.assertEqual(
# s,
# r"${\bigcup}_{2 \left(x + 1\right) = 1}^{n} "
# r"\min \left\{2 x | {x}^{y + 2} \in \mathbb{N} \wedge "
# r"\arccos \sqrt[3]{\frac{1}{3 x}} < i \rightarrow "
# r"\Omega < b , 5 = x\right\}$",
# )

# def test_tex2asciimath_ok_5(self):
# parser = Tex2ASCIIMath()
# s = parser.translate(
# """uuu_{2(x+1)=1)^{n}
# min{
# 2x|x^{y+2} in bbb(N) wedge arccos root(3}(frac{1}{3x}) < i
# rarr Omega < b, 5=x
# }""",
# pprint=True,
# )
# self.assertEqual(
# s,
# r"${\bigcup}_{2 \left(x + 1\right) = 1}^{n} "
# r"\min \left\{2 x | {x}^{y + 2} \in \mathbb{N} \wedge "
# r"\arccos \sqrt[3]{\frac{1}{3 x}} < i \rightarrow "
# r"\Omega < b , 5 = x\right\}$",
# )

# def test_tex2asciimath_ok_6(self):
# parser = Tex2ASCIIMath(log=True)
# s = parser.translate(
# "[(1,2), (2^|: 3 :|, (dstyle int x^{2(x-n)})), (2,4)]"
# )
# self.assertEqual(
# s,
# r"$\left[\begin{matrix}1 & 2 \\ {2}^{\left\vert3\right\vert} "
# r"& \left(\displaystyle{\int} {x}^{2 \left(x - n\right)}\right) "
# r"\\ 2 & 4\end{matrix}\right]$",
# )

# def test_tex2asciimath_ok_7(self):
# parser = Tex2ASCIIMath(log=True)
# s = parser.translate("langle [1,2], [2,int[3(x+1)]dx]:}")
# self.assertEqual(
# s,
# r"$\left\langle \begin{matrix}1 & 2 \\ 2 "
# r"& \int \left[3 \left(x + 1\right)\right] "
# r"\mathrm{dx}\end{matrix}\right.$",
# )

# def test_tex2asciimath_ok_8(self):
# parser = Tex2ASCIIMath(log=False)
# s = parser.translate("norm(x*y)<=norm(x)*norm(y)")
# self.assertEqual(
# s,
# r"$\left\lVert x \cdot y \right\rVert \le "
# r"\left\lVert x \right\rVert \cdot \left\lVert y \right\rVert$",
# )

# def test_tex2asciimath_ok_9(self):
# parser = Tex2ASCIIMath(log=False)
# s = parser.translate('A \\ B "setminus"')
# self.assertEqual(s, r"$A \setminus B \text{setminus}$")


if __name__ == "__main__":
unittest.main()

0 comments on commit 98713fb

Please sign in to comment.