Skip to content
FrameworkJavaScript

Equation tool: inline LaTeX math

Renders inline math (LaTeX) with KaTeX. Press Cmd/Ctrl+Shift+E to activate it. It wraps the selected text, or a formula typed into the popover input, in a <span data-latex="...">.

The data-latex attribute is the formula. The KaTeX markup is derived from it, is stripped on save, and is regenerated whenever the block renders (load, paste, undo).

Read-only surfaces that never mount an editor, blocksToHtml and <BlokView>, display the source instead. Pass an inlineRenderers entry to render the math there too.

Import

TypeScript
import { Equation } from '@bloklabs/core/tools';

Save Data

TypeScript
// An embed in the block's text field. It holds only the formula, so nothing
// derived from KaTeX's rendering is saved. Plain-text previews and search
// indexes read the formula.
// { "embed": { "equation": { "expression": "E = mc^2" } } }
JSON
// Render the math on a DOM-free view surface by plugging KaTeX in:
//   blocksToHtml(data, {
//     inlineRenderers: {
//       span: ({ attrs }) => attrs['data-latex'] === undefined
//         ? undefined
//         : katex.renderToString(attrs['data-latex'], { throwOnError: false }),
//     },
//   })
{
  "type": "paragraph",
  "data": {
    "text": [{ "text": "Einstein wrote " }, { "embed": { "equation": { "expression": "E = mc^2" } } }]
  }
}

Usage Example

TypeScript
import { Blok } from '@bloklabs/core';
import { Equation, Paragraph } from '@bloklabs/core/tools';

const editor = new Blok({
  holder: 'editor',
  tools: {
    // Paragraph is the default block and is not bundled with the core —
    // without it the editor starts with an "unsupported block" stub.
    paragraph: Paragraph,
    equation: Equation,
  },
});