7

Is there a way to highlight and quickly evaluate a simple math expression without using elisp notation? For example, we can already evaluate the elisp expression (+ 1 1) in the following text snippet by putting point behind it and pressing C-x C-e.

Here is a math equation: (+ 1 1)

But can we also do

Here is a math equation: 1 + 1

And get its result?

2 Answers 2

8

You can use the built-in calc package to do so.

  1. Mark your expression, e.g. 3 * (1 + 1)
  2. Call calc-grab-region
    • Call calc-dispatch (default C-x *), then hit g
3
  • 1
    You can also use C-u C-x * e while the expression is marked, and the marked expression will be replaced by the result. Afterwards, use C-x * x to exit calc.
    – Dodgie
    Jan 30, 2018 at 22:44
  • I just tried this, and did not need to perform the second step calc-dispatch g. Feb 9, 2018 at 15:21
  • I meant calc-dispatch, then g performs calc-grab-region. Feb 9, 2018 at 18:14
3

You might also use calc's embedded mode if you intent to replace an expression with its result in the current buffer. C-u C-x * e when your formula / expression is on it's own on a separate line.

Otherwise mark it and type C-0 C-x * e

(Update: thanks to Dodgie, forgot to mention that) When you are finished with your mathematical manipulations type C-x * e again to end embedded calc mode.

Your Answer

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

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