Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimize self references #13

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
26 changes: 26 additions & 0 deletions src/lib.typ
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@
show-sub = _pkg.t4t.def.if-auto(it => it, show-sub)
show-sub-caption = _pkg.t4t.def.if-auto((num, it) => it, show-sub-caption)

show figure.caption: it => {
show ref: it => {
let is-self-ref = (
it.element != none
and type(it.element) == content
and it.element.func() == figure
and ({
// this is brittle and may fail if users reset numberings
let ctr = counter(figure.where(kind: it.element.kind))
ctr.at(it.element.location()).first() - 1 == ctr.at(here()).first()
})
)

// if we reference a sub figure in our own figure we discard its
// supplement and use only the sub figure numbering
if is-self-ref {
let super-n = counter(figure.where(kind: it.element.kind)).at(it.element.location()).first()
let sub-n = sub-figure-counter.at(it.element.location()).first() + 1
_numbering(numbering-sub, super-n, sub-n)
} else {
it
}
}
it
}

context {
let n-super = counter(figure.where(kind: kind)).get().first() + 1

Expand Down
Binary file added test/super-caption-sub-ref/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions test/super-caption-sub-ref/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Synopsis:
// - references to sub figures within the caption of their own super super only
// display their numbering

#import "/test/util.typ": *
#import "/src/lib.typ" as subpar

#outline(target: figure)

#subpar.grid(
figure(fake-image, caption: [Inner Caption]), <1a>,
figure(fake-image, caption: [Inner Caption]), <1b>,
columns: (1fr, 1fr),
caption: [Super, consisting of @1a and @1b.],
label: <1>,
)

#subpar.grid(
figure(fake-image, caption: [Inner Caption]),
figure(fake-image, caption: [Inner Caption]),
columns: (1fr, 1fr),
caption: [Another super, referencing @1a and @1b, as well as @2a and @2b.],
)

#subpar.grid(
kind: raw,
figure(```typst = Hello World```, caption: [Inner Caption]), <2a>,
figure(```typst = Hello World```, caption: [Inner Caption]), <2b>,
columns: (1fr, 1fr),
caption: [Another super, consiting of @2a and @2b and referencing @1a and @1b.],
label: <2>,
)
Loading