# Mathematics

> Insert inline or standalone LaTeX formulas.

`mathematics()` adds inline and standalone formula nodes. Both store the original LaTeX in a `latex` attribute so the host can render it or let users edit the source.

## Try it

[Try the mathematics editor example](https://gpdoc.dev/docs/plugins/media/mathematics/#mathematics-example)

## Install

```sh
cargo add gpdoc-plugin-mathematics
```

## Example

Register the typesetting view for inline and block formulas in the [Quickstart editor configuration](/docs/quickstart/#configure-the-editor).

```rust
use gpdoc::{EditorConfig, PluginViewExt, ViewPlugin};
use gpdoc_plugin_mathematics::{mathematics, formula_content};
use gpdoc_plugin_paragraph::paragraph;


let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(mathematics().with_view(formula_view))
    .build()
    .expect("configure editor");


fn formula_view() -> ViewPlugin {
    formula_content()
}
```

Type `$$x^2$$` inside a paragraph. The closing delimiter replaces the typed source with an inline formula.

## Compose a formula control

`mathematics()` typesets both inline and block formulas. Use `formula_controls` to place a GPUI element over each formula while the editor continues to draw the math underneath.

### Anatomy

```text
formula_controls(...)     Supplies a FormulaNode for each inline or block formula
└── div().size_full()     Your control covers the measured formula
    └── on_click(...)     Your action uses the source from formula.latex()
```

### Copy a formula’s LaTeX on click

In the example above, replace the `formula_content` import with `formula_controls`. Return both views from `formula_view` so inline and block formulas support the same action.

```rust
use gpui::{ClipboardItem, div, prelude::*};


fn formula_view() -> [ViewPlugin; 2] {
    formula_controls(|formula, _, _| {
        let source = formula.latex().to_owned();
        div()
            .id(("copy-latex", formula.slot.before.0))
            .size_full()
            .on_click(move |_, _, cx| {
                cx.write_to_clipboard(ClipboardItem::new_string(source.clone()));
            })
    })
}
```

Click the formula in the example and paste into a text field to see `x^2`. To edit a formula, pass the new source to `formula.set_latex(source)` and apply the returned transaction with `EditorView::apply_node_view`. Use `formula.is_display()` to distinguish block from inline formulas.

The standalone `inline_math()` and `block_math()` plugins accept `inline_formula_controls` and `block_formula_controls`, respectively.

## Settings

### Formula types

`mathematics()` registers inline and block formulas. Register `inline_math()` or `block_math()` on its own to install just that node type, its commands, and its input rule.

```rust
use gpdoc::EditorConfig;
let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(gpdoc_plugin_mathematics::inline_math())
    .build()?;
```

GPUI typesets supported LaTeX and displays the stored source when it cannot typeset an expression.

## Commands

All six commands accept an optional nonnegative integer `pos` in document coordinates. Omitting it targets the start of the selection. For update and delete, supply the start position of the existing formula. A different node type at that position returns `CommandOutcome::NotApplicable`.

### `insertInlineMath`

Creates a formula inside a paragraph at the target position. Pass a nonempty `latex` string.

```rust
editor.run(gpdoc_plugin_mathematics::InsertInline {
    latex: "x^2".into(),
    pos: None,
})?;
```

### `updateInlineMath`

Replaces the source of an existing `inlineMath` node. The optional `latex` string defaults to its current source.

```rust
editor.run(gpdoc_plugin_mathematics::UpdateInline {
    latex: Some("x^3".into()),
    pos: Some(7),
})?;
```

### `deleteInlineMath`

Removes the `inlineMath` node at the target position. The command takes only the optional `pos`.

```rust
editor.run(gpdoc_plugin_mathematics::DeleteInline {
    pos: Some(7),
})?;
```

### `insertBlockMath`

Creates a formula on its own line at the target position. Pass a nonempty `latex` string.

```rust
editor.run(gpdoc_plugin_mathematics::InsertBlock {
    latex: "x^2".into(),
    pos: None,
})?;
```

### `updateBlockMath`

Replaces the source of an existing `blockMath` node. The optional `latex` string defaults to its current source.

```rust
editor.run(gpdoc_plugin_mathematics::UpdateBlock {
    latex: Some("x^3".into()),
    pos: Some(0),
})?;
```

### `deleteBlockMath`

Removes the `blockMath` node at the target position. The command takes only the optional `pos`.

```rust
editor.run(gpdoc_plugin_mathematics::DeleteBlock {
    pos: Some(0),
})?;
```

## Typing shortcuts

Type `$$x^2$$` inside a paragraph for inline math. Start an empty paragraph and type `$$$x^2$$$` for block math. The closing delimiter converts the expression.

## Source code

[mathematics/src/lib.rs](/source/plugins/mathematics/src/lib.rs.txt)