# LaTeX Formula

- Human documentation: [https://docs.univer.ai/ja-JP/guides/docs/features/latex](https://docs.univer.ai/ja-JP/guides/docs/features/latex)

- Agent Markdown: [https://docs.univer.ai/ja-JP/guides/docs/features/latex.md](https://docs.univer.ai/ja-JP/guides/docs/features/latex.md)

- Requested language: `ja-JP`

- Content language: `ja-JP`

- Documentation version: `1.2.0`

- Source: [docs/features/latex.ja-JP.mdx](https://github.com/dream-num/documentation/blob/dev/content/guides/docs/features/latex.ja-JP.mdx)

---

#### Package metadata

```json
{
  "mobile": true,
  "plugins": [
    {
      "client": "@univerjs-pro/docs-latex",
      "facade": "@univerjs-pro/docs-latex/facade"
    },
    {
      "client": "@univerjs-pro/docs-latex-ui",
      "locale": "@univerjs-pro/docs-latex-ui/locale/ja-JP",
      "style": "@univerjs-pro/docs-latex-ui/lib/index.css"
    }
  ],
  "license": true,
  "server": false
}
```

LaTeX formula 機能は、編集可能な formula source を Docs model に保存し、browser editor で描画します。

## プラグインモード

### インストール

#### npm

```bash
npm install @univerjs-pro/docs-latex @univerjs-pro/docs-latex-ui
```

#### pnpm

```bash
pnpm add @univerjs-pro/docs-latex @univerjs-pro/docs-latex-ui
```

#### yarn

```bash
yarn add @univerjs-pro/docs-latex @univerjs-pro/docs-latex-ui
```

#### bun

```bash
bun add @univerjs-pro/docs-latex @univerjs-pro/docs-latex-ui
```

### 使い方

```typescript
import { UniverDocsLatexPlugin } from '@univerjs-pro/docs-latex' // [!code ++]
import { UniverDocsLatexUIPlugin } from '@univerjs-pro/docs-latex-ui' // [!code ++]
import DocsLatexUIJaJP from '@univerjs-pro/docs-latex-ui/locale/ja-JP' // [!code ++]
import { LocaleType, mergeLocales, Univer } from '@univerjs/core'

import '@univerjs-pro/docs-latex-ui/lib/index.css' // [!code ++]
import '@univerjs-pro/docs-latex/facade' // [!code ++]

const univer = new Univer({
  locale: LocaleType.JA_JP,
  locales: {
    [LocaleType.JA_JP]: mergeLocales(
      DocsLatexUIJaJP, // [!code ++]
    ),
  },
})

univer.registerPlugin(UniverDocsLatexPlugin) // [!code ++]
univer.registerPlugin(UniverDocsLatexUIPlugin) // [!code ++]
```

Univer の商用ライセンスを利用している場合は、[クライアントで License を使用する](https://docs.univer.ai/ja-JP/server/license.md#in-plugin-mode) を参照してください。

## モバイル対応

まず[モバイルエディター](https://docs.univer.ai/ja-JP/guides/docs/getting-started/installation.md#mobile-support)を設定します。このページのプラグインモードの初期化コードで、以下のデスクトップ登録を同じパッケージのモバイル版に置き換え、**文書を開く前に**完了してください。モデル、スタイル、言語ファイル、Facade は維持し、両方を同時に登録しないでください。

| パッケージ                         | デスクトップの登録                 | モバイルの登録                         |
| ----------------------------- | ------------------------- | ------------------------------- |
| `@univerjs-pro/docs-latex-ui` | `UniverDocsLatexUIPlugin` | `UniverDocsLatexMobileUIPlugin` |

## Facade API

LaTeX API を呼び出す前に facade entry を import します。

```typescript
import '@univerjs-pro/docs-latex/facade'
```

現在の document に formula を挿入します。

```typescript
const doc = univerAPI.getActiveDocument()

const formula = doc?.insertLatexFormula({
  latex: 'E=mc^2',
  index: 12,
})
```

Formula を読み取り、更新します。

```typescript
const formulas = doc?.getLatexFormulas() ?? []
const firstFormula = doc?.findLatexFormulaByText('mc')

firstFormula?.update({
  latex: '\\frac{a}{b}',
})
```

range id または document data-stream offset でも検索できます。

```typescript
const byId = doc?.getLatexFormula('formula-range-id')
const atOffset = doc?.getLatexFormulaAt(42)

console.log(byId?.describe(), atOffset?.getLatex())
```
