Skip to content

Commit 66b4f2e

Browse files
committed
Add more examples to README
1 parent 41e52c5 commit 66b4f2e

File tree

1 file changed

+128
-15
lines changed

1 file changed

+128
-15
lines changed

README.md

Lines changed: 128 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
# markdown-it-math
2+
13
[![ci](https://github.com/runarberg/markdown-it-math/actions/workflows/ci.yml/badge.svg)](https://github.com/runarberg/markdown-it-math/actions/workflows/ci.yml)
24
![Coverage](https://runarberg.github.io/markdown-it-math/coverage-badge.svg)
35
[![npm](https://img.shields.io/npm/v/markdown-it-math.svg)](https://www.npmjs.com/package/markdown-it-math)
46
[![License](https://img.shields.io/npm/l/markdown-it-math)](https://github.com/runarberg/markdown-it-math/blob/main/LICENSE)
57
[![Downloads](https://img.shields.io/npm/dm/markdown-it-math)](https://npm-stat.com/charts.html?package=markdown-it-math)
68

7-
# markdown-it-math
8-
99
**Note** This library defaults to rendering your equation with an
1010
AsciiMath dialect. If you want to use LaTeX, follow the instructions
1111
below.
@@ -184,9 +184,17 @@ default renderer.
184184
}
185185
```
186186

187+
## Alternatives
188+
189+
- [@mdit/plugin-katex][@mdit/plugin-katex] -
190+
Renders expressions to KaTeX rather than MathML
191+
- [markdown-it-mathspan][markdown-it-mathspan] and
192+
[markdown-it-mathblock][markdown-it-mathblock] -
193+
Uses commonmark inspired delimiters with customiable renderer.
194+
187195
## Examples
188196

189-
Using comma as a decimal mark
197+
### Using comma as a decimal mark
190198

191199
```js
192200
import markdownIt from "markdown-it";
@@ -200,7 +208,10 @@ md.render("$40,2$");
200208
// <p><math><mn>40,2</mn></math></p>
201209
```
202210

203-
Render to a custom `<la-tex>` element
211+
### Render to a custom `<la-tex>` element
212+
213+
Refer to [temml-custom-element][temml-custom-element] for usage
214+
instructions about the `<la-tex>` custom element.
204215

205216
```js
206217
import markdownIt from "markdown-it";
@@ -221,7 +232,7 @@ $$
221232
// <la-tex display="block">\int_{0}^{\infty} E[X]</la-tex>
222233
```
223234

224-
Turning off inline math:
235+
### Turning off inline math
225236

226237
```js
227238
import markdownIt from "markdown-it";
@@ -242,7 +253,7 @@ a^2
242253
$$
243254
```
244255

245-
Using LaTeX style delimiters:
256+
### Using LaTeX style delimiters
246257

247258
```js
248259
import markdownIt from "markdown-it";
@@ -258,24 +269,114 @@ Note there are restrictions on what inline delimiters you can use,
258269
based on optimization for the markdown-it parser [see here for
259270
details][why-my-inline-rule-is-not-executed].
260271

261-
Block level math must be on its own lines.
272+
Unlike LaTeX, block level math must be on its own lines.
262273

274+
<!-- prettier-ignore -->
263275
```markdown
264276
Some text with inline math \(a^2 + b^2 = c^2\)
265277

266278
And block math:
267-
\[e = sum\_(n=0)^oo 1/n!\]
279+
\[ e = sum_(n=0)^oo 1 / n! \]
268280

269281
This expression \[P(x \in X) = 0\] will not render.
270282
```
271283

272-
[importmap]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap
273-
[jsdelivr]: https://www.jsdelivr.com/
274-
[mathup]: https://mathup.xyz/
275-
[mathml]: https://www.w3.org/TR/MathML/
276-
[markdown-it]: https://github.com/markdown-it/markdown-it
277-
[Temml]: https://temml.org
278-
[why-my-inline-rule-is-not-executed]: https://github.com/markdown-it/markdown-it/blob/master/docs/development.md#why-my-inline-rule-is-not-executed
284+
### Different rendering for different delimiters
285+
286+
```js
287+
import markdownIt from "markdown-it";
288+
import markdownItMath from "markdown-it-math";
289+
import mathup from "mathup";
290+
import temml from "temml";
291+
292+
const md = markdownIt().use(markdownItMath, {
293+
inlineDelimiters: ["$", ["\\(", "\\)"]],
294+
inlineRenderer(src, token) {
295+
if (token.markup === "$") {
296+
return mathup(src).toString();
297+
}
298+
299+
return temml.renderToString(src);
300+
},
301+
302+
blockDelimiters: [$$, ["\\[", "\\]"]],
303+
blockRenderer(src, token) {
304+
if (token.markup === "$$") {
305+
return mathup(src, { display: "block" }).toString();
306+
}
307+
308+
return temml.renderToString(src, { displayMode: true });
309+
},
310+
});
311+
```
312+
313+
Now you can use both `$"AsciiMath"$` and `\(\latex\)` expressions:
314+
315+
<!-- prettier-ignore -->
316+
```md
317+
Some text with inline AsciiMath $a^2 + b^2 = c^2$
318+
and inline LaTeX math \(\sin \theta\)
319+
320+
And AsciiMath:
321+
$$
322+
e = sum_(n=0)^oo 1 / n!
323+
$$
324+
325+
And LaTeX math:
326+
\[
327+
e = \sum_{n=0}^{\infty} \frac{1}{n!}
328+
\]
329+
```
330+
331+
### LaTeX Preample
332+
333+
```js
334+
import markdownIt from "markdown-it";
335+
import markdownItMath from "markdown-it-math";
336+
import temml from "temml";
337+
338+
// An object to keep all the global macros.
339+
const macros = {};
340+
341+
const md = markdownIt().use(markdownItMath, {
342+
inlineRenderer: (src) => temml.renderToString(src, { macros }),
343+
344+
blockDelimiters: ["$$", ["$$ preample", "$$"]],
345+
blockRenderer(src, token) {
346+
if (token.markup === "$$ preample") {
347+
// Add these defs to the global macros.
348+
Object.assign(macros, temml.definePreamble(src));
349+
350+
// Don’t render anything.
351+
return "";
352+
}
353+
354+
return temml.renderToString(src, { displayMode: true, macros });
355+
},
356+
});
357+
```
358+
359+
<!-- prettier-ignore -->
360+
```md
361+
# The Expected value
362+
363+
$$ preample
364+
\def\E{\mathbb{E}}
365+
\newcommand\d[0]{\operatorname{d}\!}
366+
$$
367+
368+
Now we can use the macros defined above.
369+
370+
$$
371+
\E[X] = \int_{-\infty}^{\infty} xf(x) \d{x}
372+
$$
373+
```
374+
375+
Note that this plugin does not support [info
376+
strings](https://spec.commonmark.org/0.31.2/#info-string) but the open
377+
delimiter can be customized to look like an info string (see
378+
below). Consider [markdown-it-mathblock][markdown-it-mathblock] if you
379+
need commonmark compliant info strings.
279380

280381
## Upgrading From v4
281382

@@ -331,3 +432,15 @@ Version 5 introduced some breaking changes, along with dropping legacy platforms
331432
blockRenderer: ascii2mathml({ ...mathRendererOptions, display: "block" }),
332433
});
333434
```
435+
436+
[@mdit/plugin-katex]: https://mdit-plugins.github.io/katex.html
437+
[importmap]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap
438+
[jsdelivr]: https://www.jsdelivr.com/
439+
[markdown-it]: https://github.com/markdown-it/markdown-it
440+
[markdown-it-mathblock]: https://github.com/runarberg/markdown-it-mathblock
441+
[markdown-it-mathspan]: https://github.com/runarberg/markdown-it-mathspan
442+
[mathml]: https://www.w3.org/TR/MathML/
443+
[mathup]: https://mathup.xyz/
444+
[Temml]: https://temml.org
445+
[temml-custom-element]: https://github.com/runarberg/temml-custom-element
446+
[why-my-inline-rule-is-not-executed]: https://github.com/markdown-it/markdown-it/blob/master/docs/development.md#why-my-inline-rule-is-not-executed

0 commit comments

Comments
 (0)