Search code examples
rmathml

R how to convert mathml to an r equation


I have a mathml version (below, copied from a pdf) of an equation I'd like to use in R. I've tried XML and xml2 packages but they don't seem to have a function to do this. The final function would look something like the following

HI <- -42 + 2.04 * RH + .22 * T^2 ...

Here's the mathml version.

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
  <mtable columnalign="left" rowspacing="4pt" columnspacing="1em">
    <mtr>
      <mtd>
        <mrow class="MJX-TeXAtom-ORD">
          <mrow class="MJX-TeXAtom-ORD">
            <mi mathvariant="normal">H</mi>
            <mi mathvariant="normal">I</mi>
          </mrow>
        </mrow>
        <mo>=</mo>
        <mo>&#x2212;<!-- − --></mo>
        <mn>42.379</mn>
        <mo>+</mo>
        <mn>2.04901523</mn>
        <mo>&#x00D7;<!-- × --></mo>
        <mrow class="MJX-TeXAtom-ORD">
          <mrow class="MJX-TeXAtom-ORD">
            <mi mathvariant="normal">R</mi>
            <mi mathvariant="normal">H</mi>
          </mrow>
        </mrow>
        <mo>&#x2212;<!-- − --></mo>
        <mn>10.14333127</mn>
      </mtd>
    </mtr>
    <mtr>
      <mtd>
        <mspace width="thinmathspace" />
        <mspace width="-65pt" />
        <mo>&#x00D7;<!-- × --></mo>
        <mrow class="MJX-TeXAtom-ORD">
          <mrow class="MJX-TeXAtom-ORD">
            <mi mathvariant="normal">R</mi>
            <mi mathvariant="normal">H</mi>
          </mrow>
        </mrow>
        <mo>&#x2212;<!-- − --></mo>
        <mn>0.22475541</mn>
        <mo>&#x00D7;<!-- × --></mo>
        <mi>T</mi>
      </mtd>
    </mtr>
    <mtr>
      <mtd>
        <mspace width="thinmathspace" />
        <mspace width="30pt" />
        <mo>&#x00D7;<!-- × --></mo>
        <mrow class="MJX-TeXAtom-ORD">
          <mrow class="MJX-TeXAtom-ORD">
            <mi mathvariant="normal">R</mi>
            <mi mathvariant="normal">H</mi>
          </mrow>
        </mrow>
        <mo>&#x2212;<!-- − --></mo>
        <mn>0.00683783</mn>
        <mo>&#x00D7;<!-- × --></mo>
        <msup>
          <mi>T</mi>
          <mn>2</mn>
        </msup>
        <mo>&#x2212;<!-- − --></mo>
        <mn>0.05481717</mn>
        <mo>&#x00D7;<!-- × --></mo>
        <mrow class="MJX-TeXAtom-ORD">
          <msup>
            <mrow class="MJX-TeXAtom-ORD">
              <mi mathvariant="normal">R</mi>
              <mi mathvariant="normal">H</mi>
            </mrow>
            <mrow class="MJX-TeXAtom-ORD">
              <mn>2</mn>
            </mrow>
          </msup>
        </mrow>
      </mtd>
    </mtr>
    <mtr>
      <mtd>
        <mspace width="thinmathspace" />
        <mspace width="20pt" />
        <mo>+</mo>
        <mn>0.00122874</mn>
        <mo>&#x00D7;<!-- × --></mo>
        <msup>
          <mi>T</mi>
          <mn>2</mn>
        </msup>
        <mo>&#x00D7;<!-- × --></mo>
        <mrow class="MJX-TeXAtom-ORD">
          <mrow class="MJX-TeXAtom-ORD">
            <mi mathvariant="normal">R</mi>
            <mi mathvariant="normal">H</mi>
          </mrow>
        </mrow>
        <mo>+</mo>
        <mn>0.00085282</mn>
        <mo>&#x00D7;<!-- × --></mo>
        <mi>T</mi>
      </mtd>
    </mtr>
    <mtr>
      <mtd>
        <mspace width="thinmathspace" />
        <mspace width="-25pt" />
        <mo>&#x00D7;<!-- × --></mo>
        <mrow class="MJX-TeXAtom-ORD">
          <msup>
            <mrow class="MJX-TeXAtom-ORD">
              <mi mathvariant="normal">R</mi>
              <mi mathvariant="normal">H</mi>
            </mrow>
            <mrow class="MJX-TeXAtom-ORD">
              <mn>2</mn>
            </mrow>
          </msup>
        </mrow>
        <mo>&#x2212;<!-- − --></mo>
        <mn>0.00000199</mn>
        <mo>&#x00D7;<!-- × --></mo>
        <msup>
          <mi>T</mi>
          <mn>2</mn>
        </msup>
        <mo>&#x00D7;<!-- × --></mo>
        <mrow class="MJX-TeXAtom-ORD">
          <msup>
            <mrow class="MJX-TeXAtom-ORD">
              <mi mathvariant="normal">R</mi>
              <mi mathvariant="normal">H</mi>
            </mrow>
            <mrow class="MJX-TeXAtom-ORD">
              <mn>2</mn>
            </mrow>
          </msup>
        </mrow>
      </mtd>
    </mtr>
  </mtable>
</math>

Solution

  • My solution, which is not elegant and only partial, is the following

    1. write the mathml code into a text file. I'm calling it mathml.txt.

    2. Run the following code

      library(xml2) xmlin <- read_xml(x = "mathml.txt") write_html(xmlin, "test.html") This creates an html file that has the equation in it. At a minimum, you can copy and paste it into a text editor to create an R equation.