typst / typst/typst

Providing different `context`s

Open
#3,930 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request scripting
Dominant language
Rust
Stars
56.1k
Forks
1.7k
Avg merge
3d 10h
Merged PRs (30d)
22

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing context behavior described in the issue and tracing how context blocks, styles, locations, and numbering functions are evaluated. Compare the translation-library and chapter-relative figure examples to define the required semantics. Done means a documented design and implementation can provide or evaluate a different context without breaking current context behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.