渲染系数作为Sympy乳胶中的分数 [英] Rendering coefficients as fractions in sympy latex

查看:43
本文介绍了渲染系数作为Sympy乳胶中的分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想渲染一个分数乘以一个整数.我试过了:

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))

生成的乳胶为:

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

看起来很可悲:

是否可以像这样将分数保留为系数?

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

可以使用 sympy live 轻松复制.感谢您的帮助.

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

推荐答案

正如@Stelios所指出的,您可以使用未经评估的Mul来使表达式按需要显示.为了避免重新键入该表达式,并且只是将Integral之外的所有内容都拉到最前面,可以使用以下命令:

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

as_independent 方法将与某物(在本例中为Integral)无关的项或因子分开.未被评估的Mul将它们放回原处,但阻止它们重新排序.

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.

这篇关于渲染系数作为Sympy乳胶中的分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆