tui-math 0.1.1

Render LaTeX math beautifully in terminal UIs with ratatui
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! MathML to Unicode terminal renderer

use crate::mathbox::MathBox;
use crate::unicode_maps::{get_greek, get_symbol, to_subscript, to_superscript, BRACKETS};
use latex2mathml::{latex_to_mathml, DisplayStyle};
use roxmltree::{Document, Node};
use std::fmt;

/// Errors that can occur during math rendering
#[derive(Debug)]
pub enum RenderError {
    LatexConversion(String),
    MathMLParse(String),
    InvalidStructure(String),
}

impl fmt::Display for RenderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RenderError::LatexConversion(e) => write!(f, "LaTeX conversion error: {}", e),
            RenderError::MathMLParse(e) => write!(f, "MathML parse error: {}", e),
            RenderError::InvalidStructure(e) => write!(f, "Invalid math structure: {}", e),
        }
    }
}

impl std::error::Error for RenderError {}

/// Math renderer that converts LaTeX/MathML to Unicode terminal output
pub struct MathRenderer {
    use_unicode_scripts: bool,
}

impl MathRenderer {
    pub fn new() -> Self {
        Self {
            use_unicode_scripts: true,
        }
    }

    /// Set whether to use Unicode superscript/subscript characters when possible
    pub fn use_unicode_scripts(mut self, use_unicode: bool) -> Self {
        self.use_unicode_scripts = use_unicode;
        self
    }

    /// Render LaTeX math to Unicode string
    pub fn render_latex(&self, latex: &str) -> Result<String, RenderError> {
        let mathml = latex_to_mathml(latex, DisplayStyle::Inline)
            .map_err(|e| RenderError::LatexConversion(e.to_string()))?;
        self.render_mathml(&mathml)
    }

    /// Render MathML to Unicode string
    pub fn render_mathml(&self, mathml: &str) -> Result<String, RenderError> {
        let doc = Document::parse(mathml)
            .map_err(|e| RenderError::MathMLParse(e.to_string()))?;
        let root = doc.root_element();
        let math_box = self.process_element(&root)?;
        Ok(math_box.to_string())
    }

    /// Render to MathBox (for advanced usage)
    pub fn render_to_box(&self, latex: &str) -> Result<MathBox, RenderError> {
        let mathml = latex_to_mathml(latex, DisplayStyle::Inline)
            .map_err(|e| RenderError::LatexConversion(e.to_string()))?;
        let doc = Document::parse(&mathml)
            .map_err(|e| RenderError::MathMLParse(e.to_string()))?;
        let root = doc.root_element();
        self.process_element(&root)
    }

    fn process_element(&self, node: &Node) -> Result<MathBox, RenderError> {
        let tag = node.tag_name().name();

        match tag {
            "math" | "mrow" | "mstyle" | "mpadded" | "mphantom" => {
                self.process_row(node)
            }
            "mi" | "mn" | "mtext" => {
                self.process_text(node)
            }
            "mo" => {
                self.process_operator(node)
            }
            "msup" => {
                self.process_superscript(node)
            }
            "msub" => {
                self.process_subscript(node)
            }
            "msubsup" => {
                self.process_subsup(node)
            }
            "mfrac" => {
                self.process_fraction(node)
            }
            "msqrt" => {
                self.process_sqrt(node)
            }
            "mroot" => {
                self.process_nthroot(node)
            }
            "mover" => {
                self.process_over(node)
            }
            "munder" => {
                self.process_under(node)
            }
            "munderover" => {
                self.process_underover(node)
            }
            "mtable" => {
                self.process_table(node)
            }
            "mtr" => {
                self.process_table_row(node)
            }
            "mtd" => {
                self.process_row(node)
            }
            "mfenced" => {
                self.process_fenced(node)
            }
            "menclose" => {
                self.process_row(node) // Simplified
            }
            "mspace" => {
                Ok(MathBox::from_text(" "))
            }
            "semantics" => {
                // Process first child only
                if let Some(child) = node.children().filter(|n| n.is_element()).next() {
                    self.process_element(&child)
                } else {
                    Ok(MathBox::empty(0, 1, 0))
                }
            }
            "annotation" | "annotation-xml" => {
                // Skip annotations
                Ok(MathBox::empty(0, 1, 0))
            }
            _ => {
                // Unknown element, try to process children
                self.process_row(node)
            }
        }
    }

    fn process_row(&self, node: &Node) -> Result<MathBox, RenderError> {
        self.process_row_inner(node, true)
    }

    fn process_row_compact(&self, node: &Node) -> Result<MathBox, RenderError> {
        self.process_row_inner(node, false)
    }

    fn process_row_inner(&self, node: &Node, add_spacing: bool) -> Result<MathBox, RenderError> {
        let child_nodes: Vec<_> = node.children().filter(|n| n.is_element()).collect();

        if child_nodes.is_empty() {
            let text = self.get_text_content(node);
            if !text.is_empty() {
                return Ok(MathBox::from_text(&text));
            }
            return Ok(MathBox::empty(0, 1, 0));
        }

        let mut boxes = Vec::new();
        let mut prev_multiline = false;

        for (i, child) in child_nodes.iter().enumerate() {
            let child_box = self.process_element(child)?;
            let is_multiline = child_box.height > 1;

            // Add spacing between multi-line elements
            if add_spacing && i > 0 && (prev_multiline || is_multiline) {
                boxes.push(MathBox::from_text(" "));
            }

            // Add spacing around binary operators in row context (not in compact mode)
            if add_spacing && child.tag_name().name() == "mo" {
                let op = self.get_text_content(child);
                let is_first = i == 0;
                let is_binary_op = !is_first && matches!(op.as_str(), "+" | "-" | "±" | "");
                let is_relation = matches!(
                    op.as_str(),
                    "=" | "" | "" | "" | "" | "" | "" | "" | "" | "×" | "÷" | "·"
                );

                if is_binary_op || is_relation {
                    // Don't add extra space if we just added one for multiline
                    if !prev_multiline && !is_multiline {
                        boxes.push(MathBox::from_text(" "));
                    }
                    boxes.push(child_box);
                    boxes.push(MathBox::from_text(" "));
                    prev_multiline = is_multiline;
                    continue;
                }
            }
            boxes.push(child_box);
            prev_multiline = is_multiline;
        }

        Ok(MathBox::concat_horizontal(&boxes))
    }

    fn process_text(&self, node: &Node) -> Result<MathBox, RenderError> {
        let text = self.get_text_content(node);

        // Handle Greek letters and special identifiers
        if let Some(greek) = get_greek(&text) {
            return Ok(MathBox::from_text(&greek.to_string()));
        }

        Ok(MathBox::from_text(&text))
    }

    fn process_operator(&self, node: &Node) -> Result<MathBox, RenderError> {
        let text = self.get_text_content(node);

        // Handle special operators
        let rendered = match text.as_str() {
            "" | "" | "" | "" | "" | "" | "" | "" => {
                // Big operators - keep as is
                text
            }
            _ => {
                // Check if it's a LaTeX command
                if text.starts_with('\\') {
                    let cmd = &text[1..];
                    if let Some(sym) = get_symbol(cmd) {
                        sym.to_string()
                    } else if let Some(greek) = get_greek(cmd) {
                        greek.to_string()
                    } else {
                        text
                    }
                } else {
                    text
                }
            }
        };

        // Spacing is handled in process_row for context-aware operator spacing
        Ok(MathBox::from_text(&rendered))
    }

    fn process_superscript(&self, node: &Node) -> Result<MathBox, RenderError> {
        let children: Vec<_> = node.children().filter(|n| n.is_element()).collect();
        if children.len() != 2 {
            return Err(RenderError::InvalidStructure(
                "msup requires exactly 2 children".to_string(),
            ));
        }

        let base = self.process_element(&children[0])?;
        // Use compact mode for superscript content (no spacing around operators)
        let sup = if children[1].tag_name().name() == "mrow" {
            self.process_row_compact(&children[1])?
        } else {
            self.process_element(&children[1])?
        };

        // Try Unicode superscript for simple cases
        if self.use_unicode_scripts && base.height == 1 && sup.height == 1 {
            let sup_text = sup.to_string();
            if let Some(unicode_sup) = to_superscript(sup_text.trim()) {
                let combined = format!("{}{}", base.to_string(), unicode_sup);
                return Ok(MathBox::from_text(&combined));
            }
        }

        // Fall back to 2D rendering
        let width = base.width + sup.width;
        let height = base.height + 1;
        let mut result = MathBox::empty(width, height, base.baseline + 1);

        // Place base at bottom
        result.blit(&base, 0, 1);
        // Place superscript at top-right
        result.blit(&sup, base.width, 0);

        Ok(result)
    }

    fn process_subscript(&self, node: &Node) -> Result<MathBox, RenderError> {
        let children: Vec<_> = node.children().filter(|n| n.is_element()).collect();
        if children.len() != 2 {
            return Err(RenderError::InvalidStructure(
                "msub requires exactly 2 children".to_string(),
            ));
        }

        let base = self.process_element(&children[0])?;
        // Use compact mode for subscript content (no spacing around operators)
        let sub = if children[1].tag_name().name() == "mrow" {
            self.process_row_compact(&children[1])?
        } else {
            self.process_element(&children[1])?
        };

        // Try Unicode subscript for simple cases
        if self.use_unicode_scripts && base.height == 1 && sub.height == 1 {
            let sub_text = sub.to_string();
            if let Some(unicode_sub) = to_subscript(sub_text.trim()) {
                let combined = format!("{}{}", base.to_string(), unicode_sub);
                return Ok(MathBox::from_text(&combined));
            }
        }

        // Fall back to 2D rendering
        let width = base.width + sub.width;
        let height = base.height + 1;
        let mut result = MathBox::empty(width, height, base.baseline);

        // Place base at top
        result.blit(&base, 0, 0);
        // Place subscript at bottom-right
        result.blit(&sub, base.width, base.height);

        Ok(result)
    }

    fn process_subsup(&self, node: &Node) -> Result<MathBox, RenderError> {
        let children: Vec<_> = node.children().filter(|n| n.is_element()).collect();
        if children.len() != 3 {
            return Err(RenderError::InvalidStructure(
                "msubsup requires exactly 3 children".to_string(),
            ));
        }

        // Check if base is a big operator (integral, sum, etc.)
        let base_text = self.get_text_content(&children[0]);
        let is_big_operator = matches!(
            base_text.as_str(),
            "" | "" | "" | "" | "" | "" | "" | ""
        );

        let base = self.process_element(&children[0])?;
        let sub = self.process_element(&children[1])?;
        let sup = self.process_element(&children[2])?;

        // For big operators, stack limits vertically (centered)
        if is_big_operator {
            return Ok(MathBox::stack_vertical(&[sup, base, sub]));
        }

        // Try Unicode scripts for simple cases
        if self.use_unicode_scripts && base.height == 1 && sub.height == 1 && sup.height == 1 {
            let sub_text = sub.to_string();
            let sup_text = sup.to_string();
            if let (Some(unicode_sub), Some(unicode_sup)) =
                (to_subscript(sub_text.trim()), to_superscript(sup_text.trim()))
            {
                let combined = format!("{}{}{}", base.to_string(), unicode_sub, unicode_sup);
                return Ok(MathBox::from_text(&combined));
            }
        }

        // 2D rendering with both
        let script_width = sub.width.max(sup.width);
        let width = base.width + script_width;
        let height = base.height + 2;
        let mut result = MathBox::empty(width, height, base.baseline + 1);

        result.blit(&base, 0, 1);
        result.blit(&sup, base.width, 0);
        result.blit(&sub, base.width, height - 1);

        Ok(result)
    }

    fn process_fraction(&self, node: &Node) -> Result<MathBox, RenderError> {
        let children: Vec<_> = node.children().filter(|n| n.is_element()).collect();
        if children.len() != 2 {
            return Err(RenderError::InvalidStructure(
                "mfrac requires exactly 2 children".to_string(),
            ));
        }

        let num = self.process_element(&children[0])?;
        let den = self.process_element(&children[1])?;

        let width = num.width.max(den.width);
        let height = num.height + 1 + den.height;
        let baseline = num.height;

        let mut result = MathBox::empty(width, height, baseline);

        // Center numerator
        let num_offset = (width - num.width) / 2;
        result.blit(&num, num_offset, 0);

        // Draw fraction line using box-drawing character
        result.fill_row(num.height, '');

        // Center denominator
        let den_offset = (width - den.width) / 2;
        result.blit(&den, den_offset, num.height + 1);

        Ok(result)
    }

    fn process_sqrt(&self, node: &Node) -> Result<MathBox, RenderError> {
        let inner = self.process_row(node)?;

        // Simple sqrt rendering: √ followed by content with overline
        // Layout: ___
        //        √abc

        if inner.height == 1 {
            let inner_text = inner.to_string();
            let inner_width = inner_text.chars().count();

            // Single line: √ + content, with overline above content
            let width = 1 + inner_width;
            let height = 2;
            let mut result = MathBox::empty(width, height, 1);

            // Draw bar above the content (not above √)
            for x in 1..width {
                result.set(x, 0, '_');
            }

            // Draw √ and content
            result.set(0, 1, '');
            for (i, ch) in inner_text.chars().enumerate() {
                result.set(1 + i, 1, ch);
            }

            return Ok(result);
        }

        // Multi-line sqrt - use simple bracket approach
        let width = inner.width + 1;
        let height = inner.height + 1;
        let mut result = MathBox::empty(width, height, inner.baseline + 1);

        // Draw bar
        for x in 1..width {
            result.set(x, 0, '_');
        }

        // Draw √ at the left
        result.set(0, 1, '');

        // Place content
        result.blit(&inner, 1, 1);

        Ok(result)
    }

    fn process_nthroot(&self, node: &Node) -> Result<MathBox, RenderError> {
        let children: Vec<_> = node.children().filter(|n| n.is_element()).collect();
        if children.len() != 2 {
            return Err(RenderError::InvalidStructure(
                "mroot requires exactly 2 children".to_string(),
            ));
        }

        let inner = self.process_element(&children[0])?;
        let index = self.process_element(&children[1])?;

        // Try Unicode superscript for index
        let index_text = index.to_string();
        if let Some(unicode_idx) = to_superscript(index_text.trim()) {
            let text = format!("{}{}", unicode_idx, inner.to_string());
            return Ok(MathBox::from_text(&text));
        }

        // 2D rendering
        let width = index.width + inner.width + 2;
        let height = (inner.height + 1).max(index.height);
        let mut result = MathBox::empty(width, height, height / 2);

        // Place index
        result.blit(&index, 0, 0);

        // Draw sqrt and content
        result.set(index.width, height - 1, '');
        for x in (index.width + 1)..width {
            result.set(x, 0, '');
        }
        result.blit(&inner, index.width + 2, 1);

        Ok(result)
    }

    fn process_over(&self, node: &Node) -> Result<MathBox, RenderError> {
        let children: Vec<_> = node.children().filter(|n| n.is_element()).collect();
        if children.len() != 2 {
            return Err(RenderError::InvalidStructure(
                "mover requires exactly 2 children".to_string(),
            ));
        }

        let base = self.process_element(&children[0])?;
        let over = self.process_element(&children[1])?;

        let over_text = over.to_string().trim().to_string();

        // Handle common accents on single-height bases
        if base.height == 1 {
            let accent = match over_text.as_str() {
                "^" | "ˆ" => Some("̂"),  // Combining circumflex
                "~" | "˜" => Some("̃"),  // Combining tilde
                "¯" | "-" => Some("̄"),  // Combining macron (bar)
                "." => Some("̇"),        // Combining dot above
                ".." | "¨" => Some("̈"), // Combining diaeresis
                "" => Some(""),        // Combining right arrow
                _ => None,
            };
            if let Some(combining) = accent {
                let base_text = base.to_string();
                let text = format!("{}{}", base_text, combining);
                return Ok(MathBox::from_text(&text));
            }
        }

        // Stack vertically
        Ok(MathBox::stack_vertical(&[over, base]))
    }

    fn process_under(&self, node: &Node) -> Result<MathBox, RenderError> {
        let children: Vec<_> = node.children().filter(|n| n.is_element()).collect();
        if children.len() != 2 {
            return Err(RenderError::InvalidStructure(
                "munder requires exactly 2 children".to_string(),
            ));
        }

        let base_text = self.get_text_content(&children[0]);
        let base = self.process_element(&children[0])?;
        let under = self.process_element(&children[1])?;

        // For "lim" and similar operators, render subscript inline
        if base_text == "lim" || base_text == "max" || base_text == "min" || base_text == "sup" || base_text == "inf" {
            // Try to convert to Unicode subscript, fallback to parentheses
            let under_text = under.to_string();
            let under_trimmed = under_text.trim();

            // Try full Unicode subscript conversion
            if let Some(subscript) = to_subscript(under_trimmed) {
                let combined = format!("{}{}", base_text, subscript);
                return Ok(MathBox::from_text(&combined));
            }

            // Fallback: use parentheses notation
            let combined = format!("{}({})", base_text, under_trimmed);
            return Ok(MathBox::from_text(&combined));
        }

        // For other elements, stack with baseline at the base element
        let width = base.width.max(under.width);
        let height = base.height + under.height;
        let mut result = MathBox::empty(width, height, base.baseline);

        // Center base
        let base_offset = (width - base.width) / 2;
        result.blit(&base, base_offset, 0);

        // Center under below base
        let under_offset = (width - under.width) / 2;
        result.blit(&under, under_offset, base.height);

        Ok(result)
    }

    fn process_underover(&self, node: &Node) -> Result<MathBox, RenderError> {
        let children: Vec<_> = node.children().filter(|n| n.is_element()).collect();
        if children.len() != 3 {
            return Err(RenderError::InvalidStructure(
                "munderover requires exactly 3 children".to_string(),
            ));
        }

        let base = self.process_element(&children[0])?;
        let under = self.process_element(&children[1])?;
        let over = self.process_element(&children[2])?;

        Ok(MathBox::stack_vertical(&[over, base, under]))
    }

    fn process_table(&self, node: &Node) -> Result<MathBox, RenderError> {
        let rows: Vec<Vec<MathBox>> = node
            .children()
            .filter(|n| n.is_element() && n.tag_name().name() == "mtr")
            .map(|row| {
                row.children()
                    .filter(|n| n.is_element() && n.tag_name().name() == "mtd")
                    .map(|cell| self.process_row(&cell))
                    .collect::<Result<Vec<_>, _>>()
            })
            .collect::<Result<Vec<_>, _>>()?;

        if rows.is_empty() {
            return Ok(MathBox::empty(0, 1, 0));
        }

        // Calculate column widths and row heights
        let num_cols = rows.iter().map(|r| r.len()).max().unwrap_or(0);
        let mut col_widths = vec![0; num_cols];
        let mut row_heights = vec![0; rows.len()];

        for (i, row) in rows.iter().enumerate() {
            for (j, cell) in row.iter().enumerate() {
                col_widths[j] = col_widths[j].max(cell.width);
                row_heights[i] = row_heights[i].max(cell.height);
            }
        }

        // Add spacing
        let spacing = 2;
        let total_width: usize = col_widths.iter().sum::<usize>() + spacing * (num_cols.saturating_sub(1));
        let total_height: usize = row_heights.iter().sum();

        let mut result = MathBox::empty(total_width, total_height, total_height / 2);

        let mut y_pos = 0;
        for (i, row) in rows.iter().enumerate() {
            let mut x_pos = 0;
            for (j, cell) in row.iter().enumerate() {
                // Center cell in its column
                let x_offset = (col_widths[j] - cell.width) / 2;
                result.blit(cell, x_pos + x_offset, y_pos);
                x_pos += col_widths[j] + spacing;
            }
            y_pos += row_heights[i];
        }

        Ok(result)
    }

    fn process_table_row(&self, node: &Node) -> Result<MathBox, RenderError> {
        let cells: Vec<MathBox> = node
            .children()
            .filter(|n| n.is_element())
            .map(|n| self.process_row(&n))
            .collect::<Result<Vec<_>, _>>()?;

        // Join cells with spacing
        let spacing = MathBox::from_text("  ");
        let mut parts = Vec::new();
        for (i, cell) in cells.into_iter().enumerate() {
            if i > 0 {
                parts.push(spacing.clone());
            }
            parts.push(cell);
        }

        Ok(MathBox::concat_horizontal(&parts))
    }

    fn process_fenced(&self, node: &Node) -> Result<MathBox, RenderError> {
        let open = node.attribute("open").unwrap_or("(");
        let close = node.attribute("close").unwrap_or(")");

        let inner = self.process_row(node)?;

        if inner.height <= 1 {
            // Simple case
            let text = format!("{}{}{}", open, inner.to_string(), close);
            return Ok(MathBox::from_text(&text));
        }

        // Scaled brackets
        let left_chars = BRACKETS.get_left(open, inner.height);
        let right_chars = BRACKETS.get_right(close, inner.height);

        let width = 1 + inner.width + 1;
        let height = inner.height;
        let mut result = MathBox::empty(width, height, inner.baseline);

        // Draw brackets
        for (y, &ch) in left_chars.iter().enumerate() {
            result.set(0, y, ch);
        }
        for (y, &ch) in right_chars.iter().enumerate() {
            result.set(width - 1, y, ch);
        }

        // Place content
        result.blit(&inner, 1, 0);

        Ok(result)
    }

    fn get_text_content(&self, node: &Node) -> String {
        let mut text = String::new();
        for child in node.children() {
            if child.is_text() {
                text.push_str(child.text().unwrap_or(""));
            }
        }
        text.trim().to_string()
    }
}

impl Default for MathRenderer {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_simple_expression() {
        let renderer = MathRenderer::new();
        let result = renderer.render_latex("x + y").unwrap();
        assert!(result.contains('x'));
        assert!(result.contains('y'));
    }

    #[test]
    fn test_superscript() {
        let renderer = MathRenderer::new();
        let result = renderer.render_latex("x^2").unwrap();
        // Should contain Unicode superscript
        assert!(result.contains('²') || result.contains('2'));
    }

    #[test]
    fn test_fraction() {
        let renderer = MathRenderer::new();
        let result = renderer.render_latex(r"\frac{a}{b}").unwrap();
        assert!(result.contains('a'));
        assert!(result.contains('b'));
        assert!(result.contains(''));
    }
}