Repository navigation
Description
Description
It's pretty standard to have equations/figures/tables/etc in academic work numbered by section or by chapter (i.e. the 23rd equation of section 5 is equation (5.23) ). While Typst makes a lot of things easier than the competition, arranging for numbering by section currently has a lot of friction. Ad-hoc solutions are starting to appear, but with varying amounts of fragility. It seems like numbering-by-section is a feature that should be built in to Typst, and easy to use out-of-the box.
This feature request is meant to less than an RFC, but a home for discussing solutions that might improve the user experience. In a perfect world, something like
set math.equation( numbering: "({h1}.1))", supplement: none)
would be enough to get equation numbers numbered by section, in parentheses, and at the same time have a reference @profound-eq yield (5.23) for equation 23 of section 5.
To make number-by-section happen currently for equations:
- A show rule for
headingneeds to be established to resent the equation counter when a new level 1 heading begins. - A custom numbering function needs to be assigned to
math.equation. Because numbering functions only receive counter values and no other info, the numbering function can only add the section number based on its call site usinglocation. Also, the numbering function needs to be careful to format the section number so that if appendix numbering by A, B, C begins, then this is correctly detected, and so that if subsections aren't numbered we don't accidentially pick up a null numbering function if we forget to seek level 1 sections. I've seen both these bugs in the wild. - Simultaneously, a show rule for
ref(with nontrivial logic because of anulltest and a type check) that gets along with the element function's numbering function needs to be set up. Because the numbering function formath.equationis location sensitive, it can't be fully recycled here and it seems like therefshow rule has to know separately the details of numbering by section. Shared code could be refactored, of course, but the point is the complexity.
All this can be made to work, but I hope this is just a temporary solution as Typst matures.
One could imagine a Gadget, managing a counter for an element function, that does the following kinds of tasks:
- Responds to a hook for updating the element's counter prior to an automatic bump. There is a hook already, the
Counttrait, but it's only available from rust. For numbering by section, the Gadget can look into the past to the last time the counter it manages was updated, grab the section number then, compare it to the section number now, and reset the managed counter if need be. The hook could also have greater responsibility, maybe replacing theCounttrait, perhaps. - Is responsible, given a location, for formatting the counter at that location. It can, e.g, look into the past for the last level 1 heading, use the heading's Gadget to format the heading counter at the past location, and then use the result in the formatting of the equation's number.
- Is the authority that references use (by default) for for formatting. The Gadget feels like the place that knows how references should be formatted; let it tell the references it's opinion on the subject.
I'm being deliberately vague about what a Gadget is. It could just be a few properties of an element function that are auto by default and are sensibly synthesized and wired-up based on a string "({h1}.1}" if needed. But it seems like it could be helpful to have a layer of abstraction sitting above or augmenting the current numbering mechanism.
Use Case
See above.
Activity
DrGo commentedon Aug 9, 2023
Agree that numbering of elements is ideally handled by the compiler. But wondering why it should be limited to equations as compared to other elements, e.g., figures, tables etc
damaxwell commentedon Aug 9, 2023
I had meant equations only as a concrete example. Same mechanism, whatever it is, for figures (and their theorem cousins) etc.
KronosTheLate commentedon Sep 14, 2023
+1 on this, would love numbering by section!
memeplex commentedon Mar 19, 2024
As a workaround, I've been doing this for some simple documents. It's probably not perfect and my understanding of the problem is very limited compared to OP's, but seems to be working fine with references and doesn't require to explicitly deal with location or other context.
laurmaedje commentedon Mar 19, 2024
Yes, that this now works properly is actually new in 0.11 because the correct context for
counter(heading).get()is provided to the numbering!memeplex commentedon Mar 19, 2024
Great, it's impressive how easy it is to extend Typst.
Is this because of what's mentioned in https://typst.app/docs/reference/context/ ?
It confused me at first that I didn't have to provide a context, even though the numbering function is for math.equation but the requested counter is for heading. So are all numbering arguments ultimately evaluating the numbering() function inside a context?
laurmaedje commentedon Mar 19, 2024
Exactly. The numbering functions are implicitly provided with the location in the document of the element in question, so that you can retrieve all sorts of counters there.
PhyX-Meow commentedon Apr 5, 2024
You should use
Otherwise the heading itself will disappear.
KronosTheLate commentedon May 21, 2024
Starting from suggestions in this thread and some hacking about (and importantly realizing that figures have one global counter and one counter based on the kind of figure) I was able to put this together:
show heading.where(level: 1): it => { counter(math.equation).update(0) counter(figure.where(kind: image)).update(0) counter(figure.where(kind: table)).update(0) counter(figure.where(kind: raw)).update(0) it } set math.equation(numbering: num => "(" + (str(counter(heading).get().at(0)) + "." + str(num)) + ")" ) set figure(numbering: num => str(counter(heading).get().at(0)) + "." + str(num) )Improvements upon previous suggestions:
Andrew15-5 commentedon May 21, 2024
Here is a bit more optimized version (although I haven't tested it):
Joelius300 commentedon May 30, 2024
i-figured seems to be an implementation of this (using nested figures) as Typst Package (see in universe).
snaggen commentedon Sep 5, 2024
I just want to point out that the above workarounds (and also the i-figured package, I think) will not correctly handle the case where you have regular chapters with "1" numbering, and then appendixes with "A" numbering. Figures in Appendixes should be referred to as A.1, but will instead be something like 8.1. So, it is important to use the actual heading number as displayed.
Joelius300 commentedon Sep 5, 2024
i-figuredcan handle that just fine, if you pass it to the respectiveshow-figurecall. I usedA.inumbering for figures in the appendix and regular chapters with1numbering. Theeqthing is a workaround to apply the same thing to equations because I didn't figure out how to get i-figured to correctly number equations, it seemed to skip around weirdly. I can talk more about that if you want.svandenhaute commentedon Sep 12, 2024
Does anyone have an idea how to also modify the formatting of the caption when using custom numbering with the approach suggested earlier?
I'd prefer to use the following show rule to format captions but this seems to mess with the numbering. Any idea what's going on?
my guess is that show rules don't always receive location context?
34j commentedon Dec 14, 2024
I would just like to say that the advantage of this method over i-figured is that it is compatible with equate (numbering per line).
Minimal Example
Andrew15-5 commentedon Dec 14, 2024
I need to point out that
num =>won't always work elsewhere, because of #2102, https://typst.app/docs/reference/model/numbering/.It's a best practice to stay consistent and write a numbering callback function like this:
uwni commentedon Jan 12, 2025
How do you customise the caption while using this trick? for example, if I want the supplement and numbering part get italic or bold. A normal way is to make something like
[*#it.supplement~#it.numbering~#it.separator* it.body]Andrew15-5 commentedon Jan 12, 2025
patrick-kidger commentedon Nov 27, 2025
For those who need a general solution to this problem, let me x-post from #2652 (comment):
I've made tree-counters in typsy, and usable like so:
mohe2015 commentedon Dec 23, 2025
@patrick-kidger Can you show an example with automatic per-chapter figure numbering where
refdoes not increase the counter?kyzyl commentedon Jul 31, 2026
So I can't tell from the discussion above, what is the status of this feature as of mid 2026? Are we still supposed to DIY hack it ourselves? Is the typst team working on integrating a real solution? I've tried most of the solutions above, and packages like i-figured/equate/etc. As far as I can tell, none of the solutions actually work. I can't get controlled depth, per-section table/figure/equation numbering, with per-line labelling.
Scientific literature really needs to be able to break stuff up by section, and authors need to be able to reference into equation blocks line-by-line. Per-block numbering lumps potentially many complicated expressions into one reference, and splitting each line into separate blocks has very poor ergonomics while breaking alignment and other things.
The equate package solves the per-line labelling, but break per-section/sub-section numbering. I'm pretty new to typst scripting, but in my experiences thus far that applies even when attempting to extend equate with various hacks people have suggested.
The i-figured package solves the per-section numbering, but is incompatible with equate. It's a similar story with other packages. Hacking it together myself similar to solutions above also has not been successful for me. There is always a couple of elements which don't quite work right, and it's hard to figure out why.
It's going to be impossible for advocates to get their academic colleagues to adopt typst if stuff like this doesn't work. It's table stakes, no pun intended. I might as well be suggesting that they switch to emacs+org-mode+tangle+elisp. People don't want to configure their environment, they want to write their paper, and they are already behind on that.
Anyhow sorry to complain. It's just frustrating when a tool solves a lot of your problems, but is then just out of reach for practical use. Hope this can get solved soon.
mohe2015 commentedon Jul 31, 2026
@kyzyl Can you try https://typst.app/universe/package/numera/ please?
I invested lots of time to try to fix the problems you mentioned and would be interested in feedback.
jbirnick commentedon Aug 1, 2026
Also have a look at rich-counters. If you need theorems you can combine it with great-theorems. (I also wish there was a native solution though.)
kyzyl commentedon Aug 1, 2026
@mohe2015 I did try it. It's a bit hard to tell if I'm using it wrong since I can only extrapolate from the examples, but like the other packages it seems to almost do the job. Take the following example:
From my perspective, here is what it does correctly and is useful:
And here is what isn't that useful or doesn't quite work:
levelargument seems to only control the level at which the counters reset, not the prefixing level. In the example above withlevel: 2I would expect the numbering to proceed (1.1), (1.2), (1.3), (1.1.1), (1.1.2), (1.2.1), (1.2.2), (1.2.3), (1.2.4), (2.1), ... Instead it resets the counter at level 2 but keeps the level 3 prefix 1.2.1.x. I can't think of a time where I don't want to fully number down to level 3, but I DO want to keep contiguous numbers for levels 3+. That seems like it would be a confusing paper to read.level: 0essentially means "no sub-numbering, just one contiguous count". Instead of (1), (2), (3), ... through all sub-sections, I get a contiguous count up but the prefixes stay (1.1), (1.2), ..., (1.2.1.8), etc. It's very common to need per-section numbering down to arbitrary levels (think numbering in a textbook), but it's also very common to decide to just have equations numbered sequentially in a short paper.math.equation(numbering: "(1)" ...). Moreover, having done that, those parentheses now appear in the references "Eq. (1.1)" meanwhile someone only want the parentheses in the margins, while the references read like "Eq. 1.1".sub-numbering: true, I get strange labels like starting with (1.11), (1.12), (2). I suppose this is just a bad interaction between the packages but it is a bit confusing.#show numera(level: 2, ...)block.In any case, thanks for the effort. It's probably as close as any solution has come :) I also maintain that this stuff really should be handled in Typst core, with sane defaults that can be overridden. 99.99% of users will not pursue a solution the folks in this thread have, they will just bail.
@jbirnick I haven't checked out rich-counters yet. After a cursory look it isn't obvious how to apply it to get the behaviour I've described but I'll think about it.