Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/preview/numera/0.0.1/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
23 changes: 23 additions & 0 deletions packages/preview/numera/0.0.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# numera

Per-chapter figure and equation numbering, subfigure numbering, numbering functions that can render differently for references, equate package compatibility.

See [example](tests/example/test.typ) and [equate example](tests/compatibility-equate/test.typ) for usage.

## Development

```bash
cargo install --locked typst-cli
cargo install --locked tytanic
cargo install --locked typstyle
cargo install --git https://github.com/typst/package-check.git
cargo install --git https://github.com/sjfhsjfh/typship.git

typship login universe

export TYPST_PACKAGE_PATH=$PWD/packages
typst-package-check check
typstyle --check .
tt run
typship publish universe
```
160 changes: 160 additions & 0 deletions packages/preview/numera/0.0.1/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#let equate-sub-numbering-state = state("equate/sub-numbering", false)

#let counting-symbols = "1aAiIαΑ一壹あいアイא가ㄱ*١۱१১ক①⓵"
#let non-counting = "[^" + counting-symbols + "]"
#let pattern = regex("^" + non-counting + "*(.*?)" + non-counting + "*$")

#let trim-numbering(s) = s.match(pattern).captures.at(0)

#let patch-numbering(the-numbering, ref: false) = {
if the-numbering == none {
none
} else if type(the-numbering) == str {
if ref {
trim-numbering(the-numbering)
} else {
the-numbering
}
} else {
the-numbering.with(ref: ref)
}
}

#let my-numbering(the-numbering, ref: false, ..nums) = {
numbering(patch-numbering(the-numbering, ref: ref), ..nums)
}

#let get-numbering(target, ref: false, location: none) = {
if location == none {
location = here()
}
patch-numbering(
query(selector(target).before(location))
.last(default: (numbering: none))
.numbering,
ref: ref,
)
}

#let display-numbering(target, ref: false) = {
let numbering = get-numbering(target, ref: ref)
if numbering == none {
return none
}
counter(target).display(numbering)
}

#let normal-figure = (
figure
.where(kind: image)
.or(figure.where(kind: table))
.or(figure.where(kind: raw))
)

#let outer-figure-counter-value() = (
query(selector(normal-figure).before(here())).last().counter.get()
)

#let numera(level: 0) = it => {
show heading: it => {
if it.level <= level {
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
}

show normal-figure: outer => {
counter(figure.where(kind: "subfigure")).update(0)

show figure.where(kind: "subfigure"): set figure(
supplement: outer.supplement,
)

outer
}

// imitates default show rule but passes (ref: true) to numbering
show ref: it => {
if it.element == none or it.element.func() != math.equation { return it }
let here = here()
let location = it.element.location()
assert(here != location)
let rendered = counter(math.equation).display(
patch-numbering(it.element.numbering, ref: true),
at: location,
)
let result = if it.element.supplement == [] {
rendered
} else {
[#it.element.supplement~#rendered]
}
link(location, result)
}

// imitates default show rule but passes (ref: true) to numbering
show ref: it => {
if it.element == none or it.element.func() != figure { return it }
if it.element.kind not in (image, table, raw, "subfigure") { return it }
let here = here()
let location = it.element.location()
assert(here != location)
let rendered = it.element.counter.display(
patch-numbering(it.element.numbering, ref: true),
at: location,
)
let result = if it.element.supplement == [] {
rendered
} else {
[#it.element.supplement~#rendered]
}
link(location, result)
}

// equate compatibility for (ref: true) and correct location context
// TODO upstream the display(at: ) as that should already fix quite a bit.
// or rather try refactoring upstream so the numbering function can retrieve this value.
show ref: it => {
if it.element == none { return it }
if it.element.func() != figure { return it }
if it.element.kind != math.equation { return it }
if it.element.body == none { return it }
if it.element.body.func() != metadata { return it }

let nums = if equate-sub-numbering-state.at(it.element.location()) {
it.element.body.value
} else {
(
it.element.body.value.first()
+ it.element.body.value.slice(1).sum(default: 1)
- 1,
)
}

assert(
it.element.numbering != none,
message: "cannot reference equation without numbering.",
)

let here = here()
let location = it.element.location()
assert(here != location)
let rendered = it.element.counter.display(
(..) => numbering(
patch-numbering(it.element.numbering, ref: true),
..nums,
),
at: location,
)
let result = if it.element.supplement == [] {
rendered
} else {
[#it.element.supplement~#rendered]
}
link(location, result)
}

it
}
135 changes: 135 additions & 0 deletions packages/preview/numera/0.0.1/tests/compatibility-equate/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#import "@preview/equate:0.3.3": equate
#import "@preview/numera:0.0.1": (
display-numbering, get-numbering, my-numbering, normal-figure, numera,
outer-figure-counter-value,
)

#show: equate.with(sub-numbering: true, number-mode: "line")
#show: numera(level: 1)

#set math.equation(numbering: (ref: false, ..nums) => {
let heading = display-numbering(heading, ref: ref)
if heading != none {
heading += "."
}
heading + my-numbering("(1.1)", ref: ref, ..nums)
})

#show normal-figure: set figure(numbering: (ref: false, ..nums) => {
let heading = display-numbering(heading, ref: ref)
if heading != none {
heading += "."
}
heading + my-numbering("(1)", ref: ref, ..nums)
})

= Test 1

$ 1 + 1 $ <eq1-1>

#show figure.where(kind: "subfigure"): set figure(numbering: (
ref: false,
..nums,
) => {
let outer-count = outer-figure-counter-value()
let heading = display-numbering(heading, ref: ref)
if heading != none {
heading += "."
}
heading + my-numbering("(S1a)", ref: ref, ..outer-count, ..nums)
})

#figure(
[
F1.1
#figure("S1.1.a", caption: "S1.1.a", kind: "subfigure") <s1-1-a>
#figure("S1.1.b", caption: "S1.1.b", kind: "subfigure") <s1-1-b>
],
caption: "F1.1",
) <fig1-1>


== Subtitle
$ 1 + 1 #<eq1-2a> \ 2 + 2 #<eq1-2b> $ <eq1-2>
#figure("F1.2", caption: "F1.2") <fig1-2>

See @eq1-1, @eq1-2, @eq1-2a, @eq1-2b, @eq2-1, @eq2-2, @eq3-1, @eq3-2, @eq4-1, @eq4-2, @eq4-2a, @eq4-2b

See @fig1-1, @s1-1-a, @s1-1-b, @fig1-2, @fig2-1, @fig2-2, @fig3-1, @fig3-2, @fig4-1, @s4-1-a, @s4-1-b, @fig4-2

#set heading(numbering: "[A.A]")

= Appendix

$ 1 + 1 $ <eq2-1>
#figure("F2.1", caption: "F2.1") <fig2-1>
$ 1 + 1 $ <eq2-2>
#figure("F2.2", caption: "F2.2") <fig2-2>

See @eq1-1, @eq1-2, @eq1-2a, @eq1-2b, @eq2-1, @eq2-2, @eq3-1, @eq3-2, @eq4-1, @eq4-2, @eq4-2a, @eq4-2b

See @fig1-1, @s1-1-a, @s1-1-b, @fig1-2, @fig2-1, @fig2-2, @fig3-1, @fig3-2, @fig4-1, @s4-1-a, @s4-1-b, @fig4-2


#set math.equation(numbering: (ref: false, ..nums) => {
let heading = display-numbering(heading, ref: ref)
if heading != none {
heading += "-"
}
heading + my-numbering("(1.1)", ref: ref, ..nums)
})

#show normal-figure: set figure(numbering: (ref: false, ..nums) => {
let heading = display-numbering(heading, ref: ref)
if heading != none {
heading += "-"
}
heading + my-numbering("(1)", ref: ref, ..nums)
})

#show figure.where(kind: "subfigure"): set figure(numbering: (
ref: false,
..nums,
) => {
let outer-count = outer-figure-counter-value()
let heading = display-numbering(heading, ref: ref)
if heading != none {
heading += "."
}
heading + my-numbering("(X1a)", ref: ref, ..outer-count, ..nums)
})

#set math.equation(supplement: "Eq")
#set figure(supplement: "Fig")

= Test 1

$ 1 + 1 \ 2 + 2 $ <eq3-1>
#figure("F3.1", caption: "F3.1") <fig3-1>
$ 1 + 1 $ <eq3-2>
#figure("F3.2", caption: "F3.2") <fig3-2>

See @eq1-1, @eq1-2, @eq1-2a, @eq1-2b, @eq2-1, @eq2-2, @eq3-1, @eq3-2, @eq4-1, @eq4-2, @eq4-2a, @eq4-2b

See @fig1-1, @s1-1-a, @s1-1-b, @fig1-2, @fig2-1, @fig2-2, @fig3-1, @fig3-2, @fig4-1, @s4-1-a, @s4-1-b, @fig4-2

#set heading(numbering: "[I.I]")

= Appendix

$ 1 + 1 $ <eq4-1>
#figure(
[
F4.1
#figure("S4.1.a", caption: "S4.1.a", kind: "subfigure") <s4-1-a>
#figure("S4.1.b", caption: "S4.1.b", kind: "subfigure") <s4-1-b>
],
caption: "F4.1",
) <fig4-1>
== Subtitle
$ 1 + 1 #<eq4-2a> \ 2 + 2 #<eq4-2b> $ <eq4-2>
#figure("F4.2", caption: "F4.2") <fig4-2>

See @eq1-1, @eq1-2, @eq1-2a, @eq1-2b, @eq2-1, @eq2-2, @eq3-1, @eq3-2, @eq4-1, @eq4-2, @eq4-2a, @eq4-2b

See @fig1-1, @s1-1-a, @s1-1-b, @fig1-2, @fig2-1, @fig2-2, @fig3-1, @fig3-2, @fig4-1, @s4-1-a, @s4-1-b, @fig4-2
Loading
Loading