From f18fc5187f87a933796e94a5403a602a5ef2044f Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 13:30:56 +0800 Subject: [PATCH] Parse substack into stacked math rows --- crates/texrra-tex-core/src/math.rs | 29 +++++++++++++++++++++++++++++ docs/TEX_CORE.md | 2 ++ 2 files changed, 31 insertions(+) diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index d23dc76..6bb6bf6 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -59,6 +59,7 @@ pub enum MathAtom { delimiter: char, size: u8, }, + StackLines(Vec), Stack { top: Box, base: Box, @@ -314,6 +315,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox { depth: 2 * 65_536, } } + MathAtom::StackLines(rows) => { + let boxes: Vec = rows.iter().map(|atom| layout_math(atom, font)).collect(); + MathBox { + width: boxes.iter().map(|b| b.width).max().unwrap_or(0), + height: boxes.iter().map(|b| b.height).sum(), + depth: boxes.last().map_or(0, |b| b.depth), + } + } MathAtom::Stack { top, base, above } => { let top = layout_math(top, font); let base = layout_math(base, font); @@ -678,6 +687,19 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) { (after + 1).min(chars.len()), ) } + "substack" => { + let mut after = next; + if chars.get(after) == Some(&'{') { + after += 1; + } + let start = after; + while after < chars.len() && chars[after] != '}' { + after += 1; + } + let content: String = chars[start..after].iter().collect(); + let rows = content.split("\\\\").map(parse_math).collect(); + (MathAtom::StackLines(rows), (after + 1).min(chars.len())) + } "overset" | "underset" | "stackrel" => { let (top, after) = parse_atom(chars, next); let (base, after) = parse_atom(chars, after); @@ -1036,6 +1058,13 @@ mod tests { assert!(tall.width > plain.width); } + #[test] + fn substack_stacks_multiple_rows() { + let font = Cmr10Metrics::default(); + let plain = layout_math(&parse_math("a"), &font); + let stacked = layout_math(&parse_math(r"\substack{a\\b}"), &font); + assert!(stacked.height > plain.height); + } #[test] fn overbrace_accepts_a_superscript_label() { let font = Cmr10Metrics::default(); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 9a76cda..a160808 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -233,3 +233,5 @@ for a later overlaid slash. | M7 incremental integration | partial | edit replay + page hashes, not wired into v1 preview | v1 remains the production renderer; the route-D core is additive and tested independently. + +`\substack{a\\b}` parses each row and stacks them as `MathAtom::StackLines`.