Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providing different contexts #3930

Open
tingerrr opened this issue Apr 14, 2024 · 1 comment
Open

Providing different contexts #3930

tingerrr opened this issue Apr 14, 2024 · 1 comment
Labels
feature request New feature or request scripting About Typst's coding capabilities

Comments

@tingerrr
Copy link
Contributor

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 tingerrr added the feature request New feature or request label Apr 14, 2024
@laurmaedje laurmaedje added the scripting About Typst's coding capabilities label Apr 29, 2024
@indicatelovelace
Copy link

Another idea that fits to this issue is to retrieve a value in the context of other elements. Sometimes you need to know the parameters of other elements, outside of their respective scope. This is for example the case when adding space between specific elements. I can image a syntax like context.with(element: par) or something like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request scripting About Typst's coding capabilities
Projects
None yet
Development

No branches or pull requests

3 participants