Search code examples
pythonsympymathml

How to display Greek letters using sympy.printing.mathml


I use Python's Sympy module.

The Sympy module has a library called sympy.printing.mathml, which converts formulas into mathml.

However, I could not convert Greek letters such as α and β to mathml.

How can I solve this problem if I want to insert Greek letters into the formula?

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

I want to get mathml like example2 from python's formula like example1.

example1

S=abs(a)*(β-α)^3/6

example2

<math><mi>S</mi><mo>=</mo><mstyle displaystyle='true'><mfrac><mrow><mo>|</mo><mi>a</mi><mo>|</mo><mo>&#x2061;<!--FUNCTION APPLICATION--></mo><mo>(</mo><mo>&#x03b2;</mo><mo>-</mo><mo>&#x03b1;</mo><msup><mo>)</mo><mn>3</mn></msup></mrow><mrow><mn>6</mn></mrow></mfrac></mstyle></math>

I executed the following code.

from sympy import *
from sympy.printing.mathml import mathml
print(mathml(S=abs(a)*(β-α)^3/6,printer='presentation'))

Solution

  • In a terminal that supported copy/paste of Greek characters, I was able to create a variable with Greek name, e.g. beta = Symbol('beta') where beta was a literal Greek character. The output of the formula you gave -- using ** instead of ^ is identical to the code generated when the symbols for alpha and beta are imported from abc:

    >>> from sympy.abc import alpha, beta
    >>> print(mathml(abs(a)*(beta-alpha)**3/6, printer='presentation'))
    <mrow><mfrac><mrow><msup><mfenced><mrow><mrow><mo>-</mo><mi>&#945;</
    o>+</mo><mi>&#946;</mi></mrow></mfenced><mn>3</mn></msup><mo>&Invisi
    o><mrow><mfenced close="|" open="|"><mi>a</mi></mfenced></mrow></mro
    </mfrac></mrow>