christianp / asciimath2tex

JavaScript library to convert AsciiMath to TeX
Apache License 2.0
68 stars 16 forks source link

Issue involving parentheses #6

Closed fabiospampinato closed 2 years ago

fabiospampinato commented 5 years ago

I have the following AsciiMath expression:

P(B|A) = (P(A|B) * P(B))/(P(A))

Which this library converts to:

P \left( B \left \lvert A \right ) = \left( P \frac{A \left \lvert B \right ) \cdot P \left( B \right )}{P \left( A \right )} \right. \right.

Then rendering it via KaTeX I get:

Screen Shot 2019-07-01 at 00 25 35

While instead I should get:

image

I'm no TeX expert, I guess the generated TeX isn't quite right? 🤔

GiovanniSalmeri commented 2 years ago

@fabiospampinato I am trying to resolve this little bug. The problem apparently lies in the vertical bar, which is considered a leftright_bracket even when, as in this case, it is actually a binary operator. The same expression without the bars is parsed correctly. More or less connected seems the only other test which fails, that is (a,b]={x in RR | a < x <= b}: even if visually the result is accettable, the formula is parsed incorrectly. But the code is rather complex... @christianp, do you have any hint in the right direction?

Edit: I have published Math, an extension for the CMS Yellow. I translated for it asciimath2tex.js in PHP, it works very well!

christianp commented 2 years ago

@GiovanniSalmeri thanks for looking at this. It's a very tricky problem! I looked at this a year or two ago, but didn't get very far.

The reason this is a problem for my parser is that its grammar doesn't make it easy to deal with an ambiguous delimiter. I'll have another think about it now, but I don't have much hope!

GiovanniSalmeri commented 2 years ago

@christianp Thank you very much for investigating it! I suspect that in fact ASCIImath syntax is in this inherently ambiguous. Perhaps the easy way for avoiding this problem would be to modify the ASCIImath syntax and introduce different tokens for the vertical bars as delimiters (for example :| and |:), or disallowing them altogether (in the end, they can be written with abs()). Perhaps it would be a better compromise. Just thinking aloud!

christianp commented 2 years ago

I've had a closer look at the output of the standard asciimath parser, and I think it doesn't correctly identify when | is used as a left or right delimiter for a bracketed expression, when the other delimiter is a different symbol. For example, (x| produces

<mrow><mo>(</mo><mi>x</mi><mrow><mo>∣</mo></mrow></mrow>

while (x) produces

<mrow><mo>(</mo><mi>x</mi><mo>)</mo></mrow>

and |x| produces

<mrow><mo>|</mo><mi>x</mi><mo>|</mo></mrow>

but MathJax correctly makes the delimiters stretchy in all cases, so it must be doing something to the MathML that asciimath produces.

I'm trying to work out how to replicate that in one go, in this parser!

pkra commented 2 years ago

MathJax correctly makes the delimiters stretchy in all cases, so it must be doing something to the MathML that asciimath produces.

As opposed to TeX, in MathML everything defaults to stretchy.

christianp commented 2 years ago

Thanks, @pkra! So it stretches based on what is in the same <mrow>?

pkra commented 2 years ago

So it stretches based on what is in the same ?

Yes (the relevant row may be implicit).