From e05caa351b6e5568ce3179bbee90a507d292ceb8 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 19:03:07 +0800 Subject: [PATCH] Add explicit math spacing commands --- crates/texrra-tex-core/src/math.rs | 37 ++++++++++++++++++++++++++++ crates/texrra-tex-core/src/render.rs | 1 + 2 files changed, 38 insertions(+) diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index 2a63471..deca5c0 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -9,6 +9,7 @@ use crate::cmr10::Cmr10Metrics; #[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] pub enum MathAtom { Symbol(char), + Space(i64), Row(Vec), Sqrt { radicand: Box, @@ -190,6 +191,11 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox { height: 7 * 65_536, depth: 2 * 65_536, }, + MathAtom::Space(width) => MathBox { + width: *width, + height: 0, + depth: 0, + }, MathAtom::Row(atoms) => { let mut width = 0; let mut height = 0; @@ -556,7 +562,25 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) { break; } } + if command.is_empty() { + let width = match chars.get(next) { + Some(',') => 109_227, + Some(':') | Some('>') => 218_454, + Some(';') => 327_680, + Some('!') => -109_227, + Some(' ') => 0, + _ => 0, + }; + if matches!( + chars.get(next), + Some(',') | Some(':') | Some('>') | Some(';') | Some('!') + ) { + return (MathAtom::Space(width), next + 1); + } + } return match command.as_str() { + "quad" => (MathAtom::Space(655_360), next), + "qquad" => (MathAtom::Space(1_310_720), next), "displaystyle" | "textstyle" | "scriptstyle" | "scriptscriptstyle" => { let mut after = next; while chars.get(after).is_some_and(|ch| ch.is_whitespace()) { @@ -1223,6 +1247,19 @@ mod tests { assert_eq!(under.height, plain.height); } + #[test] + fn math_spacing_commands_have_expected_widths() { + let font = Cmr10Metrics::default(); + let thin = layout_math(&parse_math(r"a\,b"), &font); + let medium = layout_math(&parse_math(r"a\;b"), &font); + let quad = layout_math(&parse_math(r"a\quad b"), &font); + let double = layout_math(&parse_math(r"a\qquad b"), &font); + let plain = layout_math(&parse_math("ab"), &font); + assert!(thin.width > plain.width); + assert!(medium.width > thin.width); + assert!(quad.width > medium.width); + assert!(double.width > quad.width); + } #[test] fn math_text_preserves_internal_spaces() { let font = Cmr10Metrics::default(); diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index 3296fa0..fc94942 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -487,6 +487,7 @@ impl Page { list: &mut Vec, ) { match atom { + MathAtom::Space(_) => {} MathAtom::Symbol(character) => { if !character.is_whitespace() { list.push(DisplayItem::Text {