How to get elements by tag (or something similar)?

I am using multiple standalone .typ files that all contain theorems, examples, etc besides having other content. Theorems and examples are just boxes with some content.

Is there a way to compile all theorems into one file while dismissing the other content from the files? I tried giving them labels but querying them results in getting metadata that is not true typst code nor content.

I would really appreciate some help, thank you in advance :)

You can query for all the objects that you want to recite and then just do:

#context {
  for obj in query(...) {
    obj
  }
} 

Here is a full example with great-theorems:

#import "@preview/great-theorems:0.1.1": *

#show: great-theorems-init

#let theorem = mathblock(
  blocktitle: "Theorem",
)


= Original Theorems

Here is some content and now comes a theorem.

#theorem[
  A theorem.
]

Some more content inbetween, and now another theorem.

#theorem[
  Here it is.
]

= Restate

Here is a list of all theorems:

#context {
  for theorem in query(selector(figure.where(kind: "great-theorem-uncounted").or(figure.where(kind: "great-theorem-counted")))) {
    theorem
  }
}

However, this messes up counters. For now I don’t know how to fix the counter problem in a general way.

Hey @FrederikRichter, welcome to the forum! I’ve updated your post title to better fit our guidelines for question posts: How to post in the Questions category

Make sure your post title is a question you’d ask to a friend about Typst. :wink:

Hello @FrederikRichter,
to complement @jbirnick’s answer using great-theorems:

  • If you theorems and examples are figures, then it’s easier to query and retrieve the content. You might find it easier to write things like outline(target: figure.where(kind: "theorems") ! :smiley:
  • I do not think there is a way to import labels from an external file (see How to cross-reference labels defined in external typst projects?), your best bet is probably to have a specific show rule to filter the content you do not want, e.g., show: only-maths where only-maths reads the whole document body and selects only your theorems/examples.
  • I think you might find it easier to just query all your theorems/examples on a separate page and specifically output that one using typst --pages <PAGES>. (See above answer on how to query).

If you have any code that minimally reproduces your use case, don’t hesitate to share it, or to share more details!