asnunes / mathml-to-latex

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

Fix: ensure correct handling of mtable in LaTeX output #24

Open yuxuan-wei118 opened 3 months ago

yuxuan-wei118 commented 3 months ago

The original code may mistranslate the tag. I fixed the issue by using array{ccc} to express the final output, and it should be able to translate to Latex correctly now. I also add a few tests to prove that.

yuxuan-wei118 commented 3 months ago

Although it can translate correctly, the new update code fails to pass a few tests involving the tag, which may have already been removed from the relevant web standards. So, any comment or recommended approach to fix the issue?

asnunes commented 2 months ago

Hi, @yuxuan-wei118. Thanks for your contribution! However, the issue is not clear to me. Besides the test you added, could you provide some examples of input, the expected result, and the result before the changes, highlighting the broken/missing part? I would like to understand the problem a little better.

yuxuan-wei118 commented 2 months ago

Hi @asnunes. Thank you for your reply! One mistranslating example is the code below.

<math display="block">
  <mfrac>
    <mi>A</mi>
    <mn>2</mn>
  </mfrac>
  <mo>=</mo>
  <mrow>
    <mo>(</mo>
    <mtable>
      <mtr>
        <mtd><mn>1</mn></mtd>
        <mtd><mn>2</mn></mtd>
        <mtd><mn>3</mn></mtd>
      </mtr>
      <mtr>
        <mtd><mn>4</mn></mtd>
        <mtd><mn>5</mn></mtd>
        <mtd><mn>6</mn></mtd>
      </mtr>
      <mtr>
        <mtd><mn>7</mn></mtd>
        <mtd><mn>8</mn></mtd>
        <mtd><mn>9</mn></mtd>
      </mtr>
    </mtable>
    <mo>)</mo>
  </mrow>
</math>

The expected result should be that: Expected result

However, the original code will translate the input into the following: Original result which is an illegal syntax missing \begin{array} {ccc} and end {array}.

Please feel free to give more comments!

yuxuan-wei118 commented 1 week ago

Hi @asnunes.

  1. For the first concern, I think we should change code to make these tests pass.

  2. For the second issue, the addition of hspace aims to address any possible additional formatting syntax that may appeared in mtable tag.

Here is an example mathml code that you can see mtable may include additional formatting syntax: <math xmlns="http://www.w3.org/1998/Math/MathML"><mtable columnwidth="3em 0.05em 3em 3em 3em 3em" columnspacing="0.0em" rowspacing="0.05ex" rowlines="solid" columnlines="solid" frame="solid" framespacing="0.0em 0ex"><mtr><mtd><mi>x</mi></mtd><mtd><mspace width="0.0em" height="2.5ex" depth="1.0ex"/></mtd><mtd><mo>&#x2212;</mo><mn>1</mn></mtd><mtd><mn>0</mn></mtd><mtd><mn>1</mn></mtd><mtd><mn>2</mn></mtd></mtr><mtr><mtd><mi>g</mi><mrow><mo>(</mo><mi>x</mi><mo>)</mo></mrow></mtd><mtd><mspace width="0.0em" height="2.5ex" depth="1.0ex"/></mtd><mtd><mn>6</mn></mtd><mtd><mn>4</mn></mtd><mtd><mn>2</mn></mtd><mtd><mo>&#x2212;</mo><mn>1</mn></mtd></mtr><mtr><mtd><msup><mi>g</mi><mo>'</mo></msup><mrow><mo>(</mo><mi>x</mi><mo>)</mo></mrow></mtd><mtd><mspace width="0.0em" height="2.5ex" depth="1.0ex"/></mtd><mtd><mo>&#x2212;</mo><mn>1</mn></mtd><mtd><mo>&#x2212;</mo><mn>7</mn></mtd><mtd><mo>&#x2212;</mo><mn>2</mn></mtd><mtd><mo>&#x2212;</mo><mn>3</mn></mtd></mtr></mtable></math> Adding hspace makes the output Latex code closer to the intended format given by mathml. But it is also true that it may be redundant in some cases.

  1. For the third issue, we can do this after resolving all issues above.

Please feel free to give more comments!