rstudio / bookdown

Authoring Books and Technical Documents with R Markdown
https://pkgs.rstudio.com/bookdown/
GNU General Public License v3.0
3.78k stars 1.27k forks source link

Custom formatting for figure and table captions #929

Open wjakethompson opened 4 years ago

wjakethompson commented 4 years ago

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().

https://github.com/rstudio/bookdown/blob/5782b2ffaeea5d6ee7f34562b596d55f70824192/R/html.R#L647-L672

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 understand that my issue may be closed if I don't fulfill my promises.

cderv commented 3 years ago

Thanks for the suggestion.

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

cderv commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

yihui commented 1 year ago

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.