Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Query

This section may be not very complete and fully updated for last Typst versions. Any contribution is very welcome!.

Link to reference

Query is a thing that allows you getting a location (an object that represents literally a place in document, see docs here) 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 #context s.get().
]

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())
  )
  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