Let compiler infer figure counter level from nesting depth of figures to enable custom multi-level numberings (i.e. subfigures)
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 56.1k
- Forks
- 1.7k
- Avg merge
- 3d 10h
- Merged PRs (30d)
- 22
Description
Description
This proposal intends to let the compiler to infer figure counter level from the nesting depth of figures. The idea was proposed by @laurmaedje (https://github.com/typst/typst/issues/6767#issuecomment-3365276049), who also suggested to open this feature request/proposal.
By utilizing the inherent nature of the structure of Typst documents with nested figures it would be possible for the compiler to infer and set the figure counter level (based on figure nesting depth). This would make it trivial to implement support for subfigures (and "subsubfigures", etc) that facilitate custom multi-level numberings.
Use Case
With this proposal, the compiler would be able to infer the counter state of figures based on nesting level (as outlined in https://github.com/typst/typst/issues/6767#issuecomment-3267999702 and included below for convenience).
For the given example, the compiler would then infer that the counter of "outer figure" would be (1,), the counter "subfigure 1" would be (1, 1) and the counter of "subfigure 2" would be (1, 2), as inferred by nesting level of figure elements in the document AST.
#figure(
grid(
columns: 2,
gutter: 1em,
figure(
rect(fill: aqua),
caption: [subfigure 1],
),
figure(
rect(fill: teal),
caption: [subfigure 2],
),
),
gap: 1em,
caption: [outer figure],
)

Having the compiler automatically infer the counter level of figures based on the existing nesting information of figures intrinsically embedded within the AST structure of Typst documents is would be great as it lets subfigure implementations rely on existing counters (instead of duplicating state that can go out of sync; see https://github.com/typst/typst/pull/5716#issuecomment-3143084088).
As such, this proposal would go a long way into making subfigures with custom multi-level numberings easier to implement in Typst (#246).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No files, tests, or entry points are named. Start by tracing how figure elements and counters are represented in the compiler's document AST, then compare that behavior with the linked discussion and the existing subfigure work in issue #246 and pull request #5716. Done should support nested figures with counter levels such as (1,), (1, 1), and (1, 2) for custom multi-level numbering.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100