asnunes / mathml-to-latex

A JavaScript tool to convert mathml string to LaTeX equation string.
MIT License
51 stars 14 forks source link

Make compatible with MS Word equations #21

Closed jgostick closed 4 months ago

jgostick commented 6 months ago

Hi there, I have been using this tool to copy equations from MS Word to Latex (for use in Quarto). Word has an option for copying equations as MML instead of text. There is only one problem...the Word MML has a mml: appended to the beginning of each MML tag, so what should be <mrow> is <mml:mrow>. I am not good at JS, but I did some Googling and figured out how to fix this...you just need to change this line:

const latex = MathMLToLaTeX.MathMLToLaTeX.convert(mathmlInput.value.replace(/mml:/g, ''))

Adding the .replace(/mml:/g, '') removes all the tags that Word inserts and voila, the conversion works perfectly. I have made a silly webpage here, but I think it would be very helpful is this little trick were added to the main code base. I am not good enough with JS to even know where is best to add this so I didn't bother with a PR, sorry!

asnunes commented 6 months ago

Hi, @jgostick. Can you give me more examples of input with the prepend mml:? I can try some code and check if it handles properly

jgostick commented 5 months ago

Hey, here is an example of MML from Word:

<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"><mml:msub><mml:mrow><mml:mi>V</mml:mi></mml:mrow><mml:mrow><mml:mi>i</mml:mi></mml:mrow></mml:msub><mml:mfrac><mml:mrow><mml:mi mathvariant="normal">Δ</mml:mi><mml:msubsup><mml:mrow><mml:mi>C</mml:mi></mml:mrow><mml:mrow><mml:mi>A</mml:mi><mml:mo>,</mml:mo><mml:mi>i</mml:mi></mml:mrow><mml:mrow><mml:mi>t</mml:mi></mml:mrow></mml:msubsup></mml:mrow><mml:mrow><mml:mi mathvariant="normal">Δ</mml:mi><mml:mi>t</mml:mi></mml:mrow></mml:mfrac><mml:mo>=</mml:mo><mml:mrow><mml:munderover><mml:mo stretchy="false">∑</mml:mo><mml:mrow><mml:mi>j</mml:mi><mml:mo>=</mml:mo><mml:mi>k</mml:mi></mml:mrow><mml:mrow><mml:mi>N</mml:mi></mml:mrow></mml:munderover><mml:mrow><mml:msubsup><mml:mrow><mml:mi>G</mml:mi></mml:mrow><mml:mrow><mml:mi>i</mml:mi><mml:mo>,</mml:mo><mml:mi>j</mml:mi></mml:mrow><mml:mrow><mml:mi>D</mml:mi></mml:mrow></mml:msubsup><mml:mfenced separators="|"><mml:mrow><mml:msub><mml:mrow><mml:mi>C</mml:mi></mml:mrow><mml:mrow><mml:mi>A</mml:mi><mml:mo>,</mml:mo><mml:mi>j</mml:mi></mml:mrow></mml:msub><mml:mo>-</mml:mo><mml:msub><mml:mrow><mml:mi>C</mml:mi></mml:mrow><mml:mrow><mml:mi>A</mml:mi><mml:mo>,</mml:mo><mml:mi>i</mml:mi></mml:mrow></mml:msub></mml:mrow></mml:mfenced></mml:mrow></mml:mrow></mml:math>

BTW, you can visit my online converter to see it in action: https://jgostick.github.io/mml2latex/

asnunes commented 4 months ago

Implemented #23