6

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?

6

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
  • 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 '18 at 22:44
  • I just tried this, and did not need to perform the second step calc-dispatch g. – user1404316 Feb 9 '18 at 15:21
  • I meant calc-dispatch, then g performs calc-grab-region. – Tianxiang Xiong Feb 9 '18 at 18:14
2

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, privacy policy and cookie policy

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