--- license: cc-by-nc-sa-4.0 library_name: deepcursive tags: - handwriting - handwriting-generation - online-handwriting - digital-ink - math - latex - remarkable datasets: - MathWriting language: - en pipeline_tag: other --- # DeepCursive formula model M An autoregressive transformer that writes **LaTeX math as handwritten pen strokes**. It outputs vector strokes, not pixels. It is part of [DeepCursive](https://github.com/matejban/deepcursive), which turns Markdown and LaTeX into handwritten vector PDFs and reMarkable notebooks (`.rmdoc`). ![Raw samples](samples.png) *Raw samples: seed 1, temperature 0.7, one attempt each, no selection.* ## What it does - **Input:** a LaTeX formula, tokenized into a 257-token vocabulary. - **Output:** a sequence of stroke tokens, decoded into pen strokes. - The model plans the whole formula itself: fractions, roots, sub- and superscripts, and integral limits. ## Model | | | |---|---| | Architecture | encoder-decoder transformer (`InkTransformer`), 4 encoder + 8 decoder layers, d=384, 6 heads, RoPE | | Parameters | 27.4 M | | Ink vocabulary | 1,337 tokens (DeepCursive stroke codec v2: direction/length chords + pen jumps) | | LaTeX vocabulary | 257 tokens | | Training | 60,000 steps, ~5 h on one T4 (Kaggle), no synthetic data | | Training data | MathWriting train split: 229,863 human-written formulas + 6,423 isolated symbols | ## Results These numbers come from 1,000 held-out MathWriting formulas. Each generated formula is read back by a handwriting recognizer, and its output is compared to the source LaTeX. Human handwriting goes through the same recognizer as a reference point. | | CER (short formulas) | CER (all) | ExpRate | unfinished | |---|---|---|---|---| | **This model, one attempt** | **0.031** | **0.029** | 81.2 % | 0 | | Human handwriting (same recognizer) | 0.049 | 0.038 | — | — | On DeepCursive's symbol-coverage benchmark (rare symbols and constructions), it writes 49.6 % of formulas correctly in a single attempt. In DeepCursive, the model draws N samples per formula and keeps the first one the recognizer reads back exactly. If none matches, it falls back to the layout renderer. ## Usage The easiest way is through DeepCursive. It downloads this model from a pinned revision, verifies its SHA-256 and uses it for `--engine model`: ```bash git clone https://github.com/matejban/deepcursive && cd deepcursive && ./scripts/setup.sh --with-model .venv/bin/deepcursive render examples/formule.md -o formule.pdf --engine model ``` To use the weights directly: ```bash hf download mbanban/deepcursive-formula-m --local-dir models/formula-m ``` ```python import json, torch from safetensors.torch import load_file from deepcursive.model.net import InkTransformer, ModelConfig from deepcursive.model.inkcode import InkCodec from deepcursive.model.latex_tokens import LatexVocab from deepcursive.model.train import sample_formulas cfg = json.load(open("models/formula-m/config.json")) codec = InkCodec.from_config(cfg["codec"]) vocab = LatexVocab(cfg["latex_vocab"]) model = InkTransformer(ModelConfig(**cfg["model_config"]), codec) model.load_state_dict(load_file("models/formula-m/model.safetensors")) model.eval() strokes = sample_formulas(model, codec, vocab, [r"\frac{a}{b}=\sqrt{x^{2}+1}"], seed=0)[0] # strokes: list of (N, 2) arrays in model units, y pointing down ``` ## Limitations - Function names follow the MathWriting convention and are written as letters (`log_{2}`, `sin`, `lim`). `\log`, `\sin` and `\lim` are not in the vocabulary. - A single sample is sometimes wrong: a dropped or extra symbol, or crowded sub- and superscripts. Use it with a recognizer check and several attempts, as DeepCursive does. - Very long formulas (well beyond typical MathWriting length) degrade. - It writes in one generic style learned from many writers. To get a specific person's handwriting, DeepCursive uses its own layout plus glyphs in that writer's style. ## License The weights are released under **CC BY-NC-SA 4.0**, because the training data (MathWriting, Google) is under CC BY-NC-SA 4.0. Non-commercial use only. Share derivatives under the same license. The DeepCursive code is MIT-licensed (see the GitHub repo). ## Citation If you use this model, please also cite the dataset: ```bibtex @misc{gervais2024mathwriting, title = {MathWriting: A Dataset For Handwritten Mathematical Expression Recognition}, author = {Philippe Gervais and Anastasiia Fadeeva and Andrii Maksai}, year = {2024}, eprint = {2404.10690}, archivePrefix = {arXiv} } ```