Set operator names upright and declare new ones

This commit is contained in:
gloamxun committed 2026-10-26 01:24:46 +08:00
1 parent a4c4864d2b
commit 7ba66392ea
6 files changed
+120 -32

No files matched your search

+61 -27
View File
@@ -120,6 +120,11 @@ pub enum MathAtom {
centered: bool,
},
StackLines(Vec<MathAtom>),
/// An `\operatorname` name, with optional default limits.
OperatorName {
name: String,
limits: bool,
},
Stack {
top: Box<MathAtom>,
base: Box<MathAtom>,
@@ -527,6 +532,7 @@ fn atom_class(atom: &MathAtom) -> MathClass {
MathAtom::Negated { base } => atom_class(base),
MathAtom::Cancel { base, .. } => atom_class(base),
MathAtom::BigOperator { .. } => MathClass::Op,
MathAtom::OperatorName { .. } => MathClass::Op,
MathAtom::Limits { base, .. } => atom_class(base),
MathAtom::Fraction { .. } | MathAtom::Binomial { .. } | MathAtom::Delimited { .. } => {
MathClass::Inner
@@ -1183,6 +1189,26 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: layout.depth,
}
}
MathAtom::OperatorName { name, .. } => {
let roman = Cmr10Metrics::Roman;
let mut width = 0;
let mut height = 0;
let mut depth = 0;
for character in name.chars() {
if character == ' ' {
width += roman.space();
continue;
}
width += roman.width(character);
height = height.max(roman.height(character));
depth = depth.max(roman.depth(character));
}
MathBox {
width: width.max(1),
height: height.max(8 * 65_536),
depth,
}
}
MathAtom::Stack { top, base, above } => {
let top = layout_math(top, font);
let base = layout_math(base, font);
@@ -1436,6 +1462,7 @@ fn limits_override(chars: &[char], index: usize) -> Option<(bool, usize)> {
fn operator_default_limits(base: &MathAtom) -> bool {
match base {
MathAtom::BigOperator { name } => name != "int",
MathAtom::OperatorName { limits, .. } => *limits,
_ => true,
}
}
@@ -2146,6 +2173,28 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
after,
)
}
"operatorname" => {
let mut after = next;
while chars.get(after) == Some(&'\u{0}')
|| chars.get(after).is_some_and(|c| c.is_whitespace())
{
after += 1;
}
let (limits, after) = if chars.get(after) == Some(&'*') {
(true, after + 1)
} else {
(false, after)
};
if let Some((name, after)) = read_braced_text(chars, after) {
let base = MathAtom::OperatorName {
name: name.trim().to_string(),
limits,
};
parse_limits(chars, after, base)
} else {
(MathAtom::Symbol('?'), after)
}
}
"sum" | "prod" | "int" => {
let base = MathAtom::BigOperator {
name: command.clone(),
@@ -2425,33 +2474,6 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
after,
)
}
"operatorname" => {
let mut after = next;
let starred = chars.get(after) == Some(&'*');
if starred {
after += 1;
}
if chars.get(after) == Some(&'{') {
after += 1;
}
let start = after;
while after < chars.len() && chars[after] != '}' {
after += 1;
}
let text: String = chars[start..after]
.iter()
.map(|ch| if *ch == '\u{0}' { ' ' } else { *ch })
.collect();
if after < chars.len() {
after += 1;
}
let atom = MathAtom::Text(text);
if starred {
parse_limits(chars, after, atom)
} else {
(atom, after)
}
}
"text" | "mathrm" => {
let mut after = next;
if chars.get(after) == Some(&'{') {
@@ -3182,6 +3204,18 @@ mod tests {
let limited = layout_math(&parse_math(r"\mathop{argmax}_{x}"), &font);
assert!(limited.depth > plain.depth);
}
#[test]
fn operator_names_are_upright_and_stack_when_starred() {
let font = Cmr10Metrics::default();
let upright = layout_math(&parse_math("\\operatorname{Tr}"), &font);
let italic = layout_math(&parse_math("\\mathit{Tr}"), &font);
assert_ne!(upright.width, italic.width);
let stacked = layout_math(&parse_math("\\operatorname*{arg}_{x}"), &font);
let beside = layout_math(&parse_math("\\operatorname{arg}_{x}"), &font);
assert!(stacked.width < beside.width);
assert!(stacked.height + stacked.depth > beside.height + beside.depth);
}
#[test]
fn substack_rows_stack_downward() {
let atom = parse_math(r"\substack{a \\ b}");