Skip to content

Custom formatting for figure and table captions #929

@wjakethompson

Description

@wjakethompson

This question was originally posted on Stack Overflow and was recommended for a feature request here.

By default, bookdown formats figure and table captions as with a colon (:) between the figure/table number and the caption:

Figure 2.1: Here is a nice figure!

As noted by Yihui on Stack Overflow, this is hardcoded by bookdown in parse_fig_labels().

bookdown/R/html.R

Lines 647 to 672 in 5782b2f

switch(type, fig = {
if (length(grep('^<p class="caption', content[i - 0:1])) == 0) {
# remove these labels, because there must be a caption on this or
# previous line (possible negative case: the label appears in the alt
# text of <img>)
labs[[i]] = character(length(lab))
next
}
labs[[i]] = paste0(label_prefix(type), num, ': ')
k = max(figs[figs <= i])
content[k] = paste(c(content[k], sprintf('<span id="%s"></span>', lab)), collapse = '')
}, tab = {
if (length(grep('^<caption', content[i - 0:1])) == 0) next
labs[[i]] = sprintf(
'<span id="%s">%s</span>', lab, paste0(label_prefix(type), num, ': ')
)
}, eq = {
labs[[i]] = sprintf('\\tag{%s}', num)
k = max(eqns[eqns <= i])
content[k] = sub(
'(<span class="math display")', sprintf('\\1 id="%s"', lab), content[k]
)
}, {
labs[[i]] = paste0(label_prefix(type), num, ' ')
})
}

It would be nice to be able to control the separator (e.g., use a . or <br/> if we wanted the figure/table number and caption on separate lines).

Relatedly (but maybe a separate issue), it would also be useful to be able to apply CSS to figure/table numbers and captions separately (e.g., bold figure number and italic caption, as required by APA).

Currently, figure captions are all inside a single paragraph tag (e.g., Figure 2.1 here), so it's not possible to apply styling to the elements separately.

<p class="caption">
  Figure 2.1: Here is a nice figure!
</p>

Tables put the table number inside a <span>, so it's possible to style the two pieces separately with CSS in HTML output.

<caption>
  <span id="tab:nice-tab">Table 2.1: </span>
  Here is a nice table!
</caption>

For both figure and table captions in LaTeX output, the {caption} package can be used to apply styling to the individual pieces, as well as set the separator.


By filing an issue to this repo, I promise that

  • I have fully read the issue guide at .
  • I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included . I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: .
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

Activity

cderv

cderv commented on Mar 30, 2021

@cderv
Collaborator

Thanks for the suggestion.

For later reference, this is related to #855 for more flexibility regarding label and numbering.

cderv

cderv commented on Mar 31, 2021

@cderv
Collaborator

For reference, officedown::rdocx_document() offers an argument to change table caption: https://davidgohel.github.io/officedown/articles/captions.html#sequence-options-1

not for the same purpose but worth noting that it exists.

tjmahr

tjmahr commented on Aug 11, 2021

@tjmahr

One related issue: I use the figure plotting hook to use an html 5 figure/figcaption tags. I can include the figure-id in the hook, but bookdown's figure numbering misses these cases because the regex looks for a paragraph tag. It would be nice to support <figcaption> too.

cderv

cderv commented on Aug 16, 2021

@cderv
Collaborator

@tjmahr can you open a specific feature request for this so we can look into into it and track separately ? Thank you !

added a commit that references this issue on May 7, 2023
e0c3cea
yihui

yihui commented on May 7, 2023

@yihui
Contributor

It would be nice to be able to control the separator (e.g., use a . or <br/> if we wanted the figure/table number and caption on separate lines).

The format is no longer hardcoded and can be customized since bookdown 0.22 (PR #1120). Documentation: https://bookdown.org/yihui/bookdown/internationalization.html Below is an example.

First create _bookdown.yml:

language:
  label:
    fig: !expr function(i) paste0('Figure ', i, '<br/> ')

Then an Rmd file:

---
title: "Test"
output:
  bookdown::html_document2
---

# Section one

```{r, fig.cap='A nice figure.'}
plot(1:10)
```

Relatedly (but maybe a separate issue), it would also be useful to be able to apply CSS to figure/table numbers and captions separately

That should indeed be a separate issue. It should be easy to implement (#1428) but I'm not sure if would break anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @yihui@tjmahr@cderv@wjakethompson

      Issue actions

        Custom formatting for figure and table captions · Issue #929 · rstudio/bookdown