Arief's Blog

Create your own Typst’s custom figure type

In Typst, figure is an element in Typst that allows you to put caption to your image, table, or code. It’s a simple yet powerful feature that can help you to create a better reading experience for your readers.

For example, here’s how you can create a figure for an image:

#figure(
  image("glacier.jpg", width: 80%),
  caption: [A curious figure.],
)

Which will output:

Glacier figure

This will also automatically create a table of figures at the end of your article with its own numbering and page reference.

However, you might want to create your own custom figure type with different prefix, especially if you are writing in a different language than English. For example, when writing in Indonesian, you might want to use “Gambar” instead of “Figure”.

You can achieve this feat by setting kind and supplement parameters in the figure function:

#figure(
  image("calculator.png", width: 75%),
  caption: [Tampilan webapp kalkulator sederhana],
  kind: "gambar",
  supplement: "Gambar",
)

Which will output:

Calculator

We can continue improving this by creating a function that can wrap the figure function with the custom kind and supplement parameters:

#let gambar(body, caption) = {
  figure(
    body,
    caption: caption,
    kind: "gambar",
    supplement: "Gambar",
  )
}

#gambar(
  image("calculator.png", width: 75%),
  [Tampilan webapp kalkulator sederhana],
)