Parse named math operators as upright text

This commit is contained in:
gloamxun committed 2026-10-20 13:01:07 +08:00
1 parent b11fc864f2
commit 639f5e8f8b
2 files changed
+17

No files matched your search

+15
View File
@@ -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();
+2
View File
@@ -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{...}`;