Monday, July 27, 2020

LatexML for converting Latex math to Content MathML

Rather than creating a Dockerfile, here's a one-liner:

docker run -it --rm phusion/baseimage:0.11 /bin/bash -c "apt update && apt install -y latexml libtext-unidecode-perl && latexmlmath --cmml=- A \\cdot B"

The downside is that if you got the command wrong, the complete build process is necessary.


Here are two examples of LatexML converting Latex math into Content MathML:

First, the quadratic formula (which features some ambiguity)

latexmlmath --cmml=- \\frac{b\\pm\\sqrt{b^2-4ac}}{2a}
<?xml version="1.0" encoding="UTF-8"?>
<math xmlns="http://www.w3.org/1998/Math/MathML" alttext="\frac{b\pm\sqrt{b^{2}-4ac}}{2a}" display="block">
  <apply>
    <divide/>
    <apply>
      <csymbol cd="latexml">plus-or-minus</csymbol>
      <ci>𝑏</ci>
      <apply>
        <root/>
        <apply>
          <minus/>
          <apply>
            <csymbol cd="ambiguous">superscript</csymbol>
            <ci>𝑏</ci>
            <cn type="integer">2</cn>
          </apply>
          <apply>
            <times/>
            <cn type="integer">4</cn>
            <ci>𝑎</ci>
            <ci>𝑐</ci>
          </apply>
        </apply>
      </apply>
    </apply>
    <apply>
      <times/>
      <cn type="integer">2</cn>
      <ci>𝑎</ci>
    </apply>
  </apply>
</math>

The following Latex math is ambiguous, so LatexML is not able to do much.

latexmlmath --cmml=- A \\cdot B
<?xml version="1.0" encoding="UTF-8"?>
<math xmlns="http://www.w3.org/1998/Math/MathML" alttext="A\cdot B" display="block">
  <apply>
    <ci>⋅</ci>
    <ci>𝐴</ci>
    <ci>𝐵</ci>
  </apply>
</math>

No comments:

Post a Comment