Skip to content

Fix nested theorem numbering: child counters now inherit correct parent number#35

Merged
OrangeX4 merged 4 commits into
mainfrom
copilot/feature-inherit-nested-theorem-numbering
Mar 12, 2026
Merged

Fix nested theorem numbering: child counters now inherit correct parent number#35
OrangeX4 merged 4 commits into
mainfrom
copilot/feature-inherit-nested-theorem-numbering

Conversation

Copilot AI commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

Child-counter frames (e.g. corollary) placed inside a parent frame body (e.g. theorem) received the wrong parent number. The counter step was emitted at the end of the figure body, so any nested child read theorem-counter = n-1 instead of n.

#theorem()[         // Theorem 1
  #corollary()[]    // was: Corollary 0.1 — now: Corollary 1.1 ✓
]
#theorem()[         // Theorem 2
  #corollary()[]    // was: Corollary 1.1 — now: Corollary 2.1 ✓
]

Sequential placement (corollary after theorem) is unaffected.

Changes (core.typ)

  • make-frame — emit frame-counter.step() at the start of the figure body (before metadata + render), not the end; nested children now see the incremented parent counter at their document location
  • display-number — drop the +1 offset; the step precedes here() so the raw counter value is already correct
  • theorion-display-number — use the <theorion-frame-metadata> label location (inside the body, after the step) instead of el.location() (the figure anchor, before the step) to maintain correct numbering in outlines and cross-references
Original prompt

This section details on the original issue you should resolve

<issue_title>Is it possible to inherit numbering of nested theorems?</issue_title>
<issue_description>Nested theorems work really well graphically. Is it possible to nest theorems and to keep the numbering inherited from the parent theorem?

#import "@preview/theorion:0.4.0": *
#import cosmos.fancy: *
#show: show-theorion
= This works (Sequential)
#theorem()[
]
  #corollary()[]
#theorem()[
]
  #corollary()[]

= Desired (Nested)
#theorem()[
  #corollary(number: 1.1)[] // Hardcoded
]
#theorem()[
  #corollary(number: 2.1)[] // Hardcoded
]
Image

This question originates from https://forum.typst.app/t/how-to-do-hierarchical-counting-for-sub-frames-using-the-frame-it-package/6455?u=vmartel08</issue_description>

<agent_instructions>能不能让
#import "@preview/theorion:0.4.1": *
#import cosmos.fancy: *
#show: show-theorion
#theorem()[
#corollary()[]
]
#theorem()[
#corollary()[]
]

渲染为下图,目前的嵌套编号会存在问题

</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Move frame-counter.step() to the beginning of each frame body so that
child counters (e.g. corollary inside theorem) inherit the correct
parent number. Remove the +1 adjustment in display-number since the
counter is already at the right value when here() is evaluated.
Update theorion-display-number to use the metadata location (after
the step) for accurate counter reads. Add regression test.

Co-authored-by: OrangeX4 <34951714+OrangeX4@users.noreply.github.com>
Copilot AI changed the title [WIP] Add ability to inherit numbering of nested theorems Fix nested theorem numbering: child counters now inherit correct parent number Mar 12, 2026
@OrangeX4 OrangeX4 marked this pull request as ready for review March 12, 2026 08:40
Copilot AI review requested due to automatic review settings March 12, 2026 08:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes hierarchical theorem numbering when a child frame (e.g., corollary) is placed inside a parent frame’s body (e.g., theorem) by ensuring the parent counter is stepped before nested content is laid out.

Changes:

  • Move frame-counter.step() to the start of the frame body so nested children see the incremented parent number at their location.
  • Update display-number to remove the previous +1 adjustment (counter is now stepped earlier).
  • Update theorion-display-number to compute numbering using the <theorion-frame-metadata> location (after stepping) to keep outlines/refs correct.
  • Add a regression test and reference snapshot for nested numbering.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.

File Description
core.typ Adjusts counter stepping and numbering-location logic to make nested child counters inherit the correct parent number.
tests/issues/i-nested-numbering/test.typ Adds a regression test covering nested corollaries within theorems (including multiple nested corollaries).
tests/issues/i-nested-numbering/ref/1.png Adds the visual reference output for the new regression test.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread core.typ Outdated
Comment thread core.typ
Comment on lines +267 to +274
let get-loc = () => {
let metadata-els = query(selector(<theorion-frame-metadata>).after(el.location()))
if metadata-els.len() > 0 {
metadata-els.first().location()
} else {
el.location()
}
}

Copilot AI Mar 12, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get-loc performs a query(...) each time it’s called. Since the numbering function may call get-loc more than once (especially for user-supplied numbering functions), this can repeat an O(N) query unnecessarily. Consider computing the metadata location once (outside the closure) and making get-loc return that cached location.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@OrangeX4 OrangeX4 merged commit e178b2b into main Mar 12, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Is it possible to inherit numbering of nested theorems?

3 participants