0

When I need while editing a .org file to calculate, say, 23.45 * 15, I copy (M-w) that string then call M-x calc.

I paste the numbers as a string, preceded by a single tick (using algebraic mode so I don't have to reenter the numbers or the operation).

Once the answer comes back (351.75), I mark+copy the output, return (two instances of C-xo) and paste (C-y) the result.

It almost works. I get 1: 351.75 rather than 351.75, but I'm sure there is a superior way that doesn't involve quite so many keystrokes. Can you suggest one?

P.S.1

I am familiar with the basic spreadsheet in org-mode, but here I'm wondering if there is something I'm missing for much simpler needs.

P.S.2

The same problem arises with .txt file, but a solution using what's available in .org is quite adequate.

Related

1
  • One way is the make the expression you want to calculate into a calc src block and evaluate it. Mark the expression, do C-c C-, s to add the src block delimiters, type calc to make it into a calc src block and C-c C-c to evaluate it. You still have to do a bunch of keystrokes for that and then another bunch to clean things up though, so I'm not sure whether you are saving anything. You also have to enable calc as an Org babel language in your init file (it's not enabled by default), but that's a one-time thing. And you might define a keyboard macro/function to do the setup/cleanup.
    – NickD
    Jun 8, 2023 at 20:07

3 Answers 3

1

For one–off calculations, use C-u M-x quick-calc. It prompts for an algebraic expression and then inserts the result into the buffer at point.

2
(use-package macro-math
  :bind ("\C-x=" . macro-math-eval-region)
        ("\C-x~" . macro-math-eval-and-round-region))

This works in any Emacs buffer - Org, text, email, etc. You can modify it to replace the equation rather than append = <ans> if you prefer.

http://nschum.de/src/emacs/macro-math/

0

You already have perhaps org-babel-do-load-languages for, say, python. Insert a line for calc.

(org-babel-do-load-languages
 'org-babel-load-languages
 '((python . t)
   (calc . t)))

Now write

#+begin_src calc
23.45 * 15
#+end_src

and type C-cC-c on the expression. A solution block will be inserted.

#+begin_src calc
23.45 * 15
#+end_src

#+RESULTS:
: 351.75

If you prefer, org-mode can generate the block for you if you mark 23.45 * 15, and type C-c C-, s, followed by calc.

Credit for this answer: NickD

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.