christianp / asciimath2tex

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

Matrices in parentheses not rendering correctly #26

Closed jamiemlaw closed 7 months ago

jamiemlaw commented 1 year ago

I'm using asciimath2tex@1.4.0. There seems to be something funky going on if I wrap a matrix in parentheses. I noticed this when trying to mark up a matrix determinant with the following input:

parser.parse('det([[a,b],[c,d]])');

Expected output:

'\det{\left ( \left [ \begin{matrix} a & b \\ c & d \end{matrix} \right ] \right )}'

Actual output:

'\det{\left ( \begin{matrix} \left [ a , b \right ] & \left [ c , d \right ] \end{matrix} \right )}'

It seems that the minimal test case that fails is

parser.parse('([[a],[b]])');

Expected output:

'\left ( \left [ \begin{matrix} a \\ b \end{matrix} \right ] \right )'

Actual output:

'\left ( \begin{matrix} \left [ a \right ] & \left [ b \right ] \end{matrix} \right )'
jamiemlaw commented 1 year ago

Found a workaround:

parser.parse('([{:[a],[b]:}])');

which produces

'\left ( \left [ \left . \begin{matrix} a \\ b \end{matrix} \right . \right ] \right )'
christianp commented 7 months ago

I've had this issue in my inbox for exactly a year, and today I came up with a solution! The root of the problem was that my parser works in a completely different order to the original AsciiMathML, so I couldn't just copy their logic for spotting matrices. I realised that you want to identify a matrix as deep into nested brackets as possible, so I added a part to the grammar to look for a matrix wrapped in extra brackets before just looking for a matrix, allowing it to consume the outer brackets first.

jamiemlaw commented 7 months ago

Thank you so much for taking the time to have a look at this; hope it wasn't too taxing. I've tested it out and it look to be working well. Keep up the awesome work. :-)