|
| 1 | +--- |
| 2 | +title: Math & Equations |
| 3 | +description: LaTeX math blocks and inline math for BlockNote — rendered as formulas in the editor, and exportable to Markdown, PDF, DOCX, ODT, and email. |
| 4 | +--- |
| 5 | + |
| 6 | +# Math & Equations |
| 7 | + |
| 8 | +The `@blocknote/math-block` package adds mathematical notation to your documents: a **math block** for standalone equations and **inline math** that flows with the surrounding text. Both are authored as LaTeX in a source popup and rendered as formulas — [KaTeX](https://katex.org/) converts the LaTeX to MathML, which browsers display natively. |
| 9 | + |
| 10 | +<Callout type="info"> |
| 11 | + This block is only available in React (`@blocknote/react`). |
| 12 | +</Callout> |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install @blocknote/math-block |
| 16 | +``` |
| 17 | + |
| 18 | +## Adding to your editor |
| 19 | + |
| 20 | +The package exports `createReactMathBlockSpec` (the block) and `createReactInlineMathSpec` (the inline content). Add them to your schema's `blockSpecs` and `inlineContentSpecs` respectively: |
| 21 | + |
| 22 | +```tsx |
| 23 | +import { BlockNoteSchema } from "@blocknote/core"; |
| 24 | +import { |
| 25 | + createReactMathBlockSpec, |
| 26 | + createReactInlineMathSpec, |
| 27 | +} from "@blocknote/math-block"; |
| 28 | + |
| 29 | +const schema = BlockNoteSchema.create().extend({ |
| 30 | + blockSpecs: { |
| 31 | + // Adds the Math block to the schema. |
| 32 | + mathBlock: createReactMathBlockSpec(), |
| 33 | + }, |
| 34 | + inlineContentSpecs: { |
| 35 | + // Adds the inline Math content to the schema. |
| 36 | + math: createReactInlineMathSpec(), |
| 37 | + }, |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +To highlight the LaTeX source in the popup, add the [syntax highlighting](/docs/features/blocks/code-blocks#syntax-highlighting) extension to your editor. The math block and inline math already declare their source language (`latex`), so no per-block configuration is needed: |
| 42 | + |
| 43 | +```tsx |
| 44 | +import { syntaxHighlighter } from "@blocknote/code-block"; |
| 45 | + |
| 46 | +const editor = useCreateBlockNote({ |
| 47 | + schema, |
| 48 | + extensions: [syntaxHighlighter], |
| 49 | +}); |
| 50 | +``` |
| 51 | + |
| 52 | +## Menu items & localization |
| 53 | + |
| 54 | +Because the math specs live in an optional package, their editor integrations are opt-in too — the package exports everything needed: |
| 55 | + |
| 56 | +```tsx |
| 57 | +import { |
| 58 | + getMathSlashMenuItems, // Slash Menu items for inserting math |
| 59 | + getMathBlockTypeSelectItems, // Block Type Select item for the Formatting Toolbar |
| 60 | + locales as mathLocales, // dictionary strings, merged under the `math` key |
| 61 | +} from "@blocknote/math-block"; |
| 62 | +``` |
| 63 | + |
| 64 | +- `getMathSlashMenuItems(editor)` returns [Slash Menu](/docs/react/components/suggestion-menus#slash-menu) items for inserting a math block or inline math — combine them with the default items via `combineByGroup`. |
| 65 | +- `getMathBlockTypeSelectItems(editor)` returns a [Block Type Select](/docs/react/components/formatting-toolbar) item for turning a block into a math block, to spread alongside the defaults. |
| 66 | +- `mathLocales` translates the math strings — merge a locale into the editor's `dictionary` under the `math` key (see [Localization](/docs/features/localization)); without one, the bundled English strings are used. |
| 67 | + |
| 68 | +The example below wires them all up. |
| 69 | + |
| 70 | +## Example |
| 71 | + |
| 72 | +<Example name="custom-schema/math-block" /> |
| 73 | + |
| 74 | +## Exporting |
| 75 | + |
| 76 | +Math exports to every format BlockNote supports: |
| 77 | + |
| 78 | +- **HTML** works out of the box — the export produces a native MathML `<math>` element (with the LaTeX embedded for lossless round-trips), and pasting MathML back in converts to LaTeX. |
| 79 | +- **[Markdown](/docs/features/export/markdown)** also works out of the box — math blocks export as `$$` blocks and inline math as `$...$` spans, their common Markdown notations. |
| 80 | +- **PDF, DOCX, ODT, and email** use exporter mappings, which live as subpaths of this package — spread them into the default mappings of the exporter you use, as shown per format below. Invalid LaTeX renders an error placeholder identifying the offending source, mirroring the editor. |
| 81 | + |
| 82 | +### DOCX |
| 83 | + |
| 84 | +With the [DOCX exporter](/docs/features/export/docx), math exports as native (editable) Word equations. Works server-side out of the box — the LaTeX is converted to OMML without rendering: |
| 85 | + |
| 86 | +```typescript |
| 87 | +import { |
| 88 | + DOCXExporter, |
| 89 | + docxDefaultSchemaMappings, |
| 90 | +} from "@blocknote/xl-docx-exporter"; |
| 91 | +import { |
| 92 | + inlineMathMapping, |
| 93 | + mathBlockMapping, |
| 94 | +} from "@blocknote/math-block/docx-exporter"; |
| 95 | + |
| 96 | +const exporter = new DOCXExporter(editor.schema, { |
| 97 | + ...docxDefaultSchemaMappings, |
| 98 | + blockMapping: { |
| 99 | + ...docxDefaultSchemaMappings.blockMapping, |
| 100 | + mathBlock: mathBlockMapping, |
| 101 | + }, |
| 102 | + inlineContentMapping: { |
| 103 | + ...docxDefaultSchemaMappings.inlineContentMapping, |
| 104 | + math: inlineMathMapping, |
| 105 | + }, |
| 106 | +}); |
| 107 | +``` |
| 108 | + |
| 109 | +### ODT |
| 110 | + |
| 111 | +With the [ODT exporter](/docs/features/export/odt), math exports as native (editable) formula objects. Also works server-side out of the box (LaTeX is converted to MathML without rendering): |
| 112 | + |
| 113 | +```typescript |
| 114 | +import { |
| 115 | + inlineMathMapping, |
| 116 | + mathBlockMapping, |
| 117 | +} from "@blocknote/math-block/odt-exporter"; |
| 118 | + |
| 119 | +// Spread into the ODTExporter's mappings exactly as for DOCX above. |
| 120 | +``` |
| 121 | + |
| 122 | +### PDF |
| 123 | + |
| 124 | +With the [PDF exporter](/docs/features/export/pdf), math blocks export as vector formulas — no rasterization, so they also work server-side out of the box. Inline math is rasterized to images that flow with the text: |
| 125 | + |
| 126 | +```typescript |
| 127 | +import { |
| 128 | + createInlineMathMapping, |
| 129 | + mathBlockMapping, |
| 130 | +} from "@blocknote/math-block/pdf-exporter"; |
| 131 | + |
| 132 | +// Spread into the PDFExporter's mappings as for DOCX above - note that |
| 133 | +// inline math is a factory here: `math: createInlineMathMapping()`. |
| 134 | +``` |
| 135 | + |
| 136 | +The inline math factory takes one option: |
| 137 | + |
| 138 | +```typescript |
| 139 | +createInlineMathMapping(options?: { |
| 140 | + /** |
| 141 | + * Rasterizes the formula SVG to an image. Defaults to the built-in |
| 142 | + * canvas rasterizer, which only works in the browser - when exporting |
| 143 | + * server-side, pass one backed by e.g. `@resvg/resvg-js` or `sharp`; |
| 144 | + * without it, a server-side export throws. The `RasterizeSVG` type is |
| 145 | + * exported from the same subpath. |
| 146 | + */ |
| 147 | + rasterize?: RasterizeSVG; |
| 148 | +}); |
| 149 | +``` |
| 150 | + |
| 151 | +Math blocks require the `@react-pdf/math` package (a peer dependency of the PDF mapping). |
| 152 | + |
| 153 | +### Email |
| 154 | + |
| 155 | +With the [email exporter](/docs/features/export/email), math exports as images with the LaTeX source as the alt text: math blocks are rasterized to PNG in the browser (and embedded as SVG elsewhere), inline math is always embedded as SVG: |
| 156 | + |
| 157 | +```typescript |
| 158 | +import { |
| 159 | + createInlineMathMapping, |
| 160 | + createMathBlockMapping, |
| 161 | +} from "@blocknote/math-block/email-exporter"; |
| 162 | + |
| 163 | +// Spread into the ReactEmailExporter's mappings as for DOCX above - both |
| 164 | +// are factories here: `mathBlock: createMathBlockMapping()` and |
| 165 | +// `math: createInlineMathMapping()`. |
| 166 | +``` |
| 167 | + |
| 168 | +Both factories take delivery-related options: |
| 169 | + |
| 170 | +```typescript |
| 171 | +createMathBlockMapping(options?: { |
| 172 | + /** |
| 173 | + * Rasterizes the formula SVG to a raster image. Defaults to the built-in |
| 174 | + * canvas rasterizer in the browser; elsewhere (e.g. server-side email |
| 175 | + * rendering at send time), the formula is embedded as an SVG instead - |
| 176 | + * pass a rasterizer (e.g. backed by `@resvg/resvg-js` or `sharp`) to get |
| 177 | + * PNGs there, which more email clients display. |
| 178 | + */ |
| 179 | + rasterize?: RasterizeSVG; |
| 180 | + /** |
| 181 | + * How generated images get into the email: embedded as data URLs by |
| 182 | + * default, or as inline `cid:` attachments - see the email exporter's |
| 183 | + * image delivery docs. |
| 184 | + */ |
| 185 | + imageDelivery?: ReactEmailImageDelivery; |
| 186 | +}); |
| 187 | + |
| 188 | +// `createInlineMathMapping` takes the same `imageDelivery` option (inline |
| 189 | +// math is always SVG, so there's no `rasterize`). |
| 190 | +``` |
| 191 | + |
| 192 | +Some email clients don't display data URL images — see [image delivery](/docs/features/export/email#math--diagram-blocks) on the email page for delivering the generated images as inline `cid:` attachments instead. |
0 commit comments