Skip to content

Changing the page layout for single pages (e.g. chapter title pages) #1613

Open

Description

@fruchti

Opening this as a feature request since I haven’t found a way to achieve something similar to LaTeX’s \thispagestyle, i.e. (programmatically) setting the page layout for single pages while having normal text flow (the next page has the regular layout again, whenever it begins). It seems that currently it is only possible to set a page layout together with the full contents of the affected page(s).

My concrete use case are chapter title pages with additional logos in the margins, which required me to set header-ascent. I could maybe hack around this with absolute positioning on a chapter headline, but this does not cover other uses like different page margins or background colours on single pages.

I’m not completely sure what the best solution here looks like as I’m not familiar with what typst’s internal architecture allows. I could imaging something like set page(thisionly: true) or a locate() which does not only return content. If approached more generally, i.e. allowing to set some properties of the page without forcing a page break, this could also make e.g. section-based running heads a lot easier to understand than the current way with locate().

My original discussion post with my experiments below:

Discussed in #1082

Originally posted by fruchti May 3, 2023
I’m currently trying to modify the page style for every page with a chapter heading on it. This involves header and footer texts as well as different page margins from the rest. Basically, I’m looking for something more or less like \thispagestyle in LaTeX.

The first step, adding a page break before each chapter, is not an issue:

#show heading.where(level: 1): it => [
    #pagebreak(weak: true)
    #it
]

However, I haven’t found a way to set page() the way I want. Things I’ve tried so far:

  • Using show/set (e.g. #show heading.where(level: 1): set page(..title_page_options)) effects a page break immediately after the heading. I assume since the page style is only set for the heading’s block, typst begins a new page to assure that the next block has the old page style again. [Update: With 0.5, this doesn’t do anything anymore]
  • Defining a function (used instead of = Chapter Title) which includes a #set page has the same result.
  • I might be able to build a selector out of before() and after() which gets me pages with a chapter heading on them. With this, I could adjust the header/footer text like the example in the docs. Since locate() returns a content, I cannot use this to adjust the margins. I also cannot adjust all values at once, since I didn’t find a way to pass a dictionary through locate().
  • With #show page: p => locate(loc => p) I got no output at all any more, so I didn’t try any further based on that.
  • I tried using #set… if together with locate() by returning content from locate() which either has or hasn’t a specific field, depending on whether I am currently on a page with a chapter heading or not. However, even with
    #set page(..title_page_options) if locate(loc => box(width: 1pt)).has("width")
    
    the condition never holds and the style doesn’t get applied (it looks like the locate() isn’t even evaluated actually).

Activity

  1. added
    layout
    feature request
    on Jul 3, 2023
  2. flavio20002 commented on Jul 28, 2023

    @flavio20002

    Hi, the page break happen also in last version https://typst.app/project/rPZKNPwCPtiozZHCzDmDLO.
    What do you mean with [Update: With 0.5, this doesn’t do anything anymore]?

  3. fruchti commented on Sep 25, 2023

    @fruchti
    Author

    What do you mean with [Update: With 0.5, this doesn’t do anything anymore]?

    Interestingly, I cannot reproduce this any more. I retried it with a test document and typst 0.4, 0.5, 0.6, and current main and got the same result with each version (each heading produces an extra page with just the heading and page header on it and there’s a blank page at the end). Perhaps I was using some commit with a different bug in it when I opened this issue.

    Test document:

    #show heading.where(level: 1): set page(header: "Hello")
    #lorem(200)
    = Test 1
    #lorem(100)
    = Test 2
  4. laurmaedje commented on Jul 15, 2024

    @laurmaedje
    Member

    Related: #1412, #1483

  5. pithuene commented on Oct 25, 2024

    @pithuene

    Hi, I just ran into the same problem with chapter title pages (in my case I want to hide the page header on them) and found a workaround that might be useful for others running into this problem.
    Instead of setting different page styles on the page, you can put some logic into the initial page style definition so that it differs between pages.
    In my case, I used a context if in the header with a query to check if the page number of the next level 1 heading is the current page, in which case I leave heading empty.
    If you want to change individual pages, you could probably extend this by using something like a metadata element on those pages and querying for its existence in the set page definition.

    Here is a complete example:

    #set page(
      header: context if (
        not query(heading.where(level: 1).after(here()))
          .map(h => h.location().page())
          .at(0, default: 0) == here().page()
      ) [Not a chapter page]
    )
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

    feature requestlayout

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions