Support xrightarrow and xleftarrow
The amsmath extensible arrows were unknown, so their labels fell back to plain text. The math parser now reads \xrightarrow/\xleftarrow, an optional [below] label, and the above label, and builds nested Stack atoms around the arrow symbol: above stacks upward, an optional below label stacks downward. The math test compares the bare arrow with the labeled form (width/height grow) and with the two-label form (total height plus depth grows).
This commit is contained in:
1 parent
8307fc8918
commit
4068366190
4 files changed
+54
-6
No files matched your search
@@ -2313,6 +2313,43 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
||||
let rows = content.split("\\\\").map(parse_math).collect();
|
||||
(MathAtom::StackLines(rows), (after + 1).min(chars.len()))
|
||||
}
|
||||
"xrightarrow" | "xleftarrow" => {
|
||||
let mut after = next;
|
||||
let mut below = None;
|
||||
if chars.get(after) == Some(&'[') {
|
||||
let start = after + 1;
|
||||
let mut end = start;
|
||||
while end < chars.len() && chars[end] != ']' {
|
||||
end += 1;
|
||||
}
|
||||
if end < chars.len() {
|
||||
below = Some(parse_math(&chars[start..end].iter().collect::<String>()));
|
||||
after = end + 1;
|
||||
}
|
||||
}
|
||||
let (above, after) = parse_atom(chars, after);
|
||||
let arrow = MathAtom::Symbol(if command == "xrightarrow" {
|
||||
'\u{2192}'
|
||||
} else {
|
||||
'\u{2190}'
|
||||
});
|
||||
let base = match below {
|
||||
Some(below) => MathAtom::Stack {
|
||||
top: Box::new(below),
|
||||
base: Box::new(arrow),
|
||||
above: false,
|
||||
},
|
||||
None => arrow,
|
||||
};
|
||||
(
|
||||
MathAtom::Stack {
|
||||
top: Box::new(above),
|
||||
base: Box::new(base),
|
||||
above: true,
|
||||
},
|
||||
after,
|
||||
)
|
||||
}
|
||||
"overset" | "underset" | "stackrel" => {
|
||||
let (top, after) = parse_atom(chars, next);
|
||||
let (base, after) = parse_atom(chars, after);
|
||||
@@ -3469,6 +3506,17 @@ mod tests {
|
||||
assert_eq!(plain.width, styled.width);
|
||||
assert_eq!(plain.height, styled.height);
|
||||
}
|
||||
#[test]
|
||||
fn xrightarrow_stacks_its_label() {
|
||||
let font = Cmr10Metrics::default();
|
||||
let arrow = layout_math(&parse_math("\\rightarrow"), &font);
|
||||
let labeled = layout_math(&parse_math("\\xrightarrow{abc}"), &font);
|
||||
assert!(labeled.width >= arrow.width);
|
||||
assert!(labeled.height > arrow.height);
|
||||
let below = layout_math(&parse_math("\\xrightarrow[below]{above}"), &font);
|
||||
assert!(below.height + below.depth > labeled.height + labeled.depth);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boldsymbol_maps_to_the_bold_alphabet() {
|
||||
let font = Cmr10Metrics::default();
|
||||
|
||||
Reference in new issue
Block a user