diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index 576d88d..4573765 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -558,12 +558,36 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) { ) } "smash" => { - let (body, after) = parse_atom(chars, next); + let mut cursor = next; + while cursor < chars.len() && chars[cursor].is_whitespace() { + cursor += 1; + } + let mut top = true; + let mut bottom = true; + if chars.get(cursor) == Some(&'[') { + let mut end = cursor + 1; + while end < chars.len() && chars[end] != ']' { + match chars[end] { + 't' => { + top = true; + bottom = false; + } + 'b' => { + top = false; + bottom = true; + } + _ => {} + } + end += 1; + } + cursor = (end + 1).min(chars.len()); + } + let (body, after) = parse_atom(chars, cursor); ( MathAtom::Smash { body: Box::new(body), - top: true, - bottom: true, + top, + bottom, }, after, ) @@ -828,6 +852,16 @@ mod tests { assert_eq!(calligraphic.height, upright.height); } + #[test] + fn smash_can_target_only_the_top_or_bottom() { + let font = Cmr10Metrics::default(); + let top = layout_math(&parse_math("\\smash[t]{x}"), &font); + let bottom = layout_math(&parse_math("\\smash[b]{x}"), &font); + assert_eq!(top.height, 0); + assert!(top.depth > 0); + assert!(bottom.height > 0); + assert_eq!(bottom.depth, 0); + } #[test] fn smash_removes_height_but_keeps_width() { let font = Cmr10Metrics::default(); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index cb96226..a5df1b4 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -113,7 +113,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. -- `MathAtom::Smash { body, top, bottom }` implements `\smash` (zero height/depth). +- `MathAtom::Smash { body, top, bottom }` implements `\smash` and + `\smash[t]`/`\smash[b]`. - `MathAtom::Phantom { body, horizontal, vertical }` implements `\phantom`, `\hphantom`, `\vphantom` dimension-only boxes. - `MathAtom::BigOperator { name }` gives `\sum`/`\prod` 12pt width and