`math-preview` uses [MathJax](https://www.mathjax.org/) for displaying [TeX](https://tug.org/), [MathML](https://www.w3.org/Math/) and [AsciiMath](http://asciimath.org/) math inline in Emacs buffers.

## Installation
*NOTE: it's recommended to use the latest Emacs version with this package. See [this issue](https://gitlab.com/matsievskiysv/math-preview/-/issues/1).*
*NOTE: In case of Schema errors update elisp library and nodejs program to the latest version.*
[`math-preview`](./math-preview.el) requires external nodejs program [`math-preview`](./math-preview.js).
It may be installed by issuing the command:
```bash
> npm install -g git+https://gitlab.com/matsievskiysv/math-preview
```
If you don't have `npm` installed, get it from [`asdf`](https://github.com/asdf-vm/asdf-nodejs) or [`nvm`](https://github.com/nvm-sh/nvm).
Make sure that `math-preview` is in you `PATH`.
Install companion package in Emacs:
M-x+package-install+math-preview
If `math-preview` is not in your path, then you need to set variable `math-preview-command` to the location of the program: M-x+customize-variable+math-preview-command.
Or if you use `use-package`, just add the following command:
```elisp
(use-package math-preview
:custom (math-preview-command "/path/to/math-preview"))
```
## Functions
| | |
|:--|:--|
| `math-preview-all` | Preview equations in buffer |
| `math-preview-region` | Preview equations in selected region |
| `math-preview-at-point` | Preview equation at current position |
| `math-preview-clear-all` | Clear equation images in buffer |
| `math-preview-clear-region` | Clear equation images in selected region |
| `math-preview-clear-at-point` | Clear equation image at current position |
| `math-preview-increment-scale` | Enlarge equation image at point |
| `math-preview-decrement-scale` | Shrink equation image at point |
| `math-preview-copy-svg` | Copy SVG code to clipboard |
| `math-preview-start-process` | Start child process (Not required. However, calling this function early would reduce first render time) |
| `math-preview-stop-process` | Stop child process |
| `math-preview-math-preview-reset-numbering` | Reset automatic equation numbering |
## Key bindings
`math-preview` does not add any keybindings to global keymap. However, it adds a number of keybindings to the image overlay, which become active when your cursor is on the image.
| | |
|:--|:--|
| `math-preview-clear-at-point` | Del; Backspace; Space; Enter; mouse-1 |
| `math-preview-clear-all` | Ctrl+Del; Ctrl+Backspace; Ctrl+mouse-1 |
| `math-preview-increment-scale` | +; p |
| `math-preview-decrement-scale` | -; n |
| `math-preview-copy-svg` | Ctrl+Backspace; Ctrl+Space |
## Equation preprocessing
It might be useful to preprocess equation strings before passing them to MathJax. For this you may use `math-preview-preprocess-functions`, `math-preview-tex-preprocess-functions`, `math-preview-mathml-preprocess-functions` and `math-preview-asciimath-preprocess-functions` customization options. Each equation would be modified by functions in these lists, chained from left to right.
`math-preview-preprocess-functions` are applied to all equations after type specific functions `math-preview-preprocess-functions`, `math-preview-tex-preprocess-functions`, `math-preview-mathml-preprocess-functions` and `math-preview-asciimath-preprocess-functions`. In Emacs terminology, these variables are [abnormal hooks](https://www.gnu.org/software/emacs/manual/html_node/elisp/Hooks.html). Each of them takes one argument that is a hash table with fields:
- `match`: matched string including marks;
- `string`: matched string without marks;
- `type`: equation type (`tex`, `mathml` or `asciimath`);
- `inline`: equation inline flag;
- `priority`: priority value;
- `lmark` and `rmark`: left and right marks respectively;
- `marks`: cons pair of `lmark` and `rmark`;
- `lregexp` and `rregexp`: left and right regexp flags respectively;
- `regexp`: cons pair of `lregexp` and `lregexp`;
- `prefix` and `suffix`: left and right matched marks. Equal to `lmark` and `rmark` is regexp is not used.
You may modify `string` field in place to influence further equation processing.
For example, you might want to replace some variable with another in your equations:
```elisp
(lambda (x)
(puthash 'string
(s-replace "\\phi" "\\varphi"
(gethash 'string x))
x))
```
Another practical example of equation preprocessing is a standard MathML hook
```elisp
(lambda (x) (puthash 'string (gethash 'match x) x))
```
which replaces stripped equation `string` with the unstripped version `match` in order to preserve `` tags.
## SVG postprocessing
Similarly, SVG string may be edited before rendering. `math-preview-svg-postprocess-functions` abnormal hook functions take one argument that is a hash table with single field `string`. You may modify `string` field in place to influence image rendering.
## Configuration
Configuration of the package is done using [`customize`](https://www.gnu.org/software/emacs/manual/html_node/emacs/Easy-Customization.html) framework. To open customization menu type M-x+customize-group+math-preview.
Note, that most of the configuration options are passed to companion java-script program at startup, so in order to apply new configurations you need to stop currently running program via `math-preview-stop-process` command.
### Debug configuration
Sometimes it's necessary to debug configuration. In order to enable debug buffer, issue the command M-:+(setq math-preview--debug-json t). This will create a `*math-preview*` buffer, where JSON communication between programs will be echoed. Additionally, at startup of java-script program full MathJax configuration will appear in this buffer.
### Set equation marks
Package has 6 variables for equation marks configuration: for TeX, MathML and AsciiMath, each having inline and display versions. Currently only TeX equations distinguish between inline and display modes.
Configuration menus:
* TeX: M-x+customize-group+math-preview-tex
* MathML: M-x+customize-group+math-preview-mathml
* AsciiMath: M-x+customize-group+math-preview-asciimath
### Set preprocess and postprocess functions
These abnormal hooks are described in detail above.
Configuration menus:
* Common: M-x+customize-group+math-preview
* SVG: M-x+customize-group+math-preview
* TeX: M-x+customize-group+math-preview-tex
* MathML: M-x+customize-group+math-preview-mathml
* AsciiMath: M-x+customize-group+math-preview-asciimath
### Predefine TeX macros and environments
This package allows adding custom macros and environments globally. This is done in configuration menu M-x+customize-group+math-preview-tex. Following are predefined examples of macro and environment:
$$\ddx[y]{x}$$
$$\begin{braced}A\end{braced}$$
### Preload packages
MathJax allows preload frequently used packages. This is done by adding package to the list in menu M-x+customize-group+math-preview-tex-packages. Under that menu there are sub-menus with configuration options for some of the packages.
Note that some packages require loader to be added. This is done by adding loader to the list in menu M-x+customize-group+math-preview-mathjax-loader.
### Autoload packages
It is possible to automatically load packages when certain macros are invoked. For this you should add `autoload` package to default packages list in M-x+customize-group+math-preview-tex-packages (added by default) and then assign macro and environment names to packages in M-x+customize-group+math-preview-tex-packages-autoload.
### Equation labels
MathJax keeps track of equation labels. Due to this, second conversion of the equation will produce an error `