Skip to content

Commit

Permalink
Merge pull request #5 from belerico/latex2asciimath
Browse files Browse the repository at this point in the history
Merged latex2asciimath into master
  • Loading branch information
belerico authored Apr 21, 2020
2 parents 6eada0e + 7c9c50a commit b4d96f5
Show file tree
Hide file tree
Showing 19 changed files with 1,382 additions and 566 deletions.
61 changes: 55 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

py_asciimath is a simple yet powerful Python module that:

* converts an ASCIIMath string to LaTeX or MathML
* converts an ASCIIMath ex to LaTeX or MathML
* converts a LaTeX mathematical expression to ASCIIMath (soon also to MathML)
* converts a MathML string to LaTeX (the conversion is done thank to the [XSLT MathML Library](https://sourceforge.net/projects/xsltml/). Please report any unexpected behavior)
* exposes a single translation method `translate(exp, **kwargs)`, which semantic depends on the py_asciimath translator one wish to use
* exposes a MathML parser
Expand Down Expand Up @@ -36,6 +37,7 @@ from py_asciimath.translator.translator import (
ASCIIMath2MathML,
ASCIIMath2Tex,
MathML2Tex,
Tex2ASCIIMath
)


Expand Down Expand Up @@ -68,6 +70,14 @@ if __name__ == "__main__":
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)
```

Expand Down Expand Up @@ -113,7 +123,11 @@ $ {\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...
(e)^(x) > 0 AA x in RR
```

#### From the command line
Expand Down Expand Up @@ -144,12 +158,12 @@ Usage:
Options:
--dstyle Add display style
-i <ILANG> --input=ILANG Input language
Supported input language: asciimath, mathml
Supported input language: asciimath, latex, mathml
--log Log the transformation process
--network Works only with ILANG=mathnml or OLANG=mathml
Use network to validate XML against DTD
-o <OLANG> --output=OLANG Output language
Supported output language: latex, mathml
Supported output language: asciimath, latex, mathml
--pprint Works only with OLANG=mathml. Pretty print
--to-file=OPATH Save translation to OPATH file
--version Show version
Expand Down Expand Up @@ -222,7 +236,7 @@ $e^{x} > 0 \forall x \in \mathbb{R}$

## ASCIIMath grammar

The grammar used to parse the input is:
The grammar used to parse an ASCIIMath input is:

```
start: i start* -> exp
Expand All @@ -248,7 +262,42 @@ asciimath: {} // asciimath symbols
QS: "\"" /(?<=").+(?=")/ "\"" // Quoted String
```

For the complete list of symbols, please refer to http://asciimath.org/##syntax. The only symbol that I've added is `dstyle`, that stands for `displaystyle` as a unary function.
For the complete list of symbols, please refer to http://asciimath.org/#syntax. The only symbol that I've added is `dstyle`, that stands for `displaystyle` as a unary function.

## LaTeX grammar

The grammar used to parse a LaTeX input is:

```
start: "\[" exp "\]" -> exp
| "$$" exp "$$" -> exp
| "$" exp "$" -> exp
| exp -> exp
exp: i exp* -> exp
i: s -> exp_interm
| s "_" s -> exp_under
| s "^" s -> exp_super
| s "_" s "^" s -> exp_under_super
s: l exp? r -> exp_par
| "\left" (l | "." | "\vert" | "\mid") start? "\right" (r | "." | "\vert" | "\mid") -> exp_par
| "\begin{matrix}" row_mat ("\\" row_mat?)* "\end{matrix}" -> exp_mat
| "{" i+ "}" -> exp
| u "{" exp "}" -> exp_unary
| b "{" exp "}" "{" exp "}" -> exp_binary
| "\sqrt" "[" i+ "]" "{{" exp "}}" -> exp_binary
| latex -> symbol
| c -> const
c: NUMBER
| LETTER
row_mat: exp ("&" exp?)* -> row_mat
l: "(" | "[" | "\{" | "\langle" | "\lVert" // left parenthesis
r: ")" | "]" | "\}" | "\rangle" | "\rVert" // right parenthesis
b: {} // binary functions
u: {} // unary functions
latex: {} // LaTeX symbols
```

Be careful that not all the LaTeX symbols are included in the grammar: please fill in an issue if you find that some symbols are missing

## Rendering (matrices and systems of equations)

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<mxfile host="app.diagrams.net" modified="2020-04-18T13:02:45.119Z" agent="5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) snap Chromium/81.0.4044.92 Chrome/81.0.4044.92 Safari/537.36" etag="pEsJBWVT8h4Xn8D08d1B" version="12.9.14" type="device"><diagram id="Q6-cwq1KEDNFQR1_6Txc" name="Page-1">3Zldb5swFIZ/TaTtYhHgQMJlm2ZrpVaqlEpLLz1wwZvBkXG+9utng/kwJmmX0KZtLirOsX2w3/McG+gATJPtDwaX8R0NERk4VrgdgKuB40y8ifgrHbvCMfKcwhExHBYuu3bM8V+knJbyrnCIMq0jp5RwvNSdAU1TFHDNBxmjG73bEyX6XZcwQoZjHkBien/ikMdqWa5V+68RjuLyzralWhJYdlaOLIYh3TRcYDYAU0YpL66S7RQRqV2pSzHu+57WamIMpfwlA9ZObM9CfBNf+/4u5g/bVTr7pqKsIVmpBV/Mpzc3d3Lyxaz5rpRChBOqC+NyE2OO5ksYyJaNyLvwxTwhwrLFpYqJGEfbvZO1KwkEOogmiLOd6KIGgFJghY3tKXtTJ8EulY0bCZgoH1R5j6rQtTTiQqnzH0q5ph6hIEWZlPGYRjSFZFZ7LxldpSGSUS1h1X1uKV0qrX4jzncKe7jitEtJeaPDOop50RUL0IH5q7LjkEWIH+gHuvPCEIEcr/V59C6yY+AoSby7PSuL9lhnsWLsbCwCQ6Zb+IAWZ1XJBe9NpfIIachkKpSGF/KUEFZAYJbhQBcGbTFfNK4fZS0PXWVdbVVp58auNFIx+0XdUZqPzbZ6WG6V43os9vKkPLLYGylzOzJW+l68J6g73FMsFlIRM7J1Yiq7DFEsU41qnmztQJ4eCHitQIUORqCcqmrZJ4BmnqJHg2Z9TtCcs4I2aW1Nx4LmWa1A/huDZp6PBmjBiq3zR46cqueoqwkajZwmQ/bQssEzHOXWPWJYrAuxBqELDd7HBuLdIGec0T9oSgll+SKAlf9ESwizuFpNj9yCD8HtuTdIwQ7cNbotZYfswIS97gnXZVBE7LcozKehnorC1jbVeo/dVw6d5FsflPw9T3tvQ357owXjI8l3ABhawK9/Xiuu3dncc124vr4c23+Luhid/lSyH8+XEnk2gowz3x+6xzE0br//maFe+9x3T0+ltp98oryO/Z6y2g702jn1jJxyBtNMqIMyuUFTI8Xi5Zwfqs+UpvJd/wkT0nJBgqNUkiGSKJ/ULuWrPg4guVANCQ5Dsu9Dgf4Za3D6t4J2Esv9sMnQuIMhx9qPy2mfCsbPJ8MjUvxfTFxF8upLiNaI0GUiFv31FXJF0BN/d5lyRr6RqUk/mRJm/TG8qLL6Pwpg9g8=</diagram></mxfile>
<mxfile host="app.diagrams.net" modified="2020-04-21T15:41:04.814Z" agent="5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36" etag="aNeATTFI6XeDDM7JXMsW" version="13.0.0" type="device"><diagram id="Q6-cwq1KEDNFQR1_6Txc" name="Page-1">3Zldb9owFIZ/DdJ2MZTEJJDLlrK2UitVotLg0kvcxJuJkWO+9utnJ86HY6AsQGHlovI5dk7s9zzHTtIOGM7W9wzO42caItJxrHDdAXcdxxl4A/FXOja5o+f3ckfEcJi77Moxxn+QclrKu8AhSrWBnFLC8Vx3BjRJUMA1H2SMrvRhb5Tod53DCBmOcQCJ6f2BQx6rZblW5X9AOIqLO9uW6pnBYrBypDEM6armAqMOGDJKed6arYeISO0KXfLrvu/oLSfGUMIPuWDpxPYoxI/xg+9vYv66XiSjbyrKEpKFWvDNePj4+Cwnn8+abwopRDihujBuVzHmaDyHgexZibwLX8xnRFi2aJozK26DGEfrmkvN9B7RGeJsI4aoXlAIrLCxPWWvqiTYhbJxLQED5YMq71EZupJGNJQ6/6CUa+oRClKUSRmPaUQTSEaV95bRRRIiGdUSVjXmidK50uoX4nyjsIcLTg9UMqULFqA903VUrUAWIb5nHMjHybXszQtDBHK81Kvi5CI7Bo6SxOeni7Jo93UWS8YuxiIwZHqCr2hyUZVccG0qFUdITSZToSS8kaeEsAIC0xQHujBojfmk1p7KWu66yrpbq9LOjE1hJGL2k2qgNKf1vuqyzCqua1/sxcF4qmKvpczdkrHCd/CeoO7wQrFYWUlMz9aJKe0iRL5udVX9ZGsG8vRAwGsEyoUxAmVUlcs+AjTzFG0NmvUpQHOuC7RBY2tqC5pnNQL5HwyaeT4aoAULtsweOTKq3qOuIqjXc+oM2V3LBu9wlFkviGGxLsRqhE40eKc1xLeDnHJGf6MhJZRliwBW9hM9IUzjcjXtuQX/J7eX3iAFO3BTGzaXA9I9E/a2T7gqgzziaYvCfBo6UVHY2qZa7bG7ymEr+VZL8s8Pu31VsDf3VtBvCbsDQNcCfvXzGnHtrd0nLgXX15dj+x9RCr3jH0RaEHktBBnHvN912zHUb77ymaHOfdS7x6dSOzw/UV77/omy2gx07px6Rk45g0kq1EGp3LGpkWLx8s331WdCE/l6/4YJabggwVEiyRBZlQ9nt/JVHgeQ3KiOGQ5DsuvbgP7laichh38eaCax2A/rDPW3MORYu3E57utA//1keESK/5OJViRbX0K0RITOZ0KHr2fIFUFv/Ooy5fR8I1OD02RKmNX377zKqn8igNFf</diagram></mxfile>
Binary file modified docs/source/_static/images/py_asciimath_translations.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions examples/example2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
ASCIIMath2MathML,
ASCIIMath2Tex,
MathML2Tex,
Tex2ASCIIMath
)


Expand Down Expand Up @@ -34,4 +35,12 @@
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)
Loading

0 comments on commit b4d96f5

Please sign in to comment.