# remark-math
[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]
**[remark][]** plugin to support math (`$C_L$`).
## Contents
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`unified().use(remarkMath[, options])`](#unifieduseremarkmath-options)
* [`Options`](#options)
* [Authoring](#authoring)
* [HTML](#html)
* [CSS](#css)
* [Syntax](#syntax)
* [Syntax tree](#syntax-tree)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Related](#related)
* [Contribute](#contribute)
* [License](#license)
## What is this?
This package is a [unified][] ([remark][]) plugin to add support for math
syntax.
You can use this to add support for parsing and serializing this syntax
extension.
As there is no spec for math in markdown, this extension follows how code
(fenced and text) works in Commonmark, but uses dollars (`$`).
## When should I use this?
This project is useful when you want to support math in markdown.
Extending markdown with a syntax extension makes the markdown less portable.
LaTeX equations are also quite hard.
But this mechanism works well when you want authors, that have some LaTeX
experience, to be able to embed rich diagrams of math in scientific text.
If you *just* want to turn markdown into HTML (with maybe a few extensions such
as math), we recommend [`micromark`][micromark] with
[`micromark-extension-math`][micromark-extension-math] instead.
If you don’t use plugins and want to access the syntax tree, you can use
[`mdast-util-from-markdown`][mdast-util-from-markdown] with
[`mdast-util-math`][mdast-util-math].
This plugins adds [fields on nodes][mdast-util-to-hast-fields] so that the
plugin responsible for turning markdown (mdast) into HTML (hast),
[`remark-rehype`][remark-rehype], will turn text math (inline) into
`…
` and flow math (block)
into `
…
`.
## Install
This package is [ESM only][esm].
In Node.js (version 16+), install with [npm][]:
```sh
npm install remark-math
```
In Deno with [`esm.sh`][esmsh]:
```js
import remarkMath from 'https://esm.sh/remark-math@6'
```
In browsers with [`esm.sh`][esmsh]:
```html
```
## Use
Say our document `example.md` contains:
```markdown
Lift($$L$$) can be determined by Lift Coefficient ($$C_L$$) like the following
equation.
$$
L = \frac{1}{2} \rho v^2 S C_L
$$
```
…and our module `example.js` contains:
```js
import rehypeKatex from 'rehype-katex'
import rehypeStringify from 'rehype-stringify'
import remarkMath from 'remark-math'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {read} from 'to-vfile'
import {unified} from 'unified'
const file = await unified()
.use(remarkParse)
.use(remarkMath)
.use(remarkRehype)
.use(rehypeKatex)
.use(rehypeStringify)
.process(await read('example.md'))
console.log(String(file))
```
…then running `node example.js` yields:
```html
Lift(…
) like the following
equation.
…
```
## API
This package exports no identifiers.
The default export is [`remarkMath`][api-remark-math].
### `unified().use(remarkMath[, options])`
Add support for math.
###### Parameters
* `options` ([`Options`][api-options], optional)
— configuration
###### Returns
Nothing (`undefined`).
### `Options`
Configuration (TypeScript type).
###### Fields
* `singleDollarTextMath` (`boolean`, default: `true`)
— whether to support text math (inline) with a single dollar.
Single dollars work in Pandoc and many other places, but often interfere
with “normal” dollars in text.
If you turn this off, you can still use two or more dollars for text math.
## Authoring
When authoring markdown with math, keep in mind that math doesn’t work in most
places.
Notably, GitHub currently has a really weird crappy client-side regex-based
thing.
But on your own (math-heavy?) site it can be great!
Instead of a syntax extension to markdown, you can also use fenced code with an
info string of `math`:
````markdown
```math
L = \frac{1}{2} \rho v^2 S C_L
```
````
## HTML
This plugin integrates with [`remark-rehype`][remark-rehype].
When markdown (mdast) is turned into HTML (hast) the math nodes are turned
into `…
` and
`…
` elements.
## CSS
This package does not relate to CSS.
You can choose to render the math with KaTeX, MathJax, or something else, which
might need CSS.
## Syntax
See [*Syntax* in
`micromark-extension-math`](https://github.com/micromark/micromark-extension-math#syntax).
## Syntax tree
See [*Syntax tree* in
`mdast-util-math`](https://github.com/syntax-tree/mdast-util-math#syntax-tree).
## Types
This package is fully typed with [TypeScript][].
It exports the additional type [`Options`][api-options].
If you’re working with the syntax tree, you can register the new node types
with `@types/mdast` by adding a reference:
```js
// Register math nodes in mdast:
///