Last active
April 27, 2024 02:58
-
-
Save YDX-2147483647/4196137b5eaa7779d8ddd114ec5f70fb to your computer and use it in GitHub Desktop.
Make equations numbered by chapters in Typst.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Make equations numbered by chapters (heading level 1) | |
/// | |
/// Only works if headings are numbered. | |
/// | |
/// # Usage | |
/// | |
/// ``` | |
/// #show: equation-numbering-rules | |
/// #set heading(numbering: "1.") | |
/// | |
/// $ sin x $ <eq:sin> | |
/// | |
/// @eq:sin | |
/// ``` | |
/// | |
/// # Credit | |
/// | |
/// Modified from Anton Wetzel's comment: | |
/// https://github.com/typst/typst/issues/1768#issuecomment-1921081584 | |
/// | |
/// https://gist.github.com/YDX-2147483647/4196137b5eaa7779d8ddd114ec5f70fb | |
#let equation-numbering-rules(body) = { | |
let equation-label(heading, equation) = [(#heading#sym.dash.en#equation)] | |
// Define the `numbering` of `equation`s | |
set math.equation( | |
numbering: equation => locate( | |
// Locate where we are in the document | |
loc => { | |
// Get the heading index at the location at offset 0 for heading level 1 | |
let heading-index = counter(heading).at(loc).at(0) | |
equation-label(heading-index, equation) | |
}, | |
), | |
) | |
// Overwrite how a `ref` is displayed | |
show ref: it => { | |
// Skip if the label has no corresponding element | |
// or it is not an `equation`. | |
if it.element == none or it.element.func() != math.equation { | |
return it | |
} | |
locate( | |
loc => { | |
// location of the `equation` that the `ref` points to | |
let equation-location = query(it.target, loc).first().location() | |
let heading-index = counter(heading).at(equation-location).at(0) | |
let equation-index = counter(math.equation).at(equation-location).at(0) | |
it.element.supplement + [ ] + equation-label(heading-index, equation-index) | |
}, | |
) | |
} | |
// Reset `equation`'s counter if a new chapter starts | |
show heading.where(level: 1): it => { | |
counter(math.equation).update(0) | |
// display the heading without changes | |
it | |
} | |
body | |
} |
Alternatives
-
(Recommend) https://discord.com/channels/1054443721975922748/1054443722592497796/1088839394665955378 by laurmaedje
#let equation-numbering-rules(body) = { // Or `show heading.where(level: 1): …` show heading: it => it + counter(math.equation).update(()) set math.equation(numbering: (..nums) => { // Or `counter(heading).get().at(0)` let h = counter(heading).display("1") // Remove the trailing dot let eq = nums.pos().map(str).join(".") [(#h#sym.dash.en#eq)] }) body }
#show: equation-numbering-rules // Only works if headings are numbered. #set heading(numbering: "1.") #set page(width: auto, height: auto) $ sin x $ <eq:sin> @eq:sin is the sine function. = Chapter $ cos x $ @eq:sin is still the sine function.
-
https://typst.app/universe/package/i-figured
Configurable figure and equation numbering per section.
Caveat:
ref
is not supported.
Relevant issues
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example