I'm trying to write a simple script to do some math, and print out some specific intermediate steps in LaTeX. I've found there's no real simple way to do this using sympy, so I've started manually writing functions to print out each step I want to see.
I need to take a sympy function and format it so that every variable is replaced by it's associated value, I've been accessing the values through a dictionary.
basically,
import sympy as sym
x,a = sym.symbols("x a")
var_Values = {'x':3, 'a':2}
f=(x+1)/a
print(some_Function(f, var_Values))
so that the print statement reads \frac{3+1}{2}
.
I've already tried two methods, using f.subs()
to replace the variable with the value, which prints out 2 in this case, since it evaluates the expression's value.
I've also tried this textual method:
def some_Function(f, var_Values):
expression = sym.latex(f)
for variable in f.free_symbols:
expression = expression.replace(variable.name, var_Values.get(variable.name)
return expression
which is even worse, as it returns \fr2c{3+1}{2}
, which turns more than what I wanted into numbers. It could be possible to get around this by using variable names that don't appear in the LaTeX commands I"m using, but that approach is impossible for me, as I don't choose my variable names.