I'm trying to build a comparable functionality as described here: https://castel.dev/post/lecture-notes-1/#sympy-and-mathematica

Basically, he's using something comparable to yasnippet to send a dedicated region to yasnippet and sends it to python, printing it's return value.

I don't even know how to call python and just return it to the buffer in the first place, neither how to let yasnippet do it. Thanks in advance!

  • That demo is quite formidable — thanks for bringing it to our attention!! I suggest you open a "bug-report" on yasnippet asking whether it can/cannot be done in yasnippet. If yes pointers please. If no may I (you) make a feature request. You should of course do some legwork such as how the sympy call would work from elisp at the least -- maybe synchronously, but also possibly async gnu.org/software/emacs/manual/html_node/elisp/…
    – Rusi
    Commented Oct 27, 2020 at 4:23
  • I do see that elisp can be called within backquotes. See joaotavora.github.io/yasnippet/…
    – Rusi
    Commented Oct 27, 2020 at 4:56
  • 1
    A similar work with the linked one is started here: github.com/tecosaur/LaTeX-auto-activating-snippets.
    – Ian
    Commented Oct 27, 2020 at 8:01

1 Answer 1


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:

print("hello world!")

Running eval-region-as-py on this produces the following output.

hello world!

Then you can call this function in a yasnippet template to run it. Something like

`(eval-region-as-py yas-selected-text)`

should work.

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?

4. emaxima

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.

5. Sage

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.

  • The links in the answer seem to be mixed.
    – darcamo
    Commented Nov 7, 2020 at 18:07
  • Regarding the org-mode solution, you can also name a source block and use call_<name>(<arguments>) to get the result of the source block. This allows the result to be in the middle of a sentence. See the documentation for more.
    – darcamo
    Commented Nov 7, 2020 at 18:20
  • Unmixed the links. I didn't know about call_name(arguments), thanks for the info! Another possible org-mode solution could be src_<language>[<header arguments>]{<body>} from another page in the documentation.
    – chipszd
    Commented Nov 9, 2020 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.