我有一个字符串格式的SymPy表达式
-t*(a+b+c)/2
当以LaTeX形式(包括笔记本的LaTeX输出)进行漂亮打印时,它太高而且不容易阅读.
如何组合分数并将其放在括号之前?像这样:
(-t/2)*(a+b+c)
代码示例:
from sympy import symbols,Function,Derivative,var,init_printing,pprint,latex
init_printing()
def T(y):
var('mu')
return -1 / (2 * mu) * Derivative(y,x,x)
def V(y):
var('x')
V = Function('V',commutative=True)(x)
return V * y
def K(y):
var('x')
K = Function('K',commutative=True)(x)
return K * y
def a1(y):
var('tau',positive=True)
return tau * (T(y) + V(y))
def c(A,B):
def comm(y):
return A(B(y)) - B(A(y))
return comm
var('x')
var('t')
f = Function('psi',commutative=False)
c1k = c(a1,K)
print(latex(c1k(f(x,t)).doit().simplify()))
打印和显示
- \frac{\tau \left(2 \frac{d}{d x} K{\left (x \right )} \frac{\partial}{\partial x} \psi{\left (x,t \right )} + \frac{d^{2}}{d x^{2}} K{\left (x \right )} \psi{\left (x,t \right )}\right)}{2 \mu}
我想在latex()和Jupyter笔记本中看到它们
-\frac{\tau}{2}\left(...\right)
最佳答案
SymPy 1.1.1默认断开多个分段.这导致complaint导致PR在当前主分支中禁用此行为.
要重新启用先前的默认值,请使用long_frac_ratio = 2:
The allowed ratio of the width of the numerator to the width of the denominator before we start breaking off long fractions. – 07002
>>> print(latex(c1k(f(x,t)).doit().simplify(),long_frac_ratio=2))
- \frac{\tau}{2 \mu} \left(2 \frac{d}{d x} K{\left (x \right )} \frac{\partial}{\partial x} \psi{\left (x,t \right )}\right)
设置可以包含在init_printing中:
init_printing(long_frac_ratio=2)