A simple converter from ASCIIMath to LaTeX
Project description
ASCIIMATH
AsciiMath is an easy-to-write markup language for mathematics: for more information check out the main website at ASCIIMath.
The parser utility takes an ASCIIMath string in input and returns the corresponding LaTeX translation, via a syntactic and semantic transformation.
Install
To install the package run pip install py-asciimath
Usage
Right now it's only suppported as python module, so:
from py_asciimath.parser.parser import ASCIIMath2Tex
from py_asciimath.parser.const import *
if __name__ == "__main__":
parser = ASCIIMath2Tex(
asciimath_grammar,
inplace=False,
parser="lalr",
lexer="contextual",
)
asciimath = "sum_(i=1)^n i^3=((n(n+1))/2)^2"
latex = parser.asciimath2tex(asciimath, pprint=True)
print(latex)
results in:
\sum_{i = 1}^{n} i^{3} = \left(\frac{\left(n \left(n + 1\right)\right)}{2}\right)^{2}
Grammar
The grammar used to parse the input is:
start: i start* -> exp
i: s -> exp_interm
| s "/" s -> exp_frac
| s "_" s -> exp_under
| s "^" s -> exp_super
| s "_" s "^" s -> exp_under_super
s: l start? r -> exp_par
| u s -> exp_unary
| b s s -> exp_binary
| latex -> symbol
| c -> const
| QS -> q_str
c: /d[A-Za-z]/ // derivatives
| NUMBER
| LETTER
l: {} // left parenthesis
r: {} // right parenthesis
b: {} // binary functions
u: {} // unary functions
latex: {}
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.
Rendering (semantics)
A parsed ASCIIMath string is rendered as follows:
latex,uandcsymbols are converted to their LaTeX equivalenttextandulcorrespond to the\textrmand\underlinefunctionsbb,bbb,cc,tt,frandsfcorrespond to the\boldsymbol,\mathbb,\mathcal,\texttt,\mathfrakand\textsffunctionsfracis rendered as a fraction,root n xas the n-th root of x andstackrel x ydisplays x upon y- Any text placed between a pair of
"is rendered in the same font as normal text. /stands for a fraction. The_and^tokens have the same behaviour as in LaTeX but the subscript must be placed before the superscript if they are both present
Delimiters
Left and right delimiters are preceded by the \left and \right commands to be well-sized. (: and :) are chevrons (angle parenthesis). {: and :} are invisible delimiters like LaTeX's {. |: is converted to \lvert , while ||: is converted to \lVert. The other delimiters are rendered as expected.
Useless delimiters are automatically removed in expressions like:
(...)/(...)(...)_(...),(...)^(...)and the combination of sub and superscriptu (...),b (...) (...)where u and b are unary and binary operators
If you want them to be rendered, you have to double them, for example: ((x+y))/2 or {: (x+y) :}/2.
Matrices and systems of equations
For a text to be rendered as a matrix must have a structure like
L '[' ... (, ...)* ']', '[' ... (, ...)* ']' (, '[' ... (, ...)* ']' )* R
or
L '(' ... (, ...)* ')', '(' ... (, ...)* ')' (, '(' ... (, ...)* ')' )* R
that is:
- It must be delimited by a left (
L) and right (R) parenthesis - Every row can be separated by
[]XOR()(if one starts with[], every row will be recognized with the same parenthesis, same for()), followed by,and possibly another row - Every matrix must contain at least two rows
- Every rows contains zero or more columns, where
...can be any ASCIIMath expression - Every row must contain the same number of columns
Since L and R can be any left or right parenthesis, and every matrices must have the same number of columns, to render a system of equation one can write something like {[(root n x)/(x) <= 4], [x^2=e^x]:}.
On the other hand a matrix can be somenthing like [[(root n x)/(x) <= 4, int x dx], [x^2=e^x, lim_(x to infty) 1 / (x^2)]].
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_asciimath-0.0.2-py2.py3-none-any.whl.
File metadata
- Download URL: py_asciimath-0.0.2-py2.py3-none-any.whl
- Upload date:
- Size: 15.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3.post20200330 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
118f8835312a45db2cb2cb66f49c772046c5c7b985030022103dcb14b1a0b219
|
|
| MD5 |
167bbce66facc3ecf4c6b0aab4661283
|
|
| BLAKE2b-256 |
5da383ddc2d911696a10151b08de1aef9e85793eb0963868a295aeb13ac62c09
|