diff options
| author | Sébastien d'Herbais de Thun <sebastien.d.herbais@gmail.com> | 2023-04-04 19:21:25 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-04 19:21:25 +0200 |
| commit | f347ed4314e32383dc09ff234180e8ea6fef7b8b (patch) | |
| tree | dba4296812a131e52da4eb0079fd0d240860d832 /library/src/math/mod.rs | |
| parent | 5b0297464efc131beb7be84fa7a02b9a8670b5dd (diff) | |
Improved figure numbering, labelling and referencing (#491)
Diffstat (limited to 'library/src/math/mod.rs')
| -rw-r--r-- | library/src/math/mod.rs | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/library/src/math/mod.rs b/library/src/math/mod.rs index 82ad0a2f..b07fc78f 100644 --- a/library/src/math/mod.rs +++ b/library/src/math/mod.rs @@ -39,6 +39,7 @@ use self::fragment::*; use self::row::*; use self::spacing::*; use crate::layout::{HElem, ParElem, Spacing}; +use crate::meta::Refable; use crate::meta::{Count, Counter, CounterUpdate, LocalName, Numbering}; use crate::prelude::*; use crate::text::{ @@ -134,7 +135,9 @@ pub fn module() -> Module { /// /// Display: Equation /// Category: math -#[element(Locatable, Synthesize, Show, Finalize, Layout, LayoutMath, Count, LocalName)] +#[element( + Locatable, Synthesize, Show, Finalize, Layout, LayoutMath, Count, LocalName, Refable +)] pub struct EquationElem { /// Whether the equation is displayed as a separate block. #[default(false)] @@ -159,9 +162,11 @@ pub struct EquationElem { } impl Synthesize for EquationElem { - fn synthesize(&mut self, styles: StyleChain) { + fn synthesize(&mut self, _vt: &mut Vt, styles: StyleChain) -> SourceResult<()> { self.push_block(self.block(styles)); self.push_numbering(self.numbering(styles)); + + Ok(()) } } @@ -278,6 +283,45 @@ impl LocalName for EquationElem { } } +impl Refable for EquationElem { + fn reference( + &self, + vt: &mut Vt, + styles: StyleChain, + supplement: Option<Content>, + ) -> SourceResult<Content> { + // first we create the supplement of the heading + let mut supplement = supplement.unwrap_or_else(|| { + TextElem::packed(self.local_name(TextElem::lang_in(styles))) + }); + + // we append a space if the supplement is not empty + if !supplement.is_empty() { + supplement += TextElem::packed('\u{a0}') + }; + + // we check for a numbering + let Some(numbering) = self.numbering(styles) else { + bail!(self.span(), "only numbered equations can be referenced"); + }; + + // we get the counter and display it + let numbers = Counter::of(Self::func()) + .at(vt, self.0.location().expect("missing location"))? + .display(vt, &numbering.trimmed())?; + + Ok(supplement + numbers) + } + + fn numbering(&self, styles: StyleChain) -> Option<Numbering> { + self.numbering(styles) + } + + fn counter(&self, _styles: StyleChain) -> Counter { + Counter::of(Self::func()) + } +} + pub trait LayoutMath { fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()>; } |
