Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 

Repository files navigation

Math at Point

Summary

This package provides the command math-at-point for evaluating an arithmetic expression at point using calc-eval. Similar to quick-calc, the result is displayed in the minibuffer as well as copied into the kill ring.

Installation

This package can be cloned from GitHub via the following command:

git clone https://github.com/shankar2k/math-at-point.git

To start using it, place it somewhere in your Emacs load-path and add the line

(require 'math-at-point)

in your .emacs.d/init.el file. Then run M-x math-at-point to run it. It is useful to create a global keybinding for math-at-point function. I recommend C-z = (assuming that you have first unbound C-z from suspend-emacs).

(global-unset-key (kbd "C-z"))
(global-set-key (kbd "C-z =") 'math-at-point)

If you use use-package, you can configure this as follows:

(setq map-load-path "<path to math-at-point dir>")
(unbind-key "C-z")
(use-package math-at-point
  :load-path map-load-path
  :ensure nil
  :bind ("C-z =" . math-at-point))

Usage

To use this command, simply invoke math-at-point when the point is inside of a math expression or a LaTeX math fragment.

A math expression consists of decimal numbers, the operations +, -, *, /, ^, parentheses, and can be interspersed with whitespace. The whole expression must be fully contained in the current line. If the point is inside a LaTeX math fragment, then the math expression can also contain LaTeX syntax such as \frac{2}{5} or \sqrt{4^3}.

The result is computed using calc-eval and then displayed in the minibuffer and copied into the kill ring (so that it can be pasted with yank). If the point is not within a math expression or LaTeX math fragment, then instead quick-calc is run.

If the command invoked with the optional prefix argument (C-u M-x math-at-point), then the evaluation result is inserted after the expression, prefixed by “=”. If there was already a previous result, it is replaced..

There is also the command math-at-point-simple that can evaluate a simple math expression at point. A simple math expression is similar to a math expression, but does not allow parentheses (and thus can be matched using a regular expression alone). Note that every simple math expression is a math expression, and thus the functionality of math-at-point is a superset of math-at-point-simple

Examples

Math expression

Suppose that you have the following text in your buffer:

The total force is 6.23+(3.789/(5-4)) + 6.4*(2 - (5+<3>) *736.83 ) /2000 newtons.

Here, <3> denotes that the point is on the 3 inside the fourth set of balanced parens. If M-x math-at-point is invoked, then the following message will appear in the minibuffer:

Result 6.23+(3.789/(5-4)) + 6.4*(2 - (5+3) *736.83 ) /2000 => -8.837448

If C-u M-x math-at-point is invoked, then the text is changed to:

The total force is 6.23+(3.789/(5-4)) + 6.4*(2 - (5+[3]) *736.83 ) /2000=-8.837448 newtons.

LaTeX math fragment

Suppose that the following text is in your buffer:

Lets do some cool arithmetic! Let $$x = 3$$, \[y = 4\], and \(z = 5\). Then,

\[\begin{aligned} 
x^{\frac{2}{3}} = <3>^{\frac{2}{3}} = 
\end{aligned}\]

Here, <3> denotes that the point is on the 3 after the first = sign in the \begin{aligned} environment. If M-x math-at-point is invoked, then the following message will appear in the minibuffer:

Result  3^{\frac{2}{3}}  => 2.08008382306

If C-u M-x math-at-point is invoked, then the text is changed to:

Lets do some cool arithmetic! Let $$x = 3$$, \[y = 4\], and \(z = 5\). Then,

\[\begin{aligned}
x ^{\frac{2}{3}} = 3^{\frac{2}{3}} =2.08008382306
\end{aligned}\]

Requirements

math-at-point requires Emacs 27 or later because I use rx-define to define the regular expressions used to match a simple math expression.

About

Evaluate math expression at point using calc-eval

Resources

Stars

17 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages