There are a few options I found, none of which completely address this issue.
1. Evaluating Python directly in the buffer
Python can be evaluated in the buffer with the function "eval-region-as-py" from Emacs SE - How to evaluate the selection through Python and replace the selection with it's result?. This returns the result in the buffer.
For example, let's evaluate this:
Running eval-region-as-py on this produces the following output.
Then you can call this function in a yasnippet template to run it. Something like
You can suppress the "None" with another function. It's pretty involved, so I won't get into that here. (Maybe you can start researching it with SO - Redirecting the output of a python function from STDOUT to variable in Python .)
I'm not sure how running sympy would work. When I tried it in my Emacs session, Sympy hangs while looking for definitions for each variable (e.g.,
sin(x) hangs because I didn't run
x = syms('x') first.) using this implementation: Emacs SE - How to evaluate the selection through Python and replace the selection with it's result?.
Normally, you would need to run
syms() to define all the variables, but we don't know which variables we're using before trying to evaluate the expression.
2. Using org-mode instead of LaTeX (not really a way to resolve this problem)
Another option you could try is using org-mode (although this doesn't really answer your original question).
In org-mode, you can call something like
#+begin_src python :results output
import sympy as sp
x = sp.symbols('x')
print(sp.expand_trig(sp.sin(2*x) + sp.cos(2*x)))
which will result in the following.
: 2*sin(x)*cos(x) + 2*cos(x)**2 - 1
3. Emacs Calc Embedded Mode
You type a command directly in your buffer. Suppose you typed in
solve(x^2 + 2*x - 1 = 0, x)
and evaluate it using
C-x * e. This particular expression evaluates to
x = 0.414213562373
I got this idea from Emacs SE - Quickly Evaluate Infix Math Expression?
You can try using emaxima (TeX SE - emaxima on beamer). Again, it won't give you the ability to run SymPy, but it will let you symbolically manipulate expressions and dump the results of that computation directly in the buffer.
You can use
sage-shell-mode to evaluate things in the buffer. It doesn't dump the results directly in the buffer, though; you would need to write a wrapper function to do that. It also may run more slowly than the other options, depending on the Python implementation you're using.