LaTeX Formula
Docs stores the LaTeX formula source as editable content in the document model and renders it in the browser editor.
Plugin mode
Installation
pnpm add @univerjs-pro/docs-latex @univerjs-pro/docs-latex-uinpm install @univerjs-pro/docs-latex @univerjs-pro/docs-latex-uiyarn add @univerjs-pro/docs-latex @univerjs-pro/docs-latex-uibun add @univerjs-pro/docs-latex @univerjs-pro/docs-latex-uiUsage
import { UniverDocsLatexPlugin } from '@univerjs-pro/docs-latex'import { UniverDocsLatexUIPlugin } from '@univerjs-pro/docs-latex-ui'import DocsLatexUIEnUS from '@univerjs-pro/docs-latex-ui/locale/en-US'import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import '@univerjs-pro/docs-latex-ui/lib/index.css'import '@univerjs-pro/docs-latex/facade'const univer = new Univer({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( DocsLatexUIEnUS, ), },})univer.registerPlugin(UniverDocsLatexPlugin)univer.registerPlugin(UniverDocsLatexUIPlugin)If you hold a commercial license for Univer, refer to Using License in Client for the configuration.
Mobile support
Set up the mobile editor first. In the plugin-mode setup described on this page, replace the desktop registrations below with the mobile exports from the same packages, and complete the replacement before opening a document. Keep the model plugins, styles, locales, and Facade imports unchanged, and do not register both variants.
| Package | Desktop registration | Mobile registration |
|---|---|---|
@univerjs-pro/docs-latex-ui | UniverDocsLatexUIPlugin | UniverDocsLatexMobileUIPlugin |
Facade API
Import the facade entry before calling any LaTeX API:
import '@univerjs-pro/docs-latex/facade'Insert a formula into the active document:
const doc = univerAPI.getActiveDocument()const formula = doc?.insertLatexFormula({ latex: 'E=mc^2', index: 12,})Read and update formulas:
const formulas = doc?.getLatexFormulas() ?? []const firstFormula = doc?.findLatexFormulaByText('mc')firstFormula?.update({ latex: '\\frac{a}{b}',})A formula can also be retrieved by range id or by its offset in the document's data stream:
const byId = doc?.getLatexFormula('formula-range-id')const atOffset = doc?.getLatexFormulaAt(42)console.log(byId?.describe(), atOffset?.getLatex())How is this guide?