How to change the figure caption format in bookdown

While there is no method to style figure caption prefixes via bookdown, it is possible to do this by applying a custom Lua filter (which requires pandoc 2.0 or later).

Lua-Filters

Assuming that you are starting from

Figure 1 Text of figure caption.

the following filter should do what you want (see https://pandoc.org/lua-filters.html#inline for additional formatting options):

function Image (img)
  img.caption[1] = pandoc.Strong(img.caption[1])
  img.caption[3] = pandoc.Strong(pandoc.Str(string.gsub(img.caption[3].text, ":", ".")))
  return img
end

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

As another example, if instead you are starting from

Figure 1 Text of figure caption.

the following filter should do what you want (see https://pandoc.org/lua-filters.html#inline for additional formatting options):

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)

Applying a Lua-filter

Assuming you place this filter in a file called figure_caption_patch.lua in the directory of your rmarkdown document 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"]

This should yield the desired caption style.

Figure 1. Text of figure caption.

Leave a Comment