Custom numbering function with total number of pages breaks the numbering

I was trying to have arabic numerals for the main part of the document and roman numerals for the appendix. I would also like to include the numbers of pages of the main part in the footer.

With help from How to use Arabic and Roman page numbers in one document? I ended up with this code:

#let main-numbering(..n) = context {
  numbering("1 | 1", n.at(0), ..counter(page).at(<appendix>))
}

#let footer = context [
  #h(1fr)
  #counter(page).display()
  #h(1fr)
]

#set page(paper: "a8", footer: footer, numbering: main-numbering)

/* Main Content here */
= Chapter One
#lorem(20)

#metadata("end of main content") <appendix>
#set page(numbering: "I")
#counter(page).update(1)
#pagebreak()

/* Appendix Here */
= Appendix 1
My appendix

Now from the looks of it, this works reasonably well. However, if I add a outline, the total number of main pages now appear in in a outline as well :(

It also breaks the page count in Firefoxโ€™ PDF viewer.

Is it possible to fix this? Before I added the appendix, I used to use #counter(page).display("1 | 1", both: true) which worked very well. But I canโ€™t seem to apply both in this case.

Does this work for you? I remove the total number from page.numbering, and add the logic to page.footer.

#set page(numbering: "1", footer: context {
  set align(center)
  if page.numbering == "1" {
    // Show the total number of pages when the main content.
    numbering("1 | 1", ..counter(page).get(), ..counter(page).at(<appendix>))
  } else {
    // But not for the appendix.
    counter(page).display()
  }
})




Full code
#set page(width: 20em, height: auto, margin: 4em)

#set page(numbering: "i")
#outline()

#set page(numbering: "1", footer: context {
  set align(center)
  if page.numbering == "1" {
    // Show the total number of pages when the main content.
    numbering("1 | 1", ..counter(page).get(), ..counter(page).at(<appendix>))
  } else {
    // But not for the appendix.
    counter(page).display()
  }
})
#counter(page).update(1)

/* Main Content here */
= Chapter One <chapter>
#lorem(20)
#pagebreak()
== Section
See #ref(<chapter>, form: "page").


#metadata("end of main content") <appendix>
#set page(numbering: "I")
#counter(page).update(1)

/* Appendix Here */
= Appendix 1
My appendix

If you are developing a template, you can refer to How to correctly display page numbering in a custom footer (with and without total number of pages)? - #2 by Eric for a general approach.

Yes, this seems to work perfectly in my testing! Thanks!