diff --git a/crates/typst-library/src/meta/figure.rs b/crates/typst-library/src/meta/figure.rs index 4aebdda6b..bab7790a9 100644 --- a/crates/typst-library/src/meta/figure.rs +++ b/crates/typst-library/src/meta/figure.rs @@ -359,12 +359,11 @@ impl Outlinable for FigureElem { return Ok(None); } - let Some(mut caption) = - self.caption(StyleChain::default()).map(|caption| caption.body()) - else { + let Some(caption) = self.caption(StyleChain::default()) else { return Ok(None); }; + let mut realized = caption.body(); if let ( Smart::Custom(Some(Supplement::Content(mut supplement))), Some(counter), @@ -381,10 +380,12 @@ impl Outlinable for FigureElem { supplement += TextElem::packed('\u{a0}'); } - caption = supplement + numbers + TextElem::packed(": ") + caption; + let separator = caption.separator(StyleChain::default()); + + realized = supplement + numbers + separator + caption.body(); } - Ok(Some(caption)) + Ok(Some(realized)) } } @@ -444,6 +445,19 @@ pub struct FigureCaption { })] pub position: VAlign, + /// The separator which will appear between the number and body. + /// + /// ```example + /// #set figure.caption(separator: [ --- ]) + /// + /// #figure( + /// rect[Hello], + /// caption: [A rectangle], + /// ) + /// ``` + #[default(TextElem::packed(": "))] + pub separator: Content, + /// The caption's body. /// /// Can be used alongside `kind`, `supplement`, `counter`, `numbering`, and @@ -487,13 +501,14 @@ pub struct FigureCaption { impl Synthesize for FigureCaption { fn synthesize(&mut self, _: &mut Vt, styles: StyleChain) -> SourceResult<()> { self.push_position(self.position(styles)); + self.push_separator(self.separator(styles)); Ok(()) } } impl Show for FigureCaption { #[tracing::instrument(name = "FigureCaption::show", skip_all)] - fn show(&self, vt: &mut Vt, _: StyleChain) -> SourceResult { + fn show(&self, vt: &mut Vt, styles: StyleChain) -> SourceResult { let mut realized = self.body(); if let (Some(mut supplement), Some(numbering), Some(counter), Some(location)) = @@ -503,7 +518,7 @@ impl Show for FigureCaption { if !supplement.is_empty() { supplement += TextElem::packed('\u{a0}'); } - realized = supplement + numbers + TextElem::packed(": ") + realized; + realized = supplement + numbers + self.separator(styles) + realized; } Ok(realized) diff --git a/tests/ref/meta/figure.png b/tests/ref/meta/figure.png index 90f59d147..373866988 100644 Binary files a/tests/ref/meta/figure.png and b/tests/ref/meta/figure.png differ diff --git a/tests/typ/meta/figure.typ b/tests/typ/meta/figure.typ index 62d163a93..7d618d062 100644 --- a/tests/typ/meta/figure.typ +++ b/tests/typ/meta/figure.typ @@ -100,3 +100,12 @@ We can clearly see that @fig-cylinder and #show figure: set block(breakable: true) #figure(table[a][b][c][d][e], caption: [A table]) + +--- +// Test custom separator for figure caption +#set figure.caption(separator: [ --- ]) + +#figure( + table(columns: 2)[a][b], + caption: [The table with custom separator.], +)