mathtex-editor-core 0.1.0

Headless core of the mathtex structural math editor: model, operations, navigation, selection, IR matching
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Exports the model to LaTeX with `SpanMap` byte ranges and phantom boxes for empty slots.

use std::collections::HashMap;
use std::ops::Range;

use crate::model::{Deco, FracStyle, Kind, Mark, MatrixEnv, NodeId, Selection, SeqId, Tree, Variant};

/// Byte ranges into the exported LaTeX for every node and slot.
#[derive(Debug, Clone, Default)]
pub struct SpanMap {
    /// Byte ranges for exported nodes.
    pub node: HashMap<NodeId, Range<usize>>,
    /// Byte ranges for exported sequences.
    pub seq: HashMap<SeqId, Range<usize>>,
}

impl Tree {
    /// Exports the whole document to rendering LaTeX and records spans for placeholder matching.
    pub fn export_latex(&self) -> (String, SpanMap) {
        let mut ex = Exporter::new(self, true);
        ex.emit_seq(self.root());
        (ex.out, ex.spans)
    }

    /// Exports the whole document to clean display and copy LaTeX.
    pub fn export_display(&self) -> String {
        let mut ex = Exporter::new(self, false);
        ex.emit_seq(self.root());
        ex.out
    }

    /// Exports a selection's content to clean LaTeX for the host clipboard.
    pub fn selection_latex(&self, sel: Selection) -> String {
        let lo = sel.anchor.min(sel.focus);
        let hi = sel.anchor.max(sel.focus).min(self.len(sel.seq));
        let mut ex = Exporter::new(self, false);
        let items = self.items(sel.seq)[lo..hi].to_vec();
        for n in items {
            ex.emit_node(n);
        }
        ex.out
    }
}

struct Exporter<'a> {
    tree: &'a Tree,
    out: String,
    spans: SpanMap,
    /// Controls whether empty slots render as `\phantom{x}` placeholders.
    placeholders: bool,
}

impl<'a> Exporter<'a> {
    fn new(tree: &'a Tree, placeholders: bool) -> Self {
        Self {
            tree,
            out: String::new(),
            spans: SpanMap::default(),
            placeholders,
        }
    }

    /// Emits a sequence's content or a phantom box when empty, then records its span.
    fn emit_seq(&mut self, seq: SeqId) {
        let start = self.out.len();
        if self.tree.is_empty(seq) {
            // Rendering uses a phantom box so the host can draw the visible placeholder at this span.
            if self.placeholders {
                self.out.push_str("\\phantom{x}");
            }
        } else {
            let items = self.tree.items(seq).to_vec();
            for (i, &n) in items.iter().enumerate() {
                self.emit_node(n);
                if self.needs_separator(n, items.get(i + 1).copied()) {
                    self.out.push(' ');
                }
            }
        }
        self.spans.seq.insert(seq, start..self.out.len());
    }

    /// A control word atom needs a space only when the next emitted character is a letter.
    fn needs_separator(&self, cur: NodeId, next: Option<NodeId>) -> bool {
        let cur_ends_in_control_word = match self.tree.kind(cur) {
            Some(Kind::Atom(s)) => is_control_word(&s.latex),
            // A clean exported big operator with empty limits is a bare control word.
            Some(Kind::BigOp { op, lower, upper }) => {
                !self.placeholders
                    && self.tree.is_empty(*lower)
                    && self.tree.is_empty(*upper)
                    && is_control_word(&op.latex)
            }
            _ => false,
        };
        if !cur_ends_in_control_word {
            return false;
        }
        next.and_then(|n| self.node_first_char(n))
            .is_some_and(char::is_alphabetic)
    }

    /// Reports the first character this node will emit for control word spacing.
    fn node_first_char(&self, node: NodeId) -> Option<char> {
        match self.tree.kind(node)? {
            Kind::Atom(s) => s.latex.chars().next(),
            Kind::Frac { style: FracStyle::Atop, .. } => Some('{'),
            Kind::Frac { .. }
            | Kind::Sqrt { .. }
            | Kind::Delim { .. }
            | Kind::Accent { .. }
            | Kind::Styled { .. }
            | Kind::Matrix { .. } => Some('\\'),
            // A big operator emits an operator command, so it starts with a backslash.
            Kind::BigOp { .. } => Some('\\'),
            Kind::Script { base, .. } => self.seq_first_char(*base),
            Kind::UnderOver { base, over, under, .. } => {
                if over.is_none() && under.is_none() {
                    self.seq_first_char(*base)
                } else {
                    Some('\\')
                }
            }
        }
    }

    /// Reports the first character a sequence will emit.
    fn seq_first_char(&self, seq: SeqId) -> Option<char> {
        if self.tree.is_empty(seq) {
            Some('\\')
        } else {
            self.node_first_char(self.tree.items(seq)[0])
        }
    }

    fn emit_braced(&mut self, seq: SeqId) {
        self.out.push('{');
        self.emit_seq(seq);
        self.out.push('}');
    }

    /// Emits a script argument, dropping braces only for a single character atom.
    fn emit_arg(&mut self, seq: SeqId) {
        let items = self.tree.items(seq);
        let bare = items.len() == 1
            && matches!(
                self.tree.kind(items[0]),
                Some(Kind::Atom(s)) if s.latex.chars().count() == 1
            );
        if bare {
            self.emit_seq(seq);
        } else {
            self.emit_braced(seq);
        }
    }

    fn emit_node(&mut self, node: NodeId) {
        let start = self.out.len();
        let Some(kind) = self.tree.kind(node).cloned() else {
            return;
        };
        match kind {
            Kind::Atom(s) => {
                self.out.push_str(&s.latex);
            }
            Kind::Frac { num, den, style } => match style {
                FracStyle::Atop => {
                    self.out.push('{');
                    self.emit_seq(num);
                    self.out.push_str("\\atop ");
                    self.emit_seq(den);
                    self.out.push('}');
                }
                _ => {
                    self.out.push_str(frac_cmd(style));
                    self.emit_braced(num);
                    self.emit_braced(den);
                }
            },
            Kind::Script { base, sub, sup } => {
                self.emit_seq(base);
                if let Some(s) = sub {
                    self.out.push('_');
                    self.emit_arg(s);
                }
                if let Some(s) = sup {
                    self.out.push('^');
                    self.emit_arg(s);
                }
            }
            Kind::BigOp { op, lower, upper } => {
                // Rendering keeps empty limits as placeholders, while clean export drops them.
                self.out.push_str(&op.latex);
                if self.placeholders || !self.tree.is_empty(lower) {
                    self.out.push('_');
                    self.emit_arg(lower);
                }
                if self.placeholders || !self.tree.is_empty(upper) {
                    self.out.push('^');
                    self.emit_arg(upper);
                }
            }
            Kind::Sqrt { index, radicand } => {
                self.out.push_str("\\sqrt");
                // Rendering keeps an empty degree as a placeholder, while clean export drops it.
                if self.placeholders || !self.tree.is_empty(index) {
                    self.out.push('[');
                    self.emit_seq(index);
                    self.out.push(']');
                }
                self.emit_braced(radicand);
            }
            Kind::Delim { open, close, body } => {
                self.out.push_str("\\left");
                self.out.push_str(&delim(open));
                self.emit_seq(body);
                self.out.push_str("\\right");
                self.out.push_str(&delim(close));
            }
            Kind::Accent { mark, base } => {
                self.out.push_str(accent_cmd(mark));
                self.emit_braced(base);
            }
            Kind::UnderOver {
                base,
                over,
                under,
                over_deco,
                under_deco,
            } => self.emit_under_over(base, over, under, over_deco, under_deco),
            Kind::Styled { variant, content } => {
                self.out.push_str(variant_cmd(variant));
                self.emit_braced(content);
            }
            Kind::Matrix { env, rows } => self.emit_matrix(env, &rows),
        }
        self.spans.node.insert(node, start..self.out.len());
    }

    fn emit_under_over(
        &mut self,
        base: SeqId,
        over: Option<SeqId>,
        under: Option<SeqId>,
        over_deco: Deco,
        under_deco: Deco,
    ) {
        match (over, under) {
            (Some(o), None) => match over_deco {
                Deco::Brace => {
                    self.out.push_str("\\overbrace");
                    self.emit_braced(base);
                    self.out.push('^');
                    self.emit_braced(o);
                }
                _ => {
                    self.out.push_str("\\overset");
                    self.emit_braced(o);
                    self.emit_braced(base);
                }
            },
            (None, Some(u)) => match under_deco {
                Deco::Brace => {
                    self.out.push_str("\\underbrace");
                    self.emit_braced(base);
                    self.out.push('_');
                    self.emit_braced(u);
                }
                _ => {
                    self.out.push_str("\\underset");
                    self.emit_braced(u);
                    self.emit_braced(base);
                }
            },
            (Some(o), Some(u)) => {
                self.out.push_str("\\overset");
                self.emit_braced(o);
                self.out.push('{');
                self.out.push_str("\\underset");
                self.emit_braced(u);
                self.emit_braced(base);
                self.out.push('}');
            }
            (None, None) => self.emit_seq(base),
        }
    }

    fn emit_matrix(&mut self, env: MatrixEnv, rows: &[Vec<SeqId>]) {
        let name = matrix_env_name(env);
        self.out.push_str("\\begin{");
        self.out.push_str(name);
        self.out.push('}');
        if matches!(env, MatrixEnv::Array) {
            let cols = rows.first().map_or(0, |r| r.len());
            self.out.push('{');
            self.out.push_str(&"c".repeat(cols));
            self.out.push('}');
        }
        for (ri, row) in rows.iter().enumerate() {
            if ri > 0 {
                self.out.push_str(" \\\\ ");
            }
            for (ci, &cell) in row.iter().enumerate() {
                if ci > 0 {
                    self.out.push_str(" & ");
                }
                self.emit_seq(cell);
            }
        }
        self.out.push_str("\\end{");
        self.out.push_str(name);
        self.out.push('}');
    }
}

fn is_control_word(latex: &str) -> bool {
    latex.starts_with('\\')
        && latex
            .chars()
            .last()
            .is_some_and(|c| c.is_ascii_alphabetic())
}

fn frac_cmd(style: FracStyle) -> &'static str {
    match style {
        FracStyle::Bar => "\\frac",
        FracStyle::Display => "\\dfrac",
        FracStyle::Text => "\\tfrac",
        FracStyle::Binom => "\\binom",
        FracStyle::Atop => "\\frac",
    }
}

fn delim(c: char) -> String {
    match c {
        '{' => "\\{".to_string(),
        '}' => "\\}".to_string(),
        c => c.to_string(),
    }
}

fn accent_cmd(mark: Mark) -> &'static str {
    match mark {
        Mark::Hat => "\\hat",
        Mark::Vec => "\\vec",
        Mark::Bar => "\\bar",
        Mark::Tilde => "\\tilde",
        Mark::Dot => "\\dot",
        Mark::Ddot => "\\ddot",
        Mark::Widehat => "\\widehat",
        Mark::Widetilde => "\\widetilde",
        Mark::Overline => "\\overline",
        Mark::Underline => "\\underline",
        Mark::Check => "\\check",
        Mark::Breve => "\\breve",
    }
}

fn variant_cmd(v: Variant) -> &'static str {
    match v {
        Variant::Normal => "\\mathnormal",
        Variant::Bold => "\\mathbf",
        Variant::Blackboard => "\\mathbb",
        Variant::Calligraphic => "\\mathcal",
        Variant::Fraktur => "\\mathfrak",
        Variant::Roman => "\\mathrm",
        Variant::SansSerif => "\\mathsf",
        Variant::Typewriter => "\\mathtt",
        Variant::Text => "\\text",
        Variant::OperatorName => "\\operatorname",
    }
}

fn matrix_env_name(env: MatrixEnv) -> &'static str {
    match env {
        MatrixEnv::Matrix => "matrix",
        MatrixEnv::Pmatrix => "pmatrix",
        MatrixEnv::Bmatrix => "bmatrix",
        MatrixEnv::Vmatrix => "vmatrix",
        MatrixEnv::Cases => "cases",
        MatrixEnv::Aligned => "aligned",
        MatrixEnv::Array => "array",
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{Cursor, MathClass, ScriptSlot, Symbol};

    fn atom(c: &str) -> Symbol {
        Symbol {
            latex: c.into(),
            class: MathClass::Ord,
        }
    }

    #[test]
    fn export_fraction_with_byte_spans() {
        let mut t = Tree::new();
        let root = t.root();
        let cnum = t.insert_fraction(Cursor { seq: root, index: 0 }, FracStyle::Bar, None);
        t.insert_atom(cnum, atom("a"));
        let frac = t.items(root)[0];
        let (num, den) = (t.child_seqs(frac)[0], t.child_seqs(frac)[1]);
        t.insert_atom(Cursor { seq: den, index: 0 }, atom("b"));

        let (s, spans) = t.export_latex();
        assert_eq!(s, "\\frac{a}{b}");
        assert_eq!(&s[spans.node[&frac].clone()], "\\frac{a}{b}");
        assert_eq!(&s[spans.seq[&num].clone()], "a");
        assert_eq!(&s[spans.seq[&den].clone()], "b");
    }

    #[test]
    fn empty_slot_emits_phantom_with_a_span() {
        let mut t = Tree::new();
        let root = t.root();
        t.insert_fraction(Cursor { seq: root, index: 0 }, FracStyle::Bar, None);
        let frac = t.items(root)[0];
        let num = t.child_seqs(frac)[0];
        let (s, spans) = t.export_latex();
        // Empty fraction slots each emit a phantom box for host placeholders.
        assert_eq!(s, "\\frac{\\phantom{x}}{\\phantom{x}}");
        assert_eq!(&s[spans.seq[&num].clone()], "\\phantom{x}");
    }

    #[test]
    fn typed_specials_export_escaped() {
        let mut t = Tree::new();
        let root = t.root();
        let mut c = Cursor { seq: root, index: 0 };
        for ch in "50%&$".chars() {
            c = t.insert_atom(c, Symbol::from_char(ch));
        }
        let (s, _) = t.export_latex();
        assert_eq!(s, "50\\%\\&\\$");
    }

    #[test]
    fn export_script_and_sqrt() {
        let mut t = Tree::new();
        let root = t.root();
        t.insert_atom(Cursor { seq: root, index: 0 }, atom("x"));
        let c = t.attach_script(Cursor { seq: root, index: 1 }, ScriptSlot::Sup);
        t.insert_atom(c, atom("2"));
        let (s, _) = t.export_latex();
        assert_eq!(s, "x^2");
    }

    #[test]
    fn control_word_before_scripted_letter_keeps_separator() {
        // The space after `\sum` must survive when `x` becomes a script base.
        let mut t = Tree::new();
        let root = t.root();
        let mut c = Cursor { seq: root, index: 0 };
        c = t.insert_atom(c, atom("\\sum"));
        c = t.insert_atom(c, atom("x"));
        let sup = t.attach_script(c, ScriptSlot::Sup);
        t.insert_atom(sup, atom("2"));
        let (s, _) = t.export_latex();
        assert_eq!(s, "\\sum x^2");
    }

    #[test]
    fn control_word_before_command_needs_no_separator() {
        // The following backslash already terminates `\sum`.
        let mut t = Tree::new();
        let root = t.root();
        let c = t.insert_atom(Cursor { seq: root, index: 0 }, atom("\\sum"));
        let num = t.insert_fraction(c, FracStyle::Bar, None);
        t.insert_atom(num, atom("a"));
        let (s, _) = t.export_latex();
        assert_eq!(s, "\\sum\\frac{a}{\\phantom{x}}");
    }

    #[test]
    fn script_multichar_keeps_braces() {
        let mut t = Tree::new();
        let root = t.root();
        t.insert_atom(Cursor { seq: root, index: 0 }, atom("x"));
        let c = t.attach_script(Cursor { seq: root, index: 1 }, ScriptSlot::Sup);
        let c = t.insert_atom(c, atom("1"));
        t.insert_atom(c, atom("2"));
        let (s, _) = t.export_latex();
        assert_eq!(s, "x^{12}");
    }
}