
from contextlib import contextmanager

from sympy import Eq, Lambda, Function, Indexed

def define(let, be, **kwds):
    return Eq(let, be, **kwds)

@contextmanager
def lift_to_Lambda(eq, return_eq=False, lhs_handler=lambda args: []):
    lhs = eq.lhs
    args = (lhs.args[1:] if isinstance(lhs, Indexed) else 
            lhs.args if isinstance(lhs, Function) else 
            lhs_handler(lhs))
    yield Lambda(args, eq if return_eq else eq.rhs)

from contextlib import contextmanager, redirect_stdout

from sympy import Eq, Lambda, Function, Indexed, latex

def define(let, be, **kwds):
    return Eq(let, be, **kwds)

@contextmanager
def lift_to_Lambda(eq, return_eq=False, lhs_handler=lambda args: []):
    lhs = eq.lhs
    args = (lhs.args[1:] if isinstance(lhs, Indexed) else 
            lhs.args if isinstance(lhs, Function) else 
            lhs_handler(lhs))
    yield Lambda(args, eq if return_eq else eq.rhs)

def save_latex_repr(term, filename):
    with open(filename, 'w') as f:
        with redirect_stdout(f):
            print('.. math::\n\n\t{}'.format(latex(term)))


Add a definition about saving latex representation of a term in a file capturing `print` stdout.
Add a definition about saving latex representation of a term in a file capturing `print` stdout.
