Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to have a counter across pages? #68

Open
duskmoon314 opened this issue Jun 6, 2024 · 3 comments
Open

Is it possible to have a counter across pages? #68

duskmoon314 opened this issue Jun 6, 2024 · 3 comments

Comments

@duskmoon314
Copy link
Contributor

I want the number of images and equations automatically increased across pages (or chapters). Is this possible, and how can we achieve it?

@Myriad-Dreamin
Copy link
Owner

It is possible, as the entire structure is in book.typ which you can visit it. but it should be very hard, as each page is an individual document so you should reset the counter at the start of each page files.

@Myriad-Dreamin
Copy link
Owner

I didn't consider it, I'll take it into consideration when I rework on the book package, which was made in ancient typst (for about v0.6.0 :)).

@Myriad-Dreamin
Copy link
Owner

Myriad-Dreamin commented Dec 29, 2024

I considered it carefully and came up with some solutions but they were all bad.

  1. providing some persisted state and passing them by sys.inputs. (pretty bad)
  2. providing some indicator of which chapter is compiled. (kind of bad, and unfriendly)
  3. and more.

Recently I have some initial idea about it, which utilize the PDF (ebook) target. The ebook target is the compilation where we composes all chapters into a single PDF book, so we can perform some calculation and export it to some file. Then, we can read the file and recover the state of the counter.

Overall, we will have the following steps to make state shared across pages:

  1. run typst query ebook.typ --input target=pdf --selector "<ebook-query-set>" > all-state.json then we have a all-state.json.
  2. run typst compile chapterN.typ --input target=web, which utilizes the content of all-state.json.

The cross-page counter of this issue as an example:

#let ebook-query-main(content) = {
  let figures = query(figure);
  [
    #metadata(somehow-check-figures-for-query(figures)) <ebook-query-set>
  ]
}
show: ebook-query-main

// chapter1.typ
== Chapter 1
#figure(..) <figure1>
#figure(..) <figure2>

// chapter2.typ
== Chapter 2

// chapter3.typ
== Chapter 1
#figure(..) <figure3>
#figure(..) <figure4>

Then we'll get a all-state.json file:

{
  "/api/v1/figures/counter/figure1": "1.1",
  "/api/v1/figures/counter/figure2": "1.2",
  "/api/v1/figures/counter/figure3": "3.1",
  "/api/v1/figures/counter/figure4": "3.2",
}

Then, when we export the web target, we can recovery the counter values by reading this query file:

#let query-set = json("all-state.json")
#let counter-of-figure(figure-label) = {
  query-set.at({
    "/api/v1/figures/counter/"
    str(figure-label)
  })
}


#counter-of-figure(<figure2>) // 1.2

You can find that, the above approach doesn't require any development of shiroa, and we can experiment it a bit by official typst query. If we find it useful, we can consider some convenient helper in the shiroa package to help people make their owned "shared state" across pages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants