Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
ralinres opened this issue Aug 19, 2024 · 1 comment

Comments

@ralinres
Copy link

ralinres commented Aug 19, 2024

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
Copy link
Owner

asnunes commented Nov 2, 2024

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.

@asnunes asnunes closed this as completed Nov 2, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants