alexander-c-b / AsciiMath

Compiler from asciimath to LaTeX
MIT License
0 stars 0 forks source link

Units syntax #11

Open alexander-c-b opened 3 years ago

alexander-c-b commented 3 years ago

For instance, in physics, numbers like 6.626 * 10^-19 J*s are quite common, and encoding units like J*s manually in text is clunky (" " ("kg"*"m")/("s"^(-1)), for instance).

Examples of syntax:

unit(m/s^2)
num(6.626e-19 J*s)
`6.626e-19 J*s`
unit(kg*m*s^-1)
pi/2 `kg/m`

The above items will become:

{\frac{\text{m}}{\text{s}^{2}}}
{6.626\times 10^{-19}\, \text{J}\cdot \text{s}}
{6.626\times 10^{-19}\, \text{J}\cdot \text{s}}
{\text{kg}\cdot \text{m}\cdot \text{s}^{-1}}
\frac{\pi}{2}{\frac{\text{kg}}{\text{m}}}

These can be parsed according to the rules below. Specific recognition of the letter "e" may need to be added to the lexer; in this case, the parser can convert it back to the standard Letters "e" under the constant rule.

Note that in the following specifications brackets ([...]) represent optional items.

Rules

Units

unitFunc   := 'unit' ldel unit rdel
unit       := simpleUnit ['/' simpleUnit]
simpleUnit := singleUnit ['*' simpleUnit]
singleUnit := LETTERS ['^' ['-'] NUMBER]

Num

numFunc := 'num' ldel num rdel
numTics := '`' num '`'
num     := NUMBER ['e' ['-'] NUMBER] [unit] | unit
alexander-c-b commented 3 years ago

12 will be necessary to keep expressions succinct; e.g., expressions like kg/m should be parsed as (kg)/m and not k (g/m).

alexander-c-b commented 3 years ago

This was accidentally closed resolving #3.