Skip to content

Commit

Permalink
use the element's counter for references
Browse files Browse the repository at this point in the history
a bit sad that we can't have the auto-final-dot-removing behavior though
  • Loading branch information
PgBiel committed Jan 10, 2025
1 parent fb25e80 commit 5ec35bf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
27 changes: 14 additions & 13 deletions src/element.typ
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@
let lbl-show = label(lbl-show-head + eid)
let ref-figure-kind = if reference == none { none } else { lbl-ref-figure-kind-head + eid }
// Use same counter as hidden figure for ease of use
let counter-key = if reference == none { lbl-counter-head + eid } else { figure.where(kind: ref-figure-kind) }
let counter-key = lbl-counter-head + eid
let element-counter = counter(counter-key)
let count = if count == none { none } else { count(element-counter) }
let count-needs-fields = type(count) == function
Expand Down Expand Up @@ -1519,6 +1519,18 @@
)

let ref-figure = (tag-metadata, synthesized-fields) => {
let numbering = if numbering-type == str {
reference.numbering
} else if numbering-type == function {
let numbering = (reference.numbering)(synthesized-fields)
assert(type(numbering) in (str, function), message: "element: 'reference.numbering' must be a function fields => numbering (a string or a function), but returned " + str(type(numbering)))
numbering
} else {
none
}

let number = element-counter.display(numbering)

let ref-figure = [#figure(
supplement: if supplement-type in (str, content, none) {
[#reference.supplement]
Expand All @@ -1528,15 +1540,7 @@
[]
},

numbering: if numbering-type == str {
reference.numbering
} else if numbering-type == function {
let numbering = (reference.numbering)(synthesized-fields)
assert(type(numbering) in (str, function), message: "element: 'reference.numbering' must be a function fields => numbering (a string or a function), but returned " + str(type(numbering)))
numbering
} else {
none
},
numbering: _ => number,

kind: ref-figure-kind,
)[#[]#tag-metadata#lbl-tag]#ref-label]
Expand All @@ -1545,9 +1549,6 @@

show figure: none

// Ensure we only step as much as 'count' allows
counter(figure.where(kind: ref-figure-kind)).update(n => n - 1)

tagged-figure
}

Expand Down
4 changes: 2 additions & 2 deletions test/unit/elements/reference/contextual/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
field("color", color, default: red),
field("inner", content, default: [Hello!]),
field("supplement", e.types.option(content), default: [Wock]),
field("numbering", e.types.option(e.types.union(str, function)), default: "1."),
field("numbering", e.types.option(e.types.union(str, function)), default: "1"),
),
reference: (
supplement: it => it.supplement,
Expand All @@ -26,6 +26,6 @@

#wock(color: blue, label: <my-wock>)
#wock(color: blue, label: <my-wock2>, supplement: [def])
#wock(color: blue, label: <my-wock3>, supplement: [def], numbering: "a)")
#wock(color: blue, label: <my-wock3>, supplement: [def], numbering: "a")

@my-wock ; @my-wock2 ; @my-wock3
2 changes: 1 addition & 1 deletion test/unit/elements/reference/simple/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
),
reference: (
supplement: [Wock],
numbering: "1."
numbering: "1"
),
prefix: ""
)
Expand Down

0 comments on commit 5ec35bf

Please sign in to comment.