【发布时间】:2019-09-27 18:22:21
【问题描述】:
我正在尝试在 Jupyter 下生成的 MathJax 中使用自定义属性制作 sympy 渲染表达式。如果我使用 IPython.display.HTML 显式渲染它,我可以让它工作,但是我希望将其设为 sympy 渲染表达式的默认方式。
from sympy.printing.mathml import MathMLPresentationPrinter
from sympy import init_printing,Symbol,Function
from sympy.abc import x,y,z
from IPython.display import HTML
class MyMathMLPresentationPrinter(MathMLPresentationPrinter):
def _print(self,expr):
res=super()._print(expr)
res.attributes['myattrib']='myvalue'
return(res)
def doprint(self, expr):
mathML = self._print(expr)
unistr = mathML.toxml()
xmlbstr = unistr.encode('ascii', 'xmlcharrefreplace')
res = xmlbstr.decode()
return res
F=Function('F')
expr=F(x).diff(x)
我希望的结果可以使用(您需要使用 Firefox 中的 Inspector 或类似工具来查看)。
ml=MyMathMLPresentationPrinter()._print(expr).toxml()
HTML('<math>'+ml+'</math>')
我尝试执行以下操作,但这并没有解决问题。
def my_print(expr, **settings):
ml= MyMathMLPresentationPrinter().doprint(expr)
return ml
sympy.init_printing(pretty_printer=my_print,pretty_print=True,use_latex=False)
expr
关于如何进行这项工作的任何建议?
谢谢
【问题讨论】:
标签: python sympy pretty-print