4

It seems that MathML works OK with my word with simple copy and paste for strings such as

<math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mn>1</mn><mn>2</mn></mfrac></math>

But when I try to use sympy's mathml printer:

from sympy import S
from sympy.printing.mathml import mathml
my_eqn = S(1) / 2
print(mathml(my_eqn))

The output is:

<apply><divide/><cn>1</cn><cn>2</cn></apply>

And I cannot copy and paste it into word to make it a Word equation.

Could anyone please help?

Yuxiang Wang
  • 8,095
  • 13
  • 64
  • 95
  • 1
    I don't have MS Word. Can it render XHTML? I'm thinking of http://tiarno.github.io/plastex/. – Bill Bell Dec 02 '16 at 16:20
  • 1
    @BillBell Thanks for the idea! I actually used the latex printer in sympy and converted them to word file directly using pandoc (in fact there is the nice package of `pypandoc`). :) – Yuxiang Wang Dec 02 '16 at 17:31
  • And thank you! Made me laugh to see what that can do. – Bill Bell Dec 02 '16 at 19:00

2 Answers2

5

Looks like the MathML that works is presentation MathML, whereas SymPy outputs content MathML. Unsurprisingly, Word is unable to convert from content to presentation, as that requires some degree of mathematical knowledge on the part of the software.

SymPy probably ought to support outputting presentation format, but until that is implemented, you might try to find some other software that can convert between the two (I don't know of any myself, unfortunately).

asmeurer
  • 86,894
  • 26
  • 169
  • 240
4

SymPy supports presentation MathML now. For that you use the arg printer="presentation"

mathml(expr, printer="presentation")

You should put the output inside the <math> tag:

<math xmlns = "http://www.w3.org/1998/Math/MathML">
</math>
nicoguaro
  • 3,629
  • 1
  • 32
  • 57