Query

This section is outdated. It may be still useful, but it is strongly recommended to study new context system (using the reference).

Link there

Query is a thing that allows you getting location by selector (this is the same thing we used in show rules).

That enables "time travel", getting information about document from its parts and so on. That is a way to violate Typst's purity.

It is currently one of the the darkest magics currently existing in Typst. It gives you great powers, but with great power comes great responsibility.

Time travel

#let s = state("x", 0)
#let compute(expr) = [
  #s.update(x =>
    eval(expr.replace("x", str(x)))
  )
  New value is #s.display().
]

Value at `<here>` is
#context s.at(
  query(<here>)
    .first()
    .location()
)

#compute("10") \
#compute("x + 3") \
*Here.* <here> \
#compute("x * 2") \
#compute("x - 5")
Rendered image

Getting nearest chapter

#set page(header: context {
  let elems = query(
    selector(heading).before(here()),
    here(),
  )
  let academy = smallcaps[
    Typst Academy
  ]
  if elems == () {
    align(right, academy)
  } else {
    let body = elems.last().body
    academy + h(1fr) + emph(body)
  }
})

= Introduction
#lorem(23)

= Background
#lorem(30)

= Analysis
#lorem(15)
Rendered image