From 639f5e8f8b8baa03fe1ff45dc0c0a6fcc085cdb9 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 13:01:07 +0800 Subject: [PATCH] Parse named math operators as upright text --- crates/texrra-tex-core/src/math.rs | 15 +++++++++++++++ docs/TEX_CORE.md | 2 ++ 2 files changed, 17 insertions(+) diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index 05dcf53..27ce9bf 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -421,6 +421,10 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) { let delimiter = chars.get(next).copied().unwrap_or('|'); (MathAtom::Symbol(delimiter), next + 1) } + "max" | "min" | "lim" | "log" | "ln" | "sin" | "cos" | "tan" | "cot" | "sec" + | "csc" | "exp" | "det" | "dim" | "ker" | "hom" | "arg" | "deg" | "gcd" => { + (MathAtom::Text(command.clone()), next) + } "text" | "mathrm" => { let mut after = next; if chars.get(after) == Some(&'{') { @@ -527,6 +531,17 @@ mod tests { assert!(indexed.height > plain.height); } + #[test] + fn named_operators_use_upright_text_widths() { + let font = Cmr10Metrics::default(); + let max = layout_math(&parse_math("\\max"), &font); + let single = layout_math(&parse_math("m"), &font); + assert!(max.width > single.width); + assert!(matches!( + &parse_math("\\max"), + MathAtom::Row(atoms) if matches!(&atoms[0], MathAtom::Text(text) if text == "max") + )); + } #[test] fn underline_adds_depth_without_changing_width() { let font = Cmr10Metrics::default(); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index a4ff8d0..53c96a4 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -110,6 +110,8 @@ a single cost function. - `MathAtom::Accent { accent, base }` parses `\hat`, `\widehat`, `\tilde`, `\widetilde`, `\bar`, `\overline` and `\vec`; it adds accent height without changing the base width. +- Named operators (`\max`, `\min`, `\sin`, `\log`, `\det`, ...) parse as + upright `Text` atoms. - `MathAtom::Underline { base }` parses `\underline{...}` and adds 2pt depth without changing width or height. - `MathAtom::Text(String)` parses `\text{...}` and `\mathrm{...}`;