#Figure and table numbering

74 messages · Page 1 of 1 (latest)

hollow cypress
#

Right now my figures and tables are automatically numbered "Figure 1" ,"Figure 2" etc. I would like the number to reflect what section it is in, i.e. if it is the third figure in chapter 5, it should be "Figure 5.3". How can I accomplish this?

chrome mesa
#

figure has a few options at your disposal

#

for example, numbering: accepts a function which lets you entirely customize the number

#

the function takes the number of the figure as an argument

#

and returns [content] with the text to display

#

to have what you're looking for in an automatic manner, you'll probably need to use a custom counter

#

e.g. counter: counter("figcounter") <--- this is also an option you can give to figure

#

and then do something like

#show heading.where(level: 1: it => {
   counter("figcounter").update((counter(heading), 1))
it
})```
#

to reset the counter on every level 1 heading (for example)

#

actually I think just the counter is necessary

#

let me give this a shot:

#

?render

#set heading(numbering: "1.")
#show heading.where(level: 1): it => {
counter(figure.where(kind: "figcounter")).update(0)
it
}
#let fignumbering = n => locate(loc => {
  let section = counter(heading).at(loc).map(str).join(".")

  [#section.#n]
})

= My section

#figure(supplement: "Figure", kind: "figcounter", numbering: fignumbering, caption: [Test])[a]

#figure(supplement: "Figure", kind: "figcounter", numbering: fignumbering, caption: [Test])[b]

= Other

#figure(supplement: "Figure", kind: "figcounter", numbering: fignumbering, caption: [Test])[a]
#figure(supplement: "Figure", kind: "figcounter", numbering: fignumbering, caption: [Test])[b]
chrome mesa
#

oh ok you cant define the counter

#

you actually have to set the figure 'kind'

#

ok hold on

#

there we go @hollow cypress

#

if you want to avoid this "supplement", "kind" business, you can reset the counter for all figures on each heading

#

?render

#set heading(numbering: "1.")
#show heading.where(level: 1): it => {
counter(figure.where(kind: table)).update(0)
it
}
#let fignumbering = n => locate(loc => {
  let section = counter(heading).at(loc).map(str).join(".")

  [#section.#n]
})

= My section

#figure(numbering: fignumbering, caption: [Test], table[a])

#figure(numbering: fignumbering, caption: [Test], table[a])

= Other

#figure(numbering: fignumbering, caption: [Test], table[b])
#figure(numbering: fignumbering, caption: [Test], table[c])
chrome mesa
#

like that I changed it to reset for tables only

#

at the top

#

note that the counter isnt reset on sub-headings; you can override this behavior by removing the .where(level: 1) from the heading selector at the top

#

like so:

#

?render

#set heading(numbering: "1.")
#show heading: it => {
counter(figure.where(kind: table)).update(0)
it
}
#let fignumbering = n => locate(loc => {
  let section = counter(heading).at(loc).map(str).join(".")

  [#section.#n]
})

= My section

#figure(numbering: fignumbering, caption: [Test], table[a])

#figure(numbering: fignumbering, caption: [Test], table[a])

== Subsection

#figure(numbering: fignumbering, caption: [Test], table[a])

#figure(numbering: fignumbering, caption: [Test], table[a])

= Other

#figure(numbering: fignumbering, caption: [Test], table[b])

== Sub
#figure(numbering: fignumbering, caption: [Test], table[a])

=== Sub sub
#figure(numbering: fignumbering, caption: [Test], table[c])

#figure(numbering: fignumbering, caption: [Test], table[a])
chrome mesa
#

there you go

#

I think this ^ is ultimately what you're looking for

hollow cypress
#

@chrome mesa you're amazing. thank you!

chrome mesa
#

np 👍

hollow cypress
#

It is working well, but I have a problem that when I use

#

#outline(
title: [List of Figures],
target: figure.where(kind: image),
)

#

at the top, the links to the figures are labeled as Figure 0.1 etc.

#

@chrome mesa any ideas?

chrome mesa
#

I think it's because like

#

it's taking the numbering function to the outline

#

and so the location is set to before any heading is placed

#

so it goes to 0

#

there really needs to be some sort of thing in typst where it saves the final content of something and just takes it to the outline

#

or at least saves the location

#

@hollow cypress can you open an issue on GitHub?

#

maybe we can try to work on a fix or smth

hollow cypress
#

will do

#

thanks for your help

chrome mesa
#

no prob

#

so

#

I think I see the problem in the internal code, but it seems that it'd be kinda annoying to fix maybe

#

so this is the function which figure uses to determine what to display on the outline

#

it shows caption with the given vt where vt is the context

#

and this is called from the outline element

#

with the outline's context

#

so indeed

#

I think it's using the outline's context

#

but this doesn't seem easy to fix at all

#

lol

#

cuz it has to use some context

#

ideally it would cache the context somehow

#

from where it's placed and take it to the outline on the next redraw

#

but not sure how to do that in the best way

#

anyways

#

this is something that I will comment on the issue when it is opened

#

well to say that vt is the "context" is a bit of an oversimplification as well

#

cuz it has introspection capabilities which allow accessing things outside the current context

#

but still I think the issue is something along those lines

#

ok

#

I think I am seeing the light

#

or maybe not

#

well I could try something

#

@hollow cypress can u send ur sample document later? I wanna do some experiments

chrome mesa
#

ok

#

I am not seeing the light