Rendering coefficients as fractions in sympy latex

≯℡__Kan透↙ 提交于 2021-02-19 09:01:31

问题


I would like to render a fraction multiplied by an integral. I tried:

from sympy import *
x, h = symbols("x h")
(2/h) * Integral(x**2, (x, 0, 1))

The generated latex is:

'\\frac{2 \\int\\limits_{0}^{h} x^{2}\\, dx}{h}'

which looks wretched:

Is it possible to keep the fraction as a coefficient, like so?

This can be reproduced easily using sympy live. Thanks for any help.


回答1:


You can, as @Stelios pointed out, use an unevaluated Mul to make the expression appear as you want it. To avoid retyping the expression, and to just pull everything outside of the Integral to the front, you can use the following:

>>> latex(Mul(*eq.as_independent(Integral, as_Add=False),evaluate=False))
\frac{2}{h} \int\limits_{0}^{1} x^{2}\, dx

The as_independent method separates terms or factors that are independent of something -- in this case Integral. The unevaluated Mul puts them back together but keeps them from reordering.



来源:https://stackoverflow.com/questions/57236487/rendering-coefficients-as-fractions-in-sympy-latex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!