I am generating an expression with two fractions, and want to pretty print as a whole expression with LaTeX, to then put on a worksheet.
E.g. in the form:
(5/7) * (3/4).
However, when I do the following:
fract1 = sympy.sympify(Fraction(5,7))
fract2 = sympy.sympify(Fraction(3,4))
expression = sympy.Mul(fract1,fract2,evaluate=False)
It returns
5*3/(7*4)
Clearly it is combining the fraction but not actually evaluating, but I want to be able to produce it in a format suitable as a question for a maths worksheet.
The next SymPy version will have UnevaluatedExpr
:
In [4]: uexpr = UnevaluatedExpr(S.One*5/7)*UnevaluatedExpr(S.One*3/4)
In [7]: uexpr
Out[7]: 5/7⋅3/4
To release and evaluate it, just use .doit()
:
In [8]: uexpr.doit()
Out[8]:
15
──
28
LaTeX output looks like:
In [10]: print(latex(uexpr))
\frac{5}{7} \frac{3}{4}
This feature is available since SymPy 1.1. See the documentation to find out more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With