1
+ # markdown-it-math
2
+
1
3
[ ![ 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 )
2
4
![ Coverage] ( https://runarberg.github.io/markdown-it-math/coverage-badge.svg )
3
5
[ ![ npm] ( https://img.shields.io/npm/v/markdown-it-math.svg )] ( https://www.npmjs.com/package/markdown-it-math )
4
6
[ ![ License] ( https://img.shields.io/npm/l/markdown-it-math )] ( https://github.com/runarberg/markdown-it-math/blob/main/LICENSE )
5
7
[ ![ Downloads] ( https://img.shields.io/npm/dm/markdown-it-math )] ( https://npm-stat.com/charts.html?package=markdown-it-math )
6
8
7
- # markdown-it-math
8
-
9
9
** Note** This library defaults to rendering your equation with an
10
10
AsciiMath dialect. If you want to use LaTeX, follow the instructions
11
11
below.
@@ -184,9 +184,17 @@ default renderer.
184
184
}
185
185
```
186
186
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
+
187
195
## Examples
188
196
189
- Using comma as a decimal mark
197
+ ### Using comma as a decimal mark
190
198
191
199
``` js
192
200
import markdownIt from " markdown-it" ;
@@ -200,7 +208,10 @@ md.render("$40,2$");
200
208
// <p><math><mn>40,2</mn></math></p>
201
209
```
202
210
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.
204
215
205
216
``` js
206
217
import markdownIt from " markdown-it" ;
221
232
// <la-tex display="block">\int_{0}^{\infty} E[X]</la-tex>
222
233
```
223
234
224
- Turning off inline math:
235
+ ### Turning off inline math
225
236
226
237
``` js
227
238
import markdownIt from " markdown-it" ;
242
253
$$
243
254
```
244
255
245
- Using LaTeX style delimiters:
256
+ ### Using LaTeX style delimiters
246
257
247
258
``` js
248
259
import markdownIt from " markdown-it" ;
@@ -258,24 +269,114 @@ Note there are restrictions on what inline delimiters you can use,
258
269
based on optimization for the markdown-it parser [ see here for
259
270
details] [ why-my-inline-rule-is-not-executed ] .
260
271
261
- Block level math must be on its own lines.
272
+ Unlike LaTeX, block level math must be on its own lines.
262
273
274
+ <!-- prettier-ignore -->
263
275
``` markdown
264
276
Some text with inline math \(a^2 + b^2 = c^2\)
265
277
266
278
And block math:
267
- \[e = sum\_ (n=0)^oo 1/n! \]
279
+ \[ e = sum_ (n=0)^oo 1 / n! \]
268
280
269
281
This expression \[P(x \in X) = 0\] will not render.
270
282
```
271
283
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.
279
380
280
381
## Upgrading From v4
281
382
@@ -331,3 +432,15 @@ Version 5 introduced some breaking changes, along with dropping legacy platforms
331
432
blockRenderer: ascii2mathml ({ ... mathRendererOptions, display: " block" }),
332
433
});
333
434
```
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