diff --git a/crates/typst/src/math/lr.rs b/crates/typst/src/math/lr.rs index 80ce55eb3..ccaf2959b 100644 --- a/crates/typst/src/math/lr.rs +++ b/crates/typst/src/math/lr.rs @@ -3,7 +3,9 @@ use unicode_math_class::MathClass; use crate::diag::SourceResult; use crate::foundations::{elem, func, Content, NativeElement, Packed, Smart, StyleChain}; use crate::layout::{Abs, Axis, Em, Length, Rel}; -use crate::math::{stretch_fragment, LayoutMath, MathContext, MathFragment, Scaled}; +use crate::math::{ + stretch_fragment, EquationElem, LayoutMath, MathContext, MathFragment, Scaled, +}; use crate::text::TextElem; /// How much less high scaled delimiters can be than what they wrap. @@ -21,13 +23,9 @@ pub struct LrElem { /// The delimited content, including the delimiters. #[required] #[parse( - let mut body = Content::empty(); - for (i, arg) in args.all::()?.into_iter().enumerate() { - if i > 0 { - body += TextElem::packed(','); - } - body += arg; - } + let mut arguments = args.all::()?.into_iter(); + let mut body = arguments.next().unwrap_or_default(); + arguments.for_each(|arg| body += TextElem::packed(',') + arg); body )] pub body: Content, @@ -37,9 +35,16 @@ impl LayoutMath for Packed { #[typst_macros::time(name = "math.lr", span = self.span())] fn layout_math(&self, ctx: &mut MathContext, styles: StyleChain) -> SourceResult<()> { let mut body = self.body(); - if let Some(elem) = body.to_packed::() { - if elem.size(styles).is_auto() { - body = elem.body(); + + // Extract from an EquationElem. + if let Some(equation) = body.to_packed::() { + body = equation.body(); + } + + // Extract implicit LrElem. + if let Some(lr) = body.to_packed::() { + if lr.size(styles).is_auto() { + body = lr.body(); } } diff --git a/tests/ref/math-lr-mid-size-nested-equation.png b/tests/ref/math-lr-mid-size-nested-equation.png new file mode 100644 index 000000000..df0106689 Binary files /dev/null and b/tests/ref/math-lr-mid-size-nested-equation.png differ diff --git a/tests/ref/math-lr-mid-size.png b/tests/ref/math-lr-mid-size.png new file mode 100644 index 000000000..12b4c0868 Binary files /dev/null and b/tests/ref/math-lr-mid-size.png differ diff --git a/tests/ref/math-lr-nested.png b/tests/ref/math-lr-nested.png new file mode 100644 index 000000000..eb172009e Binary files /dev/null and b/tests/ref/math-lr-nested.png differ diff --git a/tests/suite/math/delimited.typ b/tests/suite/math/delimited.typ index 632fbd407..d6e1d8648 100644 --- a/tests/suite/math/delimited.typ +++ b/tests/suite/math/delimited.typ @@ -52,6 +52,31 @@ $ { x mid(|) sum_(i=1)^oo phi_i (x) < 1 } \ mid(bar.v.double) floor(hat(I) mid(slash) { dot mid(|) dot } mid(|) I/n) } $ +--- math-lr-mid-size --- +// Test mid when lr size is set. +#set page(width: auto) + +$ lr({ A mid(|) integral }) quad + lr(size: #1em, { A mid(|) integral }) quad + lr(size: #(1em+20%), { A mid(|) integral }) \ + + lr(] A mid(|) integral ]) quad + lr(size: #1em, ] A mid(|) integral ]) quad + lr(size: #(1em+20%), ] A mid(|) integral ]) \ + + lr(( A mid(|) integral ]) quad + lr(size: #1em, ( A mid(|) integral ]) quad + lr(size: #(1em+20%), ( A mid(|) integral ]) $ + +--- math-lr-mid-size-nested-equation --- +// Test mid size when lr size is set, when nested in an equation. +#set page(width: auto) + +#let body = ${ A mid(|) integral }$ +$ lr(body) quad + lr(size: #1em, body) quad + lr(size: #(1em+20%), body) $ + --- math-lr-unbalanced --- // Test unbalanced delimiters. $ 1/(2 (x) $ @@ -63,6 +88,16 @@ $ 1/(2 y (x) (2(3)) $ // and immediately before the closing. $ [#h(1em, weak: true)A(dif x, f(x) dif x)sum#h(1em, weak: true)] $ +--- math-lr-nested --- +// Test nested lr calls. +#let body1 = math.lr($|$, size: 4em) +#let body2 = $lr(|, size: #4em)$ + +$lr(|, size: #2em)$ +$lr(lr(|, size: #4em), size: #50%)$ +$lr(body1, size: #50%)$ +$lr(body2, size: #50%)$ + --- issue-4188-lr-corner-brackets --- // Test positioning of U+231C to U+231F $⌜a⌟⌞b⌝$ = $⌜$$a$$⌟$$⌞$$b$$⌝$