Skip to content

set page inside context appends empty page #2326

Closed

Description

@EpicEricEE

Description

I'm trying to adjust the page margin depending on the maximum heading numbering width, so I need to use #set page inside locate.

#set page(numbering: "1", width: 6cm, height: 2.5cm, margin: 2em)

First Page

#locate(loc => [
  #set page(numbering: "i")
  Second Page

  Third Page
])

image

Reproduction URL

No response

Operating system

Web app

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Oct 6, 2023
  2. added
    scripting
    on Oct 9, 2023
  3. added
    needs-investigation
    on Nov 14, 2023
  4. alexanderwiller commented on Nov 18, 2023

    @alexanderwiller

    I am trying something similar, I want both different header content and different margins depending on if I'm on the first page or not.

    First, I did this to achieve a different header between the first and following pages:

    set page(paper: "a4",
      header: locate(loc => {
        if counter(page).at(loc).first() == 1 { header_1stpage } else { header }
      }),
    )

    That approach worked, but I wanted to have margins that differ per-page too, so I tried to make the margin dynamic based on the page too:

    set page(paper: "a4",
      margin: locate(loc => {
        if counter(page).at(loc).first() == 1 { (left: 25mm, top: 98.46mm, rest: 20mm) } else { (left: 25mm, top: 40mm, rest: 20mm) }
      }),
      header: locate(loc => {
        if counter(page).at(loc).first() == 1 { header_1stpage } else { header }
      }),
    )

    That approach did not work, because locate does return content (related to #1613 I think), not the type of what is contained inside the locate (dict of margins).

    In general, repeating the locate seems quite verbose, so I tried something different:

    set page(paper: "a4", margin: (left: 25mm, rest: 20mm))
    
    locate(loc => {if counter(page).at(loc).first() == 1 {
      set page(header: header_1stpage, margin: (top: 98.46mm))
    } else {
      set page(header: header, margin: (top: 40mm))
    }})

    While I really like the cleanliness, this approach works only partially:

    • The first page header and top margin is set according to the rule
    • There is no page body on the first page, it starts only on page two
    • Neither second page header nor top margin are set according to the rule

    I created a complete example that shows the situation: https://typst.app/project/rjX6qQRpfnYDu9HZJIoK4l

    In general, I wondered even before trying if this approach (with set page inside locate) might work at all, because I don't know where the locate would be placed, if not on the page. As I understand it, there is no page context for the locate at all, so where is it located?

    Is there a different way to achieve the desired result?

    My use case is a letter btw., that should have address fields only on the first page. I guess I could add the address fields and such to the content itself, pad with negative margins, but that feels quite backwards.

  5. added
    styling
    and removed
    needs-investigation
    scripting
    on Feb 13, 2024
  6. laurmaedje commented on Feb 13, 2024

    @laurmaedje
    Member

    The reason is that invisible metadata is generated through locate to which the page set rule doesn't apply. It's a bit unfortunate interaction.

    Note that putting large parts of the document into locate is generally not something I would recommend. It is not well tested and will also degrade performance.

    Is there a different way to achieve the desired result?

    Proper dynamic page margins are unfortunately not possible at the moment.

  7. l0wx commented on Mar 26, 2024

    @l0wx

    Description

    v10.0 allows for style(d=> …) set page() rules, while v11.0 doesn't do any more (context or style(d=> …) and set page(); content or page(content) make no difference).

    So the context unification led to the former style() getting dragged down instead of the former locate() getting hauled up (i.e. also not triggering the empty page appendage).

    Examples

    Minimal

    #context page(
        margin: 1cm,// set anything to trigger the bug
        "Empty Page Appendage Bug",
    )
    

    Simplified Practical Comparison with v10.0

    This is working gracefully with v10.0, but not with v11.0.

    (For simplicity, this example is tailored to A4 with default margins.)

    #import calc: sqrt, rem
    
    #show: all=> style(sts=> {
        let running-ht = measure([g], sts).height
        let running-ln-ht = measure([g\ g], sts).height - running-ht
        let running-leading = running-ln-ht - running-ht
    
        let t-area-envis-ht = 297mm - 50mm
        let t-area-addit-ht = (running-ln-ht.mm() - rem(t-area-envis-ht.mm(), running-ln-ht.mm())) * 1mm
        let t-area-ht = t-area-envis-ht + t-area-addit-ht - running-leading
    
        set page(
            margin: (bottom: 100% - 25mm - t-area-ht, rest: auto),
        )
    
        all
    })
    #lorem(999)
    

    Comparison with v10.0—Subsequent set page() Rule

    Also this is working gracefully with v10.0, but not with v11.0.

    The set page() rule is part of chic()—utilized here in order to show off an interplay with a package.

    #import "@preview/chic-hdr:0.4.0": *
    
    #show: it=> style(sts=> it)// or `context it`
    #show: chic.with(
        chic-header(left-side: [Hallo]),
    )
    #lorem(999)
    

    Background

    Primarily, a module of mine is assessing & setting the type area (i.e. the area left between the margins) in the fashion of the 2nd example above. It slightly adjusts its height based on the line height (with the current text/par settings); so the (bottom) margin depends on a measure()ment.

    I can mitigate the effects of this bug by ‘pulping’ the additionally appended page using a preceding set rule (#set page(height: auto, margin: 0pt);#context {page(paper: "a4", margin: auto, <…>)}) and subtracting one from the page total wherever shown/used. But printing reveals the existence of that page (in form of sheet demand).

    So my work is hindered as I see no alternative approach. Also, I can't switch back to v10.0 because I pressingly need the contextual property access feature.

  8. Andrew15-5 commented on Jun 17, 2024

    @Andrew15-5
    Contributor

    Here to say that setting page inside a content block and adding anything (even invisible and non-space-consuming) after that [] block will result in an empty page at the end as well: #4407.

  9. changed the title [-]`set page` inside `locate` appends empty page[/-] [+]`set page` inside `context` appends empty page[/+] on Jul 16, 2024
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