Why is the page counter in the background of my page wrong after using counter.update()?

The page numbers are broken, their order is 2, 2, 3, 4, 5, 6 (correct from here on out)
It breaks once I put the counter in set page
my code rn:

set page(background: context(
     context counter(page).display("i")
))

What it looks like rn:


is there a way to fix it?

I can’t reproduce this bug on typst 0.11.1 by provided code. Is your typst updated or is the page number was affected by other parts of your document?

To expand on @ParaN3xus, can you add a MWE (minimal working example) which demonstrates the problem? That is, a stand-alone minimal Typst document that compiles and shows the incorrect page numbering.


In addition, for the benefit of other co-Typstists please consider tagging your question (e.g. counter) and rephrasing the title so it is clearer what you ask from by just reading the title. This post might be helpful.

From my experience, this kind of numbering issues arise from multiple contextual scripting that conflict with each other or are too large in scope. My advice is to disable every contextual bits and find the culprit. Do your best to reduce the amount of context usage.

1 Like

I meant I cannot reproduce the bug with provided code. Maybe we should ask @enxinjie

I actually have the same problem.

MWE
#set page(paper: "a8")

/////////////////////////////////
// Some Content
#lorem(25)

/////////////////////////////////
// Now to the problem:
#set page(numbering: "I", number-align: top, header: context{
  set text(size: 10pt, weight: "regular")
          
  [Page number: #h(1fr) #counter(page).display("I")]
})
#counter(page).update(1)

= ToC
#outline(
  title: none,
  depth: 3, 
  indent: auto
)

#pagebreak(to: "odd")

= ToF
#outline(
  title: none,
  target: figure.where(kind: image)
)

#pagebreak(to: "odd")

Maybe this helps. I am not sure how to fix it…

I assume you want the title page to not count? I.e. ToC to start on page 2, with page counter 1? You have two issues:

  1. the #counter(page).update(1) appears on page 2, so by that time, the header (EDIT: or in OP’s case, the background) of page 2 was already written, with page counter also 2.

  2. the page counter is incremented at page start. For example the counter start out as 0 and is then immediately incremented to 1 on the first page.

What does this mean?

  • page 1 starts, the page counter becomes 1 (but there’s no header to show it)
  • page 2 starts, the page counter becomes 2
  • still on page 2, the page counter is set back to 1
  • page 3 starts, the page counter becomes 2
  • etc.

Which is why you get (no page number), II, II, III, …

The fix is to set the page counter to 0 on page 1, not to 1 on page 2:

#set page(paper: "a8")

#lorem(25)

#counter(page).update(0)

#set page(numbering: "I", number-align: top, header: context{
  set text(size: 10pt, weight: "regular")
          
  [Page number: #h(1fr) #counter(page).display("I")]
})

= ToC
#outline(
  title: none,
  depth: 3, 
  indent: auto
)

#pagebreak(to: "odd")

= ToF
#outline(
  title: none,
  target: figure.where(kind: image)
)

#pagebreak(to: "odd")

So why does it work for the example in the page counter docs? Well, that example shows page numbers in the footer, so setting the counter on the same page is early enough to affect it. I think for now, you have to be aware of where you’re showing the page number when setting the counter: somewhere on the previous page for header, on the new page for footer. A real “set the counter between pages” does not seem to be possible for now.

1 Like

Yes, your assumption was right. Your example fixed the numbering issue of the MWE. Thank you for that!

Unfortunately it still doesn’t work as intended in my main project. The numbering starts with 2 instead of 1.

I think I already identified the reason for this - the following show rule:

#show heading.where(level: 1): it => [
  #pagebreak(to: "odd", weak: true)
  #set text(size: 18pt)
  #v(11mm / 5)
  #it
  #v(11mm / 1)
]

The empty page created by the pagebreak seems to be part of the heading (not part of the previous chapter), hence the incorrect numbering.

Do you have an idea how to fix this?

hmm, interesting problem… maybe pagebreak(to: "even", weak: true) before you reset the page counter?

(I don’t think the problem is that the pagebreak is part of the heading; I think Typst ignores whitespace here correctly. It’s sometimes an edge case though, so maybe it is actually that. What I think is that since the pagebreak may go over two pages, it incremented from 0 to 2 when the first header is produced.)

I didn’t follow the whole conversation, but it might be worth testing the results on the development version because various bugs with respect to interactions of show rules, pagebreaks, and counters were fixed.

2 Likes

Some further testing showed that the following works (on 0.11.1):

#set page(paper: "a8")
#show heading.where(level: 1): it => [
  #pagebreak(to: "odd", weak: true)
  #set text(size: 18pt)
  #v(11mm / 5)
  #it
  #v(11mm / 1)
]

#lorem(25)

#pagebreak(to: "even", weak: true)
// if the even page contains nothing but the counter update,
// it doesn't work. An invisible dot fixes this; since this
// page is by definition otherwise empty, the dot shouldn't
// affect anything.
#hide[.]
#counter(page).update(0)

#set page(numbering: "I", number-align: top, header: context{
  set text(size: 10pt, weight: "regular")
          
  [Page number: #h(1fr) #counter(page).display("I")]
})

= ToC
#outline(
  title: none,
  depth: 3, 
  indent: auto
)

#pagebreak(to: "odd")

= ToF
#outline(
  title: none,
  target: figure.where(kind: image)
)

#pagebreak(to: "odd")

I don’t want to set up a from-source install of Typst right now, but maybe someone else can find out if it will be more seamless in 0.12 :slight_smile:

You can download tinymist that follows nightly Typst from here

It’s a VSCode extension. Use integrated preview or “export pdf” button to see results.

1 Like

To be clear, tinymist is a multi-tool, but the initial and probably mostly used feature is an LSP server, which can be configured with different IDE/text editors.

Exactly. And LSP server executable is also provided in the link.

Hi @enxinjie, I have updated your post’s title to what I thought was a good title according to the response you selected as an answer. If I got it wrong and the new title does not fit your original issue, feel free to change it, but make sure you follow the question guidelines:

Good titles are questions you would ask your friend about Typst.