I have a calculation in my org-mode buffer:

7700*1/100 + 18000*2/100

I'm looking for the quicker way of completing it, and displaying it like this:

7700*1/100 + 18000*2/100 = 437

My current way of doing this is a little harassing.

  1. do the computation with calc embedded mode: C-x * e
  2. leave the calc embedded mode: q
  3. Copy the result
  4. Revert the change(C-x u), append "=" and paste the result.

What is a quicker way to accomplish the same thing?

2 Answers 2

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

And I have this note in my emacs.org:

NOTE: Edit macro-math.el and fix:
# ;;(delete-region beg end)
# (goto-char (region-end))
# (insert " = " rounded)

And there is an issue to support appending = <result> rather than replacing the equation with a result: https://github.com/nschum/macro-math.el/issues/2


I hastily edited a little function that does this job. The expression to be evaluated must be a region containing an expression valid for the syntax of calc. For example 1 + 2 will be evaluated and replaced by 3 and 1 + 2 => will be evaluated and replaced by 1 + 2 = 3. Note that Calc's precedence rules impose parentheses in some cases, to return 437 the expression 7700 * 1/100 + 18000 * 2/100 = 437 must be written 7700 * (1/100) + 18000 * (2/100). There are undoubtedly still things to be tweaked.

(defun evaluate-in-line (beg end)
  "Evaluates the math expression from the region with calc syntax. 
   The expression can be ended with => or not."
    (interactive (list (region-beginning) (region-end)) )
    (let ((evaluation(calc-eval(substring(buffer-string) (1- beg)(1- end)))))
      (when (stringp evaluation)
        (kill-region beg end)
                   (insert  evaluation)
                  (when (search-forward  "\\evalto" nil t) (replace-match "")
                   (search-forward "\\to" nil t)
                   (replace-match "="))

Evaluate this function with C-x C-e just after the last parentheses, select a region that content an algebraic expression. M-x evaluate-in-line to proceed. If you find it handy, you can save it in your init file and bind this function to some keysstrokes.

  • Thank you for the effort, yet I do not find this function useful as it is. Strangely enough, it seems to delete the multiplication operator (*) on the left part. Also, if I want to keep the details of the computation (which I do), it displays it with => instead of =, which makes no sense: in mathematics, the latter has the meaning of an implication, not of an equality.
    – kotchwane
    Commented Mar 26, 2021 at 22:41
  • Also, while it is true that Calc has one particular precedence rule (as per the documentation states: "Calc gives ‘/’ lower precedence than ‘’, so that ‘a/bc’ is interpreted as ‘a/(b*c)’; this is not standard across all computer languages."), I think it does not give multiplication and division lower precedence than addition. So the parentheses do seem unnecessary in this example.
    – kotchwane
    Commented Mar 26, 2021 at 22:56

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.