问题
I want to get latex output from a sympy expression without calculating the expression. For instance if I do latex((2+3)/7) the output would be 5/7 (with latex), but what I am after is for it to just output (2+3)/7 with latex. Like this: \frac{2+3}{7}
回答1:
You can manually adjust the long_frac_ratio
long_frac_ratio: The allowed ratio of the width of the numerator to the width of the denominator before we start breaking off long fractions. The default value is 2.
>>> latex(e,long_frac_ratio=3)
'\\frac{2 + 3}{7}'
回答2:
You can try to sympify the expression with keyword evaluate=False:
>>> latex(S('(2+3)/7',evaluate=False))
'\\frac{1}{7} \\left(2 + 3\\right)'
回答3:
(better added as another answer)
Don't forget about the mode options:
>>> e = S('(2+3)/7', evaluate=False)
>>> latex(e,mode='inline')
'$\\left(2 + 3\\right) / 7$'
>>> latex(e,mode='plain')
'\\frac{1}{7} \\left(2 + 3\\right)'
>>> latex(e,mode='equation')
'\\begin{equation}\\frac{1}{7} \\left(2 + 3\\right)\\end{equation}'
>>> latex(e,mode='equation*')
'\\begin{equation*}\\frac{1}{7} \\left(2 + 3\\right)\\end{equation*}'
回答4:
Perhaps the fold_short_frac option is what you are looking for?
>>> latex(S('(2+3)/7',evaluate=False), fold_short_frac=True)
'\\left(2 + 3\\right) / 7'
Also, not sure what you mean about the evaluate=False not working for Mul. Perhaps you can give an example.
>>> S('2*3',evaluate=False)
2*3
>>> S('2*3/7',evaluate=False)
2*3/7
来源:https://stackoverflow.com/questions/28543995/using-sympys-latex-function-without-calculating-the-input