Skip to content
gpdoc
Playground

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.

Terminal window
cargo add gpdoc-plugin-mathematics

Register the typesetting view for inline and block formulas in the Quickstart editor configuration.

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.

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.

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()

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.

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.

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.

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.

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.

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

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

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

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

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

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

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

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

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

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

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

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

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.

mathematics/src/lib.rs