Skip to content

Commit

Permalink
Fix #1
Browse files Browse the repository at this point in the history
Simplify inner representation which allows for relative numbering.
  • Loading branch information
tingerrr committed Apr 18, 2024
1 parent 7114ae1 commit 2321a03
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 148 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ Above in @full, we see a figure which is composed of 2 other figures, namely @a
![ex]

## Known Issues
- Relative numbering is incorrect for super-figures, see #1
- Supplement is always "Figure", regardless of `text.lang`, see: #2
- Relative numbering is incorrect for super-figures, see [#1]
- Supplement is always "Figure", regardless of `text.lang`, see: [#2]
- Figure show rule cannot be easily reconfigured

## TODO
Expand All @@ -43,3 +43,6 @@ The following tasks remain before the first version of subpar is released:
[ex]: /examples/example.png

[Typst]: https://typst.app/

[#1]: https://github.com/tingerrr/subpar/issues/1
[#2]: https://github.com/tingerrr/subpar/issues/2
3 changes: 2 additions & 1 deletion examples/example.typ
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
]) <b>],
),
caption: [A figure composed of two subfigures.],
) <full>
label: <full>,
)

Above in @full, we see a figure which is composed of 2 other figures, namely @a and @b.
102 changes: 0 additions & 102 deletions src/core.typ
Original file line number Diff line number Diff line change
@@ -1,107 +1,5 @@
#import "util.typ" as _util

#let keys = (
superfigure: "__subpar:superfigure-unprepared",
counter: "__subpar:subfigure-counter",
)

#let subfigure-counter = counter(keys.counter)

#let apply-for-all(
values,
rule,
) = outer => {
show: inner => {
values.map(rule).fold(inner, (acc, f) => f(acc))
}

outer
}

#let super-figure-unprepared(
kind: image,
numbering-sub: "(a)",
numbering-sub-ref: "1a",
numbering-super: "1",
// TODO: somehow resolve this as good as we can for common kinds and for more languages
supplement: [Figure],
caption: none,
placement: none,
gap: 0.65em,
outlined: true,
body,
) = figure(
kind: keys.superfigure,
placement: placement,
caption: caption,
supplement: supplement,
gap: gap,
outlined: outlined,
_util.embed-payload(
kind: kind,
numbering-sub: numbering-sub,
numbering-sub-ref: numbering-sub-ref,
numbering-super: numbering-super,
body
),
)

#let super-figure-prepared(
kind-super: image,
kinds-sub: (image, raw, table),

numbering-sub: "(a)",
numbering-sub-ref: "1a",
numbering-super: "1",

supplement: none,
caption: none,
placement: none,
gap: 0.65em,
outlined-super: true,
outlined-sub: false,
body,
) = {
let n-super = counter(figure.where(kind: kind-super)).get().first() + 1

figure(
kind: kind-super,
numbering: _ => numbering(numbering-super, n-super),
supplement: supplement,
caption: caption,
placement: placement,
gap: gap,
outlined: outlined-super,
{
show: apply-for-all(
kinds-sub,
kind => body => {
show figure.where(kind: kind): set figure(numbering: _ => numbering(
numbering-sub-ref, n-super, subfigure-counter.get().first() + 1
))
body
}
)

set figure(supplement: supplement, outlined: outlined-sub, placement: none)
set figure.caption(separator: none)

show figure: it => {
let n-sub = subfigure-counter.get().first() + 1
show figure.caption: it => {
numbering(numbering-sub, n-sub)
it.separator
[ ]
it.body
}

subfigure-counter.step()
it
counter(figure.where(kind: it.kind)).update(n => n - 1)
}

subfigure-counter.update(0)
body
},
)
}
102 changes: 61 additions & 41 deletions src/lib.typ
Original file line number Diff line number Diff line change
@@ -1,52 +1,72 @@
#import "core.typ"
#import "util.typ"

#let super-figure = core.super-figure-unprepared
#let super-figure(
kind-super: image,
kinds-sub: (image, raw, table),

numbering-sub: "(a)",
numbering-sub-ref: "1a",
numbering-super: "1",

// TODO: somehow resolve this as good as we can for common kinds and for more languages
supplement: [Figure],
caption: none,
placement: none,
gap: 0.65em,
outlined-super: true,
outlined-sub: false,
label: none,
body,
) = context {
let n-super = counter(figure.where(kind: kind-super)).get().first() + 1

[#figure(
kind: kind-super,
numbering: _ => numbering(numbering-super, n-super),
supplement: supplement,
caption: caption,
placement: placement,
gap: gap,
outlined: outlined-super,
{
show: util.apply-for-all(
kinds-sub,
kind => body => {
show figure.where(kind: kind): set figure(numbering: _ => numbering(
numbering-sub-ref, n-super, core.subfigure-counter.get().first() + 1
))
body
}
)

set figure(supplement: supplement, outlined: outlined-sub, placement: none)
set figure.caption(separator: none)

show figure: it => {
let n-sub = core.subfigure-counter.get().first() + 1
show figure.caption: it => {
numbering(numbering-sub, n-sub)
it.separator
[ ]
it.body
}

core.subfigure-counter.step()
it
counter(figure.where(kind: it.kind)).update(n => n - 1)
}

core.subfigure-counter.update(0)
body
},
)#label]
}

#let subpar(
sub-figure-kinds: (image, raw, table),
outline-subfigures: false,
body,
) = {
show figure.where(kind: core.keys.superfigure): it => {
let payload = util.extract-payload(it.body)

core.super-figure-prepared(
kind-super: payload.kind,
kinds-sub: sub-figure-kinds,

numbering-sub: payload.numbering-sub,
numbering-sub-ref: payload.numbering-sub-ref,
numbering-super: payload.numbering-super,

supplement: it.supplement,
caption: if it.caption != none { it.caption.body },
placement: it.placement,
gap: it.gap,
outlined-super: it.outlined,
outlined-sub: outline-subfigures,
it.body,
)
}

// TODO: outline adjustement

show ref: it => {
if util.is-element(it.element, figure) and it.element.kind == core.keys.superfigure {
let payload = util.extract-payload(it.element.body)
link(it.element.location(), {
it.element.supplement
[ ]
numbering(
// TODO: this is executed in the wrong context, which is the only blocker for relative numbering, see #1
payload.numbering-super,
counter(figure.where(kind: payload.kind)).at(it.element.location()).first() + 1
)
})
} else {
it
}
}

body
}
11 changes: 11 additions & 0 deletions src/util.typ
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,14 @@
#let embed-payload(body, ..payload) = {
metadata(payload.named()) + body
}

#let apply-for-all(
values,
rule,
) = outer => {
show: inner => {
values.map(rule).fold(inner, (acc, f) => f(acc))
}

outer
}
Binary file added test/example/chapter-relative/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.
56 changes: 56 additions & 0 deletions test/example/chapter-relative/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#import "/test/util.typ": *

#import "/src/lib.typ": subpar, super-figure
#show: subpar

#let sub-figure-numbering = (super, sub) => numbering("1.1a", counter(heading).get().first(), super, sub)
#let figure-numbering = super => numbering("1.1", counter(heading).get().first(), super)

#set heading(numbering: "1.1")
#show heading.where(level: 1): it => counter(figure.where(kind: image)).update(0) + it
#show figure.where(kind: image): set figure(numbering: figure-numbering)

#let super-figure = super-figure.with(
numbering-sub-ref: sub-figure-numbering,
numbering-super: figure-numbering,
)

#outline(target: figure.where(kind: image))

= Chapter

#figure(fake-image, caption: [aaa])

#super-figure(
grid(columns: (1fr, 1fr),
[#figure(fake-image, caption: [Inner caption]) <a>],
[#figure(fake-image, caption: [Inner caption]) <b>],
),
caption: [Outer caption],
label: <full1>,
)

#figure(
fake-image,
caption: [aaa],
)

#super-figure(
grid(columns: (1fr, 1fr),
[#figure(`adas`, caption: [Inner caption]) <c>],
[#figure(fake-image, caption: [Inner caption]) <d>],
),
caption: [Outer caption],
label: <full2>,
)

= Another Chapter

#figure(
fake-image,
caption: [aaa],
)

See @full1, @a and @b.

See also @full2, @c and @d.
File renamed without changes
6 changes: 4 additions & 2 deletions test/example/test.typ → test/example/default/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
[#figure(fake-image, caption: [Inner caption]) <b>],
),
caption: [Outer caption],
) <full1>
label: <full1>,
)

#figure(
fake-image,
Expand All @@ -26,7 +27,8 @@
[#figure(fake-image, caption: [Inner caption]) <d>],
),
caption: [Outer caption],
) <full2>
label: <full2>,
)

#figure(
fake-image,
Expand Down

0 comments on commit 2321a03

Please sign in to comment.