Figure and table captions with chapter number

Hi,

I managed to create a demo on using counters to display figure and table captions with numbering. Here is the link:

https://typst.app/project/rHVlsLUqcZ5zRT26hUfZFB

I paste the code here too

#set heading(numbering: "1.")
#let fig = counter("figure")
#let tab = counter("table")

#let fig_numbering(.., desc) = {
  fig.step()
  context str(counter(heading).get().at(0)) + "-" + context fig.display()
}

#let tab_numbering(.., desc) = {
  tab.step()
  context str(counter(heading).get().at(0)) + "-" + context tab.display()
}

#show figure.where(
  kind: table
): set figure.caption(position: top)


= Introduction

== Background

The current value is: #context counter(heading).display() \
Heading 1 value = #context counter(heading).get().at(0) \
Heading 1 in roman numerals: #context counter(heading).display("I")

#figure(
  image("circles.png"),
  caption: "Drawing circles in AutoCAD",
  numbering: fig_numbering
)

#figure(
  table(
    columns: 4,
    [t], [1], [2], [3],
    [y], [0.3s], [0.4s], [0.8s],
  ),
  caption: [Timing results],
  numbering: tab_numbering
)

== More Background

#figure(
  image("circles.png"),
  caption: "Drawing circles in AutoCAD",
  numbering: fig_numbering
)

= Theory
#counter("figure").update(0)
#counter("table").update(0)

== More Theory

#figure(
  image("circles.png"),
  caption: "Drawing circles in AutoCAD",
  numbering: fig_numbering
)

#figure(
  table(
    columns: 4,
    [t], [1], [2], [3],
    [y], [0.3s], [0.4s], [0.8s],
  ),
  caption: [Timing results],
  numbering: tab_numbering
)

2 Likes

If you start your code block with ```typst then it will have proper syntax highlighting. So the following block

```typst
#set text(weight: "bold")
This text is bold
```

will be rendered as

#set text(weight: "bold")
This text is bold

EDIT
At a second glance I figure you used a quote instead of a codeblock. Please prefer code blocks for actual code as it makes it easier for people to read. Especially if you use some indents for blocks of code.

You accidentally included the image in the codeblock.

1 Like

Hi Yoonghm, thanks for sharing your code. I’m targeting Heading Level 2, so changed it for that (shown below). I’m going to try to reset the counters for figures after each new Heading 2, but realised the Outline isn’t honouring the new numbering (e.g. 1.1, 1.2, 3.3) but instead showing 0.1, 0.2, 0.3 - obvs incrementing the Figure number, and ignoring the Heading prefix. Have you needed to overcome this yourself?

context str(counter(heading.where(level: 2)).display()) + "-" + context tab.display()

Hi @Hi.as.MyKite, could I ask you to post this as a new question? I think there have been some relevant changes in Typst since this showcase, and a new post will give your issue more visibility.

One thing I can tell you though:

counter(heading.where(level: 2))

There is only the counter(heading), and that counter counts all levels of heading. This is probably the immediate reason why you always get zeros instead of the number you want: the counter you use is not used anywhere else.

Yes, I will - hadn’t realised the orig post was under showcase. I found an updated method on the forum, which was very simple to implement and control - I’ll update the posts once back at my laptop.

Just chiming in to mention that this topic was discussed in the (currently open) GitHub issue #1896.

There, a comment posted what is, in my opinion, the best solution:

// show chapter on figure numbering
#set figure(numbering: (..num) =>
  numbering("1.1", counter(heading).get().first(), num.pos().first())
)

// show chapter on equation numbering
#set math.equation(numbering: (..num) =>
  numbering("(1.1)", counter(heading).get().first(), num.pos().first())
)

#show heading.where(level: 1): it => {
  // reset figure counters so they are counted per chapter
  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)
    
  // ...
}

Also mention that there is the i-figured package does the same thing, and gives you more options and a cleaner configuration syntax, but uses custom i-figured figures, which may clash with the rest of the config you have for figures.