问题
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