How to change the figure/table caption style in bookdown

Hello, I'd like to follow a question:

The desired caption style required by most of journals is:

Figure 1. Text of figure caption

There is a very close solution proposed by @crsh, which is to apply a custom Lua filter:

function Image (img)
  img.caption[1] = pandoc.Strong(img.caption[1])
  img.caption[3] = pandoc.Strong(img.caption[3])
  img.caption[4] = pandoc.Strong(".  ")
  return img
end

(Assuming that img.caption[2] is the white space between Figure and the number and img.caption[4] is the white space between the number and the caption)

Place this filter in a file called figure_caption_patch.lua in the directory of the rmarkdown document so that you can apply it by adding a pandoc argument in the YAML front matter:

output:
  bookdown::word_document2:
    pandoc_args: ["--lua-filter", "figure_caption_patch.lua"]

However, this solution couldn't get the desired style anymore. Instead, it returns:

Figure 1:. Text of figure caption

I was wondering, is it possible to modify the Lua filter to achieve the desired style?

Your kind guidance is much appreciated!

I think you get this behavior because we changed the output to have a : as separator by default. It was suppose to be there and it was not. It is possible that we revert back this change because you are not the first one to be impacted by this.

However, you can now do that with R directly, no need of Lua.

You can add this in a _bookdown.yml file:

language:
  label:
    fig: !expr function(x) sprintf("**Figure %s.** ", x)

This is documented in 4.5 Internationalization | bookdown: Authoring Books and Technical Documents with R Markdown and supported since bookdown 0.22

As you see you can pass Markdown syntax to obtain the Strong type.

Would that work for you ?

Otherwise, you we need to replace the sep : if it exists using Lua function. I guess indexing by number is not robust to any change in the default caption. (string.gsub should work in Lua for example)

1 Like

Thank you very much @cderv!

I just updated the bookdown package but didn't get the desired style. What I got is still the default style:

Figure 1: This is a figure.

May I ask what is the possible reason? How about the output is a single word document? This is my YAML head:

title: "caption style test"
language:
  label:
    fig: !expr function(x) sprintf("**Figure %s.** ", x)
output: bookdown::word_document2

This is not to be set in YAML. As I said:

This is to be set in a _bookdown.yml file which is picked up by a bookdown project. Usually this is used with a book project, but it works also with a single document format like bookdown::word_document2

Unfortunately We don't yet support bookdown: key or else in YAML header.

Done! I have been struggling with the caption style for a long time. Finally, it's perfectly solved. Thank you so much @cderv!

In addition, may I ask, is it possible to set different styles in different chapters?

That is, in the main chapters, the style is:

Figure 1. This is a figure.

while in the supplementary chapter, the style is:

Figure S1. This is a supplementary figure.

Many thanks for your kind guidance!

No I don't think this is currently possible. The configuration is for the whole book. Mainly because you usually want same style on the whole book.

You would need to come with a post processing way of tweaking this is you want to follow this path.

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

Get it. Thank you very much @cderv.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.