Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to parse math #8

Open
mcendu opened this issue Aug 21, 2022 · 1 comment
Open

Attempt to parse math #8

mcendu opened this issue Aug 21, 2022 · 1 comment

Comments

@mcendu
Copy link

mcendu commented Aug 21, 2022

Currently, aside from recognizing groups and environments, math is treated as a simple stream of tokens. As LaTeX is known for its excellent math equation generation, better parsing of math allows better formatting of math expressions, easing maintenance of documentation websites making heavy use of math notation.

Here is a rough idea on how to implement it, inspired in part by programming language parsers:

expr_unit "expression unit"
    = num+
    / char
    / whitespace
    / !binary_macro macro
    / begin_group b_expr end_group

power "superscript and subscript"
    = expr_unit
    / power "^" expr_unit
    / power "_" expr_unit

d_expr "delimited expression"
    = power
    / left_delimiter b_expr? right_delimiter

m_expr "multiplication expression"
    = power
    / m_expr power

u_expr "unary plus/minus expression"
    = m_expr
    / [+-] u_expr

b_expr "binary expression"
    = u_expr
    / b_expr binary_operator u_expr

A description in table form for the commons:

Operator Description
Superscript and subscript Normally ^ and _.
Logical delimitation Parentheses, brackets, \{, \}, and \left-\right pairs. The delimiter on the left and right can be different.
"Multiplication" Denoted by the lack of operators between two logical units. 2x is considered a "multiplication" expression, while 2 \times x is not.
Unary operators +, -, \neg, etc.
Binary operators +, -, =, \times, \le, etc.
@siefkenj
Copy link
Owner

This sounds like a nice idea! Whatever the parser is, it will have to be very error tolerant. People often misuse latex symbols. For example using |x| for absolute value of x and a|b for a divides b, etc.

You can play around at https://siefkenj.github.io/latex-parser-playground/ if you go to the "Debug" view, you can write a PEG grammar to reprocess items that have already been processed. It's a little more work to reprocess arrays with PEGjs (since it was designed for strings), but it is done several times in unified-latex. For example, https://github.com/siefkenj/unified-latex/blob/main/packages/unified-latex-util-pegjs/grammars/align-environment.pegjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants