typst icon indicating copy to clipboard operation
typst copied to clipboard

Providing different `context`s

Open tingerrr opened this issue 1 year ago • 0 comments

Description

Allow, constructing/retrieving and in return providing a different context than the context at which a context block is executed.

Contexts are a rich feature that enable Typst users to do some very cool stuff like relative numbering:

// number headings and figures with the chapter number in the element's context
#set heading(numbering: "1.1")
#set figure(numbering: n => numbering("1.1", counter(heading).get().first(), n))

// reset figures on each chapter
#show heading.where(level: 1): it => {
  pagebreak(weak: true)
  counter(figure.where(kind: image)).update(0)
  it
}

However, once a package tries to provide an interface similar to the various numbering parameters on elements, it cannot feasibly replicate this behavior in all cases. If the given numbering function would be executed in another context, it would have a possibly incorrect chapter number.

I know that passing around the location of queried elements is rather trivial as this was and still is possible today, but I'm not sure how feasible this is for styles.

Use Case

A translation library that I worked on, but stopped due to the lack of this feature specifically, would incorrectly resolve values incorrectly based on interspersed language styles in queries such as outlines. This is one use case that strictly needs the style part of another context.

#show heading: it => text.lang
#show outline.entry: it => {
  text.lang // run this within the context of "it"
}

#set text(lang: "de")
#outline()

#set text(lang: "en")
= heading

As mentioned before, providing something like a function as an argument to something takes away the libraries control over which context must be provided. I've been working on a package for subfigures and a problem I ran into is that I can't trivially support chapter relative numbering of references to these figures without using numbering functions, which are in turn evaluated in the wrong context. This is one use case that only requires the location part of the context. I have the location at which I want the numbering function to be evaluated, but I can't tell it to do so.

tingerrr avatar Apr 14 '24 16:04 tingerrr