How to compile only certain elements of a document and discard the rest?

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!

Here’s a solution that lets you select the output from the command-line (and I think also fixes the counter issue):

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

#let doc-or-theorems(doc) = {
  let only-theorems = sys.inputs.at("theorems", default: "false") == "true"
  if not only-theorems {
    return doc
  }
  show heading: none
  context query(selector.or(
    figure.where(kind: "great-theorem-uncounted"),
    figure.where(kind: "great-theorem-counted"),
    heading,
  )).join()
  show: it => place(hide(it))
  show pagebreak: none
  doc
}

#set heading(numbering: "1.1")
#show: great-theorems-init
#show: doc-or-theorems

#let mathcounter = rich-counter(
  identifier: "mathblocks",
  inherited_levels: 1
)

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

= Some Heading

#theorem[
  This is some theorem.
] <mythm>

= Another Heading

#theorem(title: "some title")[
  This is a theorem with a title.
] <thm2>

(This won’t work though if you change page settings after applying this template.)

Use typst c file.typ to get the normal document, and typst c file.typ --input theorems=true to get only the theorems.