tylax 0.3.5

Tylax - High-performance bidirectional LaTeX ↔ Typst converter
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
//! Robust Table Handling Module
//!
//! This module provides comprehensive table parsing and conversion,
//! inspired by Pandoc's grid table logic. Supports multicolumn, multirow,
//! and various alignment specifications.

#![allow(clippy::while_let_on_iterator)]

/// Cell alignment
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Alignment {
    #[default]
    Default,
    Left,
    Center,
    Right,
}

impl Alignment {
    /// Parse from LaTeX alignment character
    pub fn from_latex_char(c: char) -> Self {
        match c {
            'l' => Alignment::Left,
            'c' => Alignment::Center,
            'r' => Alignment::Right,
            'p' | 'm' | 'b' | 'X' => Alignment::Left, // paragraph types
            _ => Alignment::Default,
        }
    }

    /// Convert to LaTeX alignment character
    pub fn to_latex_char(&self) -> char {
        match self {
            Alignment::Left | Alignment::Default => 'l',
            Alignment::Center => 'c',
            Alignment::Right => 'r',
        }
    }

    /// Convert to Typst alignment
    pub fn to_typst(&self) -> &'static str {
        match self {
            Alignment::Left | Alignment::Default => "left",
            Alignment::Center => "center",
            Alignment::Right => "right",
        }
    }
}

/// Column width specification
#[derive(Debug, Clone, PartialEq, Default)]
pub enum ColWidth {
    /// Auto-determined width
    #[default]
    Auto,
    /// Fixed width (in some unit, normalized to 0-1 fraction)
    Fixed(f64),
    /// Percentage of table width
    Percent(f64),
}

/// Column specification
#[derive(Debug, Clone, Default)]
pub struct ColSpec {
    pub alignment: Alignment,
    pub width: ColWidth,
    pub has_left_border: bool,
    pub has_right_border: bool,
}

/// A single table cell
#[derive(Debug, Clone)]
pub struct Cell {
    /// Cell content (can contain formatted text, math, etc.)
    pub content: String,
    /// Number of columns this cell spans
    pub colspan: u32,
    /// Number of rows this cell spans
    pub rowspan: u32,
    /// Cell-specific alignment (overrides column default)
    pub alignment: Option<Alignment>,
}

impl Cell {
    pub fn new(content: String) -> Self {
        Self {
            content,
            colspan: 1,
            rowspan: 1,
            alignment: None,
        }
    }

    pub fn with_span(content: String, colspan: u32, rowspan: u32) -> Self {
        Self {
            content,
            colspan,
            rowspan,
            alignment: None,
        }
    }

    pub fn is_empty(&self) -> bool {
        self.content.trim().is_empty()
    }
}

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

/// A table row
#[derive(Debug, Clone, Default)]
pub struct Row {
    pub cells: Vec<Cell>,
    /// Whether this row has a bottom border (hline)
    pub has_bottom_border: bool,
}

impl Row {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn push(&mut self, cell: Cell) {
        self.cells.push(cell);
    }

    pub fn is_empty(&self) -> bool {
        self.cells.is_empty() || self.cells.iter().all(|c| c.is_empty())
    }
}

/// Table caption
#[derive(Debug, Clone, Default)]
pub struct Caption {
    pub short: Option<String>,
    pub long: String,
}

/// Complete table structure
#[derive(Debug, Clone)]
pub struct Table {
    /// Column specifications
    pub colspecs: Vec<ColSpec>,
    /// Header rows
    pub header: Vec<Row>,
    /// Body rows
    pub body: Vec<Row>,
    /// Footer rows
    pub footer: Vec<Row>,
    /// Table caption
    pub caption: Option<Caption>,
    /// Table label for cross-referencing
    pub label: Option<String>,
    /// Whether the table has a top border
    pub has_top_border: bool,
}

impl Table {
    pub fn new(num_cols: usize) -> Self {
        Self {
            colspecs: vec![ColSpec::default(); num_cols],
            header: Vec::new(),
            body: Vec::new(),
            footer: Vec::new(),
            caption: None,
            label: None,
            has_top_border: false,
        }
    }

    pub fn num_cols(&self) -> usize {
        self.colspecs.len()
    }

    pub fn push_header(&mut self, row: Row) {
        self.header.push(row);
    }

    pub fn push_body(&mut self, row: Row) {
        self.body.push(row);
    }

    pub fn push_footer(&mut self, row: Row) {
        self.footer.push(row);
    }
}

// ============================================================================
// LaTeX Table Parsing
// ============================================================================

/// Parse LaTeX table environment
pub fn parse_latex_table(input: &str) -> Option<Table> {
    // Detect table environment type
    let is_tabular = input.contains("\\begin{tabular}");
    let is_longtable = input.contains("\\begin{longtable}");
    let is_tabularx = input.contains("\\begin{tabularx}");

    if !is_tabular && !is_longtable && !is_tabularx {
        return None;
    }

    // Extract column specification
    let colspecs = extract_colspecs(input)?;
    let mut table = Table::new(colspecs.len());
    table.colspecs = colspecs;

    // Extract caption if present
    if let Some(caption) = extract_caption(input) {
        table.caption = Some(caption);
    }

    // Extract label if present
    if let Some(label) = extract_label(input) {
        table.label = Some(label);
    }

    // Parse rows
    let content = extract_table_content(input);
    let rows = parse_rows(&content, table.num_cols());

    // Determine header vs body (first row after hline is typically header)
    let mut in_header = true;
    for row in rows {
        if in_header && (row.has_bottom_border || table.header.is_empty()) {
            table.push_header(row);
            in_header = false;
        } else {
            table.push_body(row);
        }
    }

    Some(table)
}

/// Extract column specifications from LaTeX
fn extract_colspecs(input: &str) -> Option<Vec<ColSpec>> {
    // Find the alignment string {|c|c|c|} or similar
    let begin_pattern = if input.contains("\\begin{tabularx}") {
        "\\begin{tabularx}"
    } else if input.contains("\\begin{longtable}") {
        "\\begin{longtable}"
    } else {
        "\\begin{tabular}"
    };

    let start = input.find(begin_pattern)? + begin_pattern.len();

    // Skip width argument for tabularx
    let rest = &input[start..];
    let rest = if begin_pattern == "\\begin{tabularx}" {
        // Skip {width}
        skip_braced_arg(rest)
    } else {
        rest.trim_start()
    };

    // Find the column spec in braces
    if !rest.starts_with('{') {
        return None;
    }

    let end = find_matching_brace(rest)?;
    let spec_str = &rest[1..end];

    let mut colspecs: Vec<ColSpec> = Vec::new();
    let mut has_left_border = false;
    let mut chars = spec_str.chars().peekable();

    while let Some(c) = chars.next() {
        match c {
            '|' => {
                if colspecs.is_empty() {
                    has_left_border = true;
                } else if let Some(last) = colspecs.last_mut() {
                    last.has_right_border = true;
                }
            }
            'l' | 'c' | 'r' => {
                let spec = ColSpec {
                    alignment: Alignment::from_latex_char(c),
                    has_left_border,
                    ..Default::default()
                };
                has_left_border = false;
                colspecs.push(spec);
            }
            'p' | 'm' | 'b' => {
                // Skip width specification
                if chars.peek() == Some(&'{') {
                    skip_braced_content(&mut chars);
                }
                let spec = ColSpec {
                    alignment: Alignment::Left,
                    has_left_border,
                    ..Default::default()
                };
                has_left_border = false;
                colspecs.push(spec);
            }
            'X' => {
                // tabularx X column
                let spec = ColSpec {
                    alignment: Alignment::Left,
                    width: ColWidth::Fixed(1.0), // Equal distribution
                    has_left_border,
                    ..Default::default()
                };
                has_left_border = false;
                colspecs.push(spec);
            }
            '*' => {
                // *{n}{spec} - repeat specification
                if chars.peek() == Some(&'{') {
                    if let Some(count) = extract_repeat_count(&mut chars) {
                        if let Some(repeat_spec) = extract_repeat_spec(&mut chars) {
                            for _ in 0..count {
                                // Parse the repeated spec
                                for rc in repeat_spec.chars() {
                                    if rc == '|' {
                                        if colspecs.is_empty() {
                                            has_left_border = true;
                                        } else if let Some(last) = colspecs.last_mut() {
                                            last.has_right_border = true;
                                        }
                                    } else if "lcr".contains(rc) {
                                        let spec = ColSpec {
                                            alignment: Alignment::from_latex_char(rc),
                                            has_left_border,
                                            ..Default::default()
                                        };
                                        has_left_border = false;
                                        colspecs.push(spec);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            '@' | '>' | '<' | '!' => {
                // Skip these specifications
                if chars.peek() == Some(&'{') {
                    skip_braced_content(&mut chars);
                }
            }
            _ => {}
        }
    }

    if colspecs.is_empty() {
        None
    } else {
        Some(colspecs)
    }
}

/// Skip a braced argument and return the rest
fn skip_braced_arg(s: &str) -> &str {
    let s = s.trim_start();
    if !s.starts_with('{') {
        return s;
    }

    if let Some(end) = find_matching_brace(s) {
        &s[end + 1..]
    } else {
        s
    }
}

/// Find the position of the matching closing brace
fn find_matching_brace(s: &str) -> Option<usize> {
    let mut depth = 0;
    for (i, c) in s.char_indices() {
        match c {
            '{' => depth += 1,
            '}' => {
                depth -= 1;
                if depth == 0 {
                    return Some(i);
                }
            }
            _ => {}
        }
    }
    None
}

/// Skip braced content in a char iterator
fn skip_braced_content(chars: &mut std::iter::Peekable<std::str::Chars>) {
    if chars.peek() != Some(&'{') {
        return;
    }
    chars.next(); // consume '{'
    let mut depth = 1;
    while let Some(c) = chars.next() {
        match c {
            '{' => depth += 1,
            '}' => {
                depth -= 1;
                if depth == 0 {
                    break;
                }
            }
            _ => {}
        }
    }
}

/// Extract repeat count from *{n}
fn extract_repeat_count(chars: &mut std::iter::Peekable<std::str::Chars>) -> Option<usize> {
    if chars.peek() != Some(&'{') {
        return None;
    }
    chars.next(); // consume '{'

    let mut num_str = String::new();
    while let Some(&c) = chars.peek() {
        if c == '}' {
            chars.next();
            break;
        }
        num_str.push(c);
        chars.next();
    }

    num_str.trim().parse().ok()
}

/// Extract repeat spec from *{n}{spec}
fn extract_repeat_spec(chars: &mut std::iter::Peekable<std::str::Chars>) -> Option<String> {
    if chars.peek() != Some(&'{') {
        return None;
    }
    chars.next(); // consume '{'

    let mut spec = String::new();
    let mut depth = 1;
    while let Some(c) = chars.next() {
        match c {
            '{' => {
                depth += 1;
                spec.push(c);
            }
            '}' => {
                depth -= 1;
                if depth == 0 {
                    break;
                }
                spec.push(c);
            }
            _ => spec.push(c),
        }
    }

    Some(spec)
}

/// Extract caption from LaTeX table
fn extract_caption(input: &str) -> Option<Caption> {
    if let Some(start) = input.find("\\caption") {
        let rest = &input[start + "\\caption".len()..];

        // Check for short caption
        let (short, rest) = if rest.trim_start().starts_with('[') {
            let trimmed = rest.trim_start();
            if let Some(end) = trimmed.find(']') {
                (Some(trimmed[1..end].to_string()), &trimmed[end + 1..])
            } else {
                (None, rest)
            }
        } else {
            (None, rest)
        };

        // Get long caption
        let rest = rest.trim_start();
        if rest.starts_with('{') {
            if let Some(end) = find_matching_brace(rest) {
                return Some(Caption {
                    short,
                    long: rest[1..end].to_string(),
                });
            }
        }
    }
    None
}

/// Extract label from LaTeX table
fn extract_label(input: &str) -> Option<String> {
    if let Some(start) = input.find("\\label{") {
        let rest = &input[start + "\\label{".len()..];
        if let Some(end) = rest.find('}') {
            return Some(rest[..end].to_string());
        }
    }
    None
}

/// Extract table content between begin and end
fn extract_table_content(input: &str) -> String {
    // Find content between \begin{...} and \end{...}
    let env_patterns = [
        ("\\begin{tabular}", "\\end{tabular}"),
        ("\\begin{tabularx}", "\\end{tabularx}"),
        ("\\begin{longtable}", "\\end{longtable}"),
    ];

    for (begin, end) in env_patterns {
        if let Some(begin_pos) = input.find(begin) {
            // Find the end of the begin statement (after column spec)
            let after_begin = &input[begin_pos + begin.len()..];
            // Skip optional arguments and column spec
            let after_spec = if let Some(brace_start) = after_begin.find('{') {
                let from_brace = &after_begin[brace_start..];
                if let Some(brace_end) = find_matching_brace(from_brace) {
                    &after_begin[brace_start + brace_end + 1..]
                } else {
                    after_begin
                }
            } else {
                after_begin
            };

            if let Some(end_pos) = after_spec.find(end) {
                return after_spec[..end_pos].to_string();
            }
        }
    }

    input.to_string()
}

/// Parse table rows from content
fn parse_rows(content: &str, _num_cols: usize) -> Vec<Row> {
    let mut rows: Vec<Row> = Vec::new();
    let mut current_row = Row::new();
    let mut current_cell = String::new();
    let mut in_brace = 0;

    let lines: Vec<&str> = content.lines().collect();

    for line in lines {
        let line = line.trim();

        // Skip hline/cline commands but track them
        if line.starts_with("\\hline")
            || line.starts_with("\\toprule")
            || line.starts_with("\\midrule")
            || line.starts_with("\\bottomrule")
            || line.starts_with("\\cmidrule")
        {
            if !current_row.is_empty() {
                current_row.has_bottom_border = true;
            } else if let Some(last) = rows.last_mut() {
                last.has_bottom_border = true;
            }
            continue;
        }

        // Skip endhead, endfirsthead for longtable
        if line.starts_with("\\endhead") || line.starts_with("\\endfirsthead") {
            continue;
        }

        // Parse row content
        for c in line.chars() {
            match c {
                '{' => {
                    in_brace += 1;
                    current_cell.push(c);
                }
                '}' => {
                    in_brace -= 1;
                    current_cell.push(c);
                }
                '&' if in_brace == 0 => {
                    // Cell separator
                    let cell = parse_cell(current_cell.trim());
                    current_row.push(cell);
                    current_cell.clear();
                }
                _ => {
                    current_cell.push(c);
                }
            }
        }

        // Check for row end
        if line.ends_with("\\\\") || line.contains("\\\\") {
            // Remove trailing \\
            let cell_content = current_cell
                .trim()
                .trim_end_matches("\\\\")
                .trim_end_matches('\\')
                .trim();
            if !cell_content.is_empty() || !current_row.cells.is_empty() {
                let cell = parse_cell(cell_content);
                current_row.push(cell);
            }

            if !current_row.is_empty() {
                rows.push(current_row);
            }
            current_row = Row::new();
            current_cell.clear();
        }
    }

    // Handle last row without \\
    if !current_cell.trim().is_empty() {
        let cell = parse_cell(current_cell.trim());
        current_row.push(cell);
    }
    if !current_row.is_empty() {
        rows.push(current_row);
    }

    rows
}

/// Parse a single cell, handling multicolumn/multirow
fn parse_cell(content: &str) -> Cell {
    let content = content.trim();

    // Check for \multicolumn{n}{align}{content}
    if content.starts_with("\\multicolumn{") {
        return parse_multicolumn(content);
    }

    // Check for \multirow{n}{width}{content}
    if content.starts_with("\\multirow{") {
        return parse_multirow(content);
    }

    Cell::new(content.to_string())
}

/// Parse multicolumn cell
fn parse_multicolumn(content: &str) -> Cell {
    let rest = &content["\\multicolumn{".len()..];

    // Get colspan
    let colspan = if let Some(end) = rest.find('}') {
        rest[..end].parse().unwrap_or(1)
    } else {
        return Cell::new(content.to_string());
    };

    // Skip to alignment spec
    let rest = &rest[rest.find('}').unwrap_or(0) + 1..];
    let rest = rest.trim_start();

    // Get alignment
    let alignment = if rest.starts_with('{') {
        if let Some(end) = find_matching_brace(rest) {
            let align_str = &rest[1..end];
            Some(Alignment::from_latex_char(
                align_str
                    .chars()
                    .find(|c| "lcr".contains(*c))
                    .unwrap_or('c'),
            ))
        } else {
            None
        }
    } else {
        None
    };

    // Skip to content
    let rest = if rest.starts_with('{') {
        if let Some(end) = find_matching_brace(rest) {
            &rest[end + 1..]
        } else {
            rest
        }
    } else {
        rest
    };

    // Get content
    let cell_content = if rest.trim_start().starts_with('{') {
        let trimmed = rest.trim_start();
        if let Some(end) = find_matching_brace(trimmed) {
            trimmed[1..end].to_string()
        } else {
            rest.to_string()
        }
    } else {
        rest.to_string()
    };

    let mut cell = Cell::with_span(cell_content, colspan, 1);
    cell.alignment = alignment;
    cell
}

/// Parse multirow cell
fn parse_multirow(content: &str) -> Cell {
    let rest = &content["\\multirow{".len()..];

    // Get rowspan
    let rowspan = if let Some(end) = rest.find('}') {
        rest[..end].parse().unwrap_or(1)
    } else {
        return Cell::new(content.to_string());
    };

    // Skip width argument
    let rest = &rest[rest.find('}').unwrap_or(0) + 1..];
    let rest = if rest.trim_start().starts_with('{') {
        skip_braced_arg(rest)
    } else {
        rest
    };

    // Get content
    let cell_content = if rest.trim_start().starts_with('{') {
        let trimmed = rest.trim_start();
        if let Some(end) = find_matching_brace(trimmed) {
            trimmed[1..end].to_string()
        } else {
            rest.to_string()
        }
    } else {
        rest.to_string()
    };

    Cell::with_span(cell_content, 1, rowspan)
}

// ============================================================================
// Typst Table Parsing
// ============================================================================

/// Parse Typst table
pub fn parse_typst_table(input: &str) -> Option<Table> {
    if !input.contains("table(") && !input.contains("#table(") {
        return None;
    }

    // This is a simplified parser for common Typst table patterns
    // Full parsing would require proper Typst syntax parsing

    // Try to extract columns count (default to 2)
    let num_cols = extract_typst_columns(input).unwrap_or(2);

    let mut table = Table::new(num_cols);

    // Extract cell contents
    if let Some(cells) = extract_typst_cells(input) {
        let mut current_row = Row::new();
        for (i, cell_content) in cells.iter().enumerate() {
            current_row.push(Cell::new(cell_content.clone()));

            if (i + 1) % num_cols == 0 {
                table.push_body(current_row);
                current_row = Row::new();
            }
        }

        if !current_row.is_empty() {
            table.push_body(current_row);
        }
    }

    Some(table)
}

/// Extract number of columns from Typst table
fn extract_typst_columns(input: &str) -> Option<usize> {
    // Look for columns: n or columns: (...)
    if let Some(start) = input.find("columns:") {
        let rest = &input[start + "columns:".len()..];
        let rest = rest.trim_start();

        // Check for simple number
        if let Some(c) = rest.chars().next() {
            if c.is_ascii_digit() {
                let num_str: String = rest.chars().take_while(|c| c.is_ascii_digit()).collect();
                return num_str.parse().ok();
            }

            // Check for tuple (auto, auto, ...)
            if c == '(' {
                let mut count = 0;
                let mut depth = 0;
                for c in rest.chars() {
                    match c {
                        '(' => depth += 1,
                        ')' => {
                            depth -= 1;
                            if depth == 0 {
                                count += 1;
                                break;
                            }
                        }
                        ',' if depth == 1 => count += 1,
                        _ => {}
                    }
                }
                return Some(count);
            }
        }
    }

    None
}

/// Extract cell contents from Typst table
fn extract_typst_cells(input: &str) -> Option<Vec<String>> {
    // Find the table content
    let start = input.find("table(")? + "table(".len();
    let rest = &input[start..];

    // Find matching closing paren
    let mut depth = 1;
    let mut end = 0;
    for (i, c) in rest.char_indices() {
        match c {
            '(' | '[' | '{' => depth += 1,
            ')' | ']' | '}' => {
                depth -= 1;
                if depth == 0 {
                    end = i;
                    break;
                }
            }
            _ => {}
        }
    }

    let content = &rest[..end];

    // Extract bracketed content [...]
    let mut cells = Vec::new();
    let mut i = 0;
    let chars: Vec<char> = content.chars().collect();

    while i < chars.len() {
        if chars[i] == '[' {
            let mut cell = String::new();
            let mut bracket_depth = 1;
            i += 1;

            while i < chars.len() && bracket_depth > 0 {
                if chars[i] == '[' {
                    bracket_depth += 1;
                } else if chars[i] == ']' {
                    bracket_depth -= 1;
                    if bracket_depth == 0 {
                        break;
                    }
                }
                cell.push(chars[i]);
                i += 1;
            }

            cells.push(cell.trim().to_string());
        }
        i += 1;
    }

    if cells.is_empty() {
        None
    } else {
        Some(cells)
    }
}

// ============================================================================
// Table to LaTeX Conversion
// ============================================================================

/// Convert Table to LaTeX
pub fn table_to_latex(table: &Table) -> String {
    let mut output = String::new();

    // Begin table environment if we have caption
    if table.caption.is_some() {
        output.push_str("\\begin{table}[htbp]\n");
        output.push_str("\\centering\n");
    }

    // Build column spec
    let col_spec: String = table
        .colspecs
        .iter()
        .map(|spec| {
            let mut s = String::new();
            if spec.has_left_border {
                s.push('|');
            }
            s.push(spec.alignment.to_latex_char());
            if spec.has_right_border {
                s.push('|');
            }
            s
        })
        .collect();

    output.push_str(&format!("\\begin{{tabular}}{{{}}}\n", col_spec));

    if table.has_top_border {
        output.push_str("\\hline\n");
    }

    // Header rows
    for row in &table.header {
        output.push_str(&row_to_latex(row));
        if row.has_bottom_border {
            output.push_str("\\hline\n");
        }
    }

    // Body rows
    for row in &table.body {
        output.push_str(&row_to_latex(row));
        if row.has_bottom_border {
            output.push_str("\\hline\n");
        }
    }

    // Footer rows
    for row in &table.footer {
        output.push_str(&row_to_latex(row));
        if row.has_bottom_border {
            output.push_str("\\hline\n");
        }
    }

    output.push_str("\\end{tabular}\n");

    // Caption and label
    if let Some(ref caption) = table.caption {
        if let Some(ref short) = caption.short {
            output.push_str(&format!("\\caption[{}]{{{}}}\n", short, caption.long));
        } else {
            output.push_str(&format!("\\caption{{{}}}\n", caption.long));
        }
    }

    if let Some(ref label) = table.label {
        output.push_str(&format!("\\label{{{}}}\n", label));
    }

    if table.caption.is_some() {
        output.push_str("\\end{table}\n");
    }

    output
}

/// Convert a row to LaTeX
fn row_to_latex(row: &Row) -> String {
    let cells: Vec<String> = row.cells.iter().map(cell_to_latex).collect();

    format!("{} \\\\\n", cells.join(" & "))
}

/// Convert a cell to LaTeX
fn cell_to_latex(cell: &Cell) -> String {
    if cell.colspan > 1 {
        let align = cell.alignment.unwrap_or(Alignment::Center);
        format!(
            "\\multicolumn{{{}}}{{{}}}{{{}}}",
            cell.colspan,
            align.to_latex_char(),
            cell.content
        )
    } else if cell.rowspan > 1 {
        format!("\\multirow{{{}}}{{*}}{{{}}}", cell.rowspan, cell.content)
    } else {
        cell.content.clone()
    }
}

// ============================================================================
// Table to Typst Conversion
// ============================================================================

/// Convert Table to Typst
pub fn table_to_typst(table: &Table) -> String {
    let mut output = String::new();

    // Figure wrapper if we have caption
    if table.caption.is_some() {
        output.push_str("#figure(\n");
    }

    // Table
    output.push_str("  table(\n");

    // Columns
    let _cols = table.num_cols();
    let widths: Vec<String> = table
        .colspecs
        .iter()
        .map(|spec| match spec.width {
            ColWidth::Auto => "auto".to_string(),
            ColWidth::Fixed(w) => format!("{}%", w * 100.0),
            ColWidth::Percent(p) => format!("{}%", p),
        })
        .collect();

    output.push_str(&format!("    columns: ({}),\n", widths.join(", ")));

    // Alignment
    let aligns: Vec<&str> = table
        .colspecs
        .iter()
        .map(|spec| spec.alignment.to_typst())
        .collect();
    output.push_str(&format!("    align: ({}),\n", aligns.join(", ")));

    // Stroke for borders
    if table.has_top_border
        || table
            .colspecs
            .iter()
            .any(|s| s.has_left_border || s.has_right_border)
    {
        output.push_str("    stroke: 0.5pt,\n");
    }

    // Header rows
    if !table.header.is_empty() {
        output.push_str("    table.header(\n");
        for row in &table.header {
            output.push_str(&row_to_typst(row));
        }
        output.push_str("    ),\n");
    }

    // Body rows
    for row in &table.body {
        output.push_str(&row_to_typst(row));
    }

    // Footer rows
    if !table.footer.is_empty() {
        output.push_str("    table.footer(\n");
        for row in &table.footer {
            output.push_str(&row_to_typst(row));
        }
        output.push_str("    ),\n");
    }

    output.push_str("  )");

    // Caption
    if let Some(ref caption) = table.caption {
        output.push_str(",\n");
        output.push_str(&format!("  caption: [{}]\n", caption.long));
        output.push(')');

        // Label
        if let Some(ref label) = table.label {
            output.push_str(&format!(" <{}>", label));
        }
    }

    output.push('\n');
    output
}

/// Convert a row to Typst
fn row_to_typst(row: &Row) -> String {
    let cells: Vec<String> = row.cells.iter().map(cell_to_typst).collect();

    format!("      {},\n", cells.join(", "))
}

/// Convert a cell to Typst
fn cell_to_typst(cell: &Cell) -> String {
    if cell.colspan > 1 || cell.rowspan > 1 {
        let mut args = Vec::new();
        if cell.colspan > 1 {
            args.push(format!("colspan: {}", cell.colspan));
        }
        if cell.rowspan > 1 {
            args.push(format!("rowspan: {}", cell.rowspan));
        }
        format!("table.cell({})[{}]", args.join(", "), cell.content)
    } else {
        format!("[{}]", cell.content)
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_parse_simple_table() {
        let input = r#"
\begin{tabular}{|c|c|c|}
\hline
A & B & C \\
\hline
1 & 2 & 3 \\
4 & 5 & 6 \\
\hline
\end{tabular}
"#;
        let table = parse_latex_table(input).unwrap();
        assert_eq!(table.num_cols(), 3);
        assert!(!table.header.is_empty());
    }

    #[test]
    fn test_colspec_parsing() {
        let specs = extract_colspecs("\\begin{tabular}{|l|c|r|}").unwrap();
        assert_eq!(specs.len(), 3);
        assert_eq!(specs[0].alignment, Alignment::Left);
        assert_eq!(specs[1].alignment, Alignment::Center);
        assert_eq!(specs[2].alignment, Alignment::Right);
    }

    #[test]
    fn test_multicolumn() {
        let cell = parse_multicolumn("\\multicolumn{3}{c}{Merged}");
        assert_eq!(cell.colspan, 3);
        assert_eq!(cell.alignment, Some(Alignment::Center));
    }

    #[test]
    fn test_table_to_latex() {
        let mut table = Table::new(2);
        table.colspecs[0].alignment = Alignment::Left;
        table.colspecs[1].alignment = Alignment::Right;

        let mut row = Row::new();
        row.push(Cell::new("A".to_string()));
        row.push(Cell::new("B".to_string()));
        table.push_body(row);

        let latex = table_to_latex(&table);
        assert!(latex.contains("\\begin{tabular}{lr}"));
        assert!(latex.contains("A & B"));
    }

    #[test]
    fn test_table_to_typst() {
        let mut table = Table::new(2);

        let mut row = Row::new();
        row.push(Cell::new("A".to_string()));
        row.push(Cell::new("B".to_string()));
        table.push_body(row);

        let typst = table_to_typst(&table);
        assert!(typst.contains("table("));
        assert!(typst.contains("columns:"));
        assert!(typst.contains("[A]"));
    }

    #[test]
    fn test_caption_extraction() {
        let input = r#"\caption[Short]{Long caption}"#;
        let caption = extract_caption(input).unwrap();
        assert_eq!(caption.short, Some("Short".to_string()));
        assert_eq!(caption.long, "Long caption");
    }

    #[test]
    fn test_colspan_cell() {
        let cell = Cell::with_span("Merged".to_string(), 3, 1);
        let latex = cell_to_latex(&cell);
        assert!(latex.contains("\\multicolumn{3}"));

        let typst = cell_to_typst(&cell);
        assert!(typst.contains("colspan: 3"));
    }

    #[test]
    fn test_rowspan_cell() {
        let cell = Cell::with_span("Vertical".to_string(), 1, 2);
        let latex = cell_to_latex(&cell);
        assert!(latex.contains("\\multirow{2}"));

        let typst = cell_to_typst(&cell);
        assert!(typst.contains("rowspan: 2"));
    }
}