Skip to content

#context page.margin can't get margin from an ad-hoc #page #3671

Closed

Description

@PgBiel

Description

Not sure if this is really a bug, but seems to be at least a shortcoming or inconsistency of some sort, so I didn't want this to pass by unnoticed. Some packages, such as hydra, depend on knowing the page margin for certain calculations, and they can't switch to fetching the page margin through context unless it's fully reliable.

Basically, when using #set page(margin: 5pt), #context page.margin will properly report 5pt.

However, using

// initial value, for example
#set page(margin: 5pt)

#page(margin: 10pt)[#context page.margin]

reports 5pt, when that is not the current page margin.

Of course, it makes sense in a way, if we consider how things work in general. But I think it'd be valid for #page(...) to override the stylechain similarly to how #text(...) and #par(...) do it, i.e. the following example works properly:

// Correctly reports 0.65em / default value / current value
#context par.leading

// Correctly reports 5em
#par(leading: 5em)[#context par.leading]

As a solution, we could try to override the stylechain within #page[...] calls with the updated fields. Not sure of which implications that might have though.

Reproduction URL

No response

Operating system

Web app

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Mar 14, 2024
  2. PgBiel commented on Mar 15, 2024

    @PgBiel
    ContributorAuthor

    One possible fix is to always resolve page styles when laying out a page, e.g. (the code below would be shorter in practice, as we'd have some generic implementation of this in #[elem])

    diff --git a/crates/typst/src/layout/page.rs b/crates/typst/src/layout/page.rs
    index f6221887..27ca0154 100644
    --- a/crates/typst/src/layout/page.rs
    +++ b/crates/typst/src/layout/page.rs
    @@ -9,7 +9,7 @@ use crate::diag::{bail, SourceResult};
     use crate::engine::Engine;
     use crate::foundations::{
         cast, elem, AutoValue, Cast, Content, Context, Dict, Fold, Func, NativeElement,
    -    Packed, Resolve, Smart, StyleChain, Value,
    +    Packed, Resolve, Smart, StyleChain, Styles, Value,
     };
     use crate::introspection::{Counter, CounterDisplayElem, CounterKey, ManualPageCounter};
     use crate::layout::{
    @@ -384,6 +384,33 @@ impl Packed<PageElem> {
                     .spanned(self.span());
             }
     
    +        // Ensure the child is aware of the page's properties.
    +        let page_styles = {
    +            let mut page_styles = Styles::new();
    +            page_styles.set(PageElem::set_width(self.width(styles).map(Length::from)));
    +            page_styles.set(PageElem::set_height(self.height(styles).map(Length::from)));
    +            page_styles.set(PageElem::set_flipped(self.flipped(styles)));
    +            page_styles.set(PageElem::set_margin(self.margin(styles)));
    +            page_styles.set(PageElem::set_binding(self.binding(styles)));
    +            page_styles.set(PageElem::set_columns(self.columns(styles)));
    +            page_styles.set(PageElem::set_fill(self.fill(styles).clone()));
    +            page_styles.set(PageElem::set_numbering(self.numbering(styles).clone()));
    +            page_styles.set(PageElem::set_number_align(self.number_align(styles)));
    +            page_styles.set(PageElem::set_header(self.header(styles).clone()));
    +            page_styles.set(PageElem::set_header_ascent(
    +                self.header_ascent(styles).map(Length::from),
    +            ));
    +            page_styles.set(PageElem::set_footer(self.footer(styles).clone()));
    +            page_styles.set(PageElem::set_footer_descent(
    +                self.footer_descent(styles).map(Length::from),
    +            ));
    +            page_styles.set(PageElem::set_background(self.background(styles).clone()));
    +            page_styles.set(PageElem::set_foreground(self.foreground(styles).clone()));
    +            page_styles
    +        };
    +        let styles = styles.chain(&page_styles);
    +
             let area = size - margin.sum_by_axis();
             let mut regions = Regions::repeat(area, area.map(Abs::is_finite));
             regions.root = true;

    This also fixes #context page.margin returning auto, and is probably the preferred solution (I can verify it solves this issue as well).

    We could also override Construct for PageElem by taking the generated code and modifying body to apply .styled_with_map, similarly to ParElem, but that led to several regressions (likely because PageElem might be constructed internally too), so that doesn't seem like a viable solution.

  3. changed the title [-][0.11.0-rc1] `#context page.margin` can't get margin from an ad-hoc `#page`[/-] [+]`#context page.margin` can't get margin from an ad-hoc `#page`[/+] on Mar 18, 2024
  4. added
    styling
    on Mar 18, 2024
  5. tingerrr commented on Mar 19, 2024

    @tingerrr
    Contributor

    Are there any other cases where this might be relevant? One thing I ran into, is page.binding which I assume is fixed by this too. But what about other elements?

  6. PgBiel commented on Mar 19, 2024

    @PgBiel
    ContributorAuthor

    This GitHub issue in particular is a bit too narrow, though other elements also suffer from not having fully resolved properties available through the style chain, such as text - I believe text.dir starts as auto, for example.

  7. l0wx commented on Mar 28, 2024

    @l0wx

    It's the same with page.width & .height.

  8. tingerrr commented on Mar 31, 2024

    @tingerrr
    Contributor

    It's the same with page.width & .height.

    Do you mean that they can be auto? They're only auto if explicitly set, and here, unlike with page.margin, this is actually a meaningful value instead of meaning "infer from other parameters".

    In general, it seems that we would only want to "resolve" auto when it's meant to be a placeholder for a value that is known completely from looking at other parameters. A perhaps crazy suggestion is adding yet another keyword bottom type, a la auto/none, which is used for those cases. But I don't think that would carry its own weight, I just think auto was a great idea alongside none but could sometimes need more context.

  9. l0wx commented on Apr 1, 2024

    @l0wx

    @tingerrr

    They're only auto if explicitly set, […]

    Yeah, but they lead to auto if that definition is in a set-ruled, not if it's coming from an argument.

    page(width: 14.2024cm, context page.width)

    → e.g. 595.20pt (21cm) or even auto if defined so by a preceding set page(width: auto)

    page(width: auto, context page.width)

    → Not (per sé) auto

    In general, it seems that we would only want to "resolve" auto when it's meant to be a placeholder […]

    That was also my comprehension of the current discourse. But, as it has been discussed in parallel on Discord #contributors, may it be that you're mixing this up with the auto auto-resolution related to #3636, despite being independent?

    If it's about my thoughts on that, I'll clarify that on the other issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugstyling

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions