asnunes / mathml-to-latex

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

Unnecessary \left(\right.) and \left.\right) Pairs in Converted LaTeX from MathML #26

Closed ralinres closed 4 weeks ago

ralinres commented 3 months ago

Description:

When converting certain MathML equations to LaTeX using the mathml-to-latex library, the output LaTeX includes unnecessary \left(\right.) and \left.\right) delimiters even when there is no content inside the parentheses. Additionally, spaces between words in mtext elements are handled inconsistently, resulting in the inclusion of \textrm{ } for a simple space.

Example Input MathML:

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
  <semantics>
    <mrow>
      <msub>
        <mtext>Required Value</mtext>
        <mtext>other</mtext>
      </msub>
      <mo>≥</mo>
      <mfrac>
        <mrow>
          <mn>21</mn>
          <mi>f</mi>
          <msup>
            <mi>t</mi>
            <mn>3</mn>
          </msup>
        </mrow>
        <mrow>
          <mi>A</mi>
          <mi>C</mi>
          <mi>H</mi>
        </mrow>
      </mfrac>
      <mo>⋅</mo>
      <mo fence="true">(</mo>
      <mfrac>
        <msub>
          <mi>I</mi>
          <mi>o</mi>
        </msub>
        <mrow>
          <mn>1000</mn>
          <msub>
            <mi>B</mi>
            <mrow>
              <mtext>Btu</mtext>
              <mi mathvariant="normal">/</mi>
              <mtext>h</mtext>
            </mrow>
          </msub>
        </mrow>
      </mfrac>
      <mo fence="true">)</mo>
    </mrow>
  </semantics>
</math>

Output LaTeX: \left(\text{Required}\textrm{ }\text{Value}\right)_{\text{other}} \geq \frac{21 f t^{3}}{A C H} \cdot \left(\right. \frac{I_{o}}{1000 B_{\text{Btu} / \text{h}}} \left.\right) Expected LaTeX: \text{Required Value}_{\text{other}} \geq \frac{21 f t^{3}}{\text{ACH}} \cdot \left( \frac{I_{o}}{1000 B_{\text{Btu}/\text{h}}} \right)

asnunes commented 4 weeks ago

Hi @ralinres! Thanks for bringing this up.

I looked into it, and while I couldn't get exactly the same output you saw, here’s what I got:

\\left(\\text{Required Value}\\right)_{\\text{other}} \\geq \\frac{21 f t^{3}}{A C H} \\cdot \\left(\\right. \\frac{I_{o}}{1000 B_{\\text{Btu} / \\text{h}}} \\left.\\right)

Notice there’s no \textrm{ } between "Required" and "Value," but I do see the extra \\left( and \\right) showing up unnecessarily. This library is designed to prioritize valid LaTeX syntax, so sometimes it errs on the side of being a bit verbose.

That said, I agree there’s room to clean this up! I made some changes to cut down on unneeded characters. Now, with the latest release, the output for your MathML looks like this:

\\text{Required Value}_{\\text{other}} \\geq \\frac{21 f t^{3}}{A C H} \\cdot \\left(\\right. \\frac{I_{o}}{1000 B_{\\text{Btu} / \\text{h}}} \\left.\\right)

You’ll see that we no longer add parentheses around single child commands. However, the \\left(\\right. and \\left.\\right) are still there. This is expected based on your MathML, which uses separate, childless <mo fence="true">(</mo> and <mo fence="true">)</mo>.

To avoid these in the future, I’d recommend switching to mfenced elements, which better keep opening and closing characters paired.

These changes are live in the version v1.4.2.