2

Default R Markdown captions in Word appear as:

  • Figure 1: Figure Title
  • Table 1: Table Title

I would like to format my captions so that the Figure 1/Table 1 is bold and the title is italic on the next line. This is the APA style of captions:

Figure 1

Figure Title

I have been trying to figure out an easy way to do this, but can't seem to find a solution. (If figure captions can be above figure, rather than below, that would also be great!). Here is a minimal reprex:

---
title: "Untitled"
author: "Test"
date: "11/8/2020"
output:
  bookdown::word_document2: default
---

{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(flextable)




{r table, tab.cap="Descriptives", echo=FALSE, message=FALSE, warning=FALSE}
psych::describe(cars) %>%
  as.data.frame() %>%
  flextable() %>%
  set_table_properties(layout = "autofit", width = 1)


{r fig, fig.cap="Pressure Figure", echo=FALSE, message=FALSE, warning=FALSE}
plot(pressure)
neilfws
  • 32,751
  • 5
  • 50
  • 63
acircleda
  • 673
  • 4
  • 13
  • Directly related (duplicate?): [How to change the figure caption format in bookdown](https://stackoverflow.com/questions/51945079/how-to-change-the-figure-caption-format-in-bookdown); bottom line: there's no easy solution. Have you considered moving away from Word templates and using LaTeX instead (within RMarkdown)? These type of customisations are usually a lot easier in LaTeX. – Maurits Evers Nov 09 '20 at 01:36

1 Answers1

0

For tables you can do it with officedown, officer and flextable packages, styling in reference_doxc, and by taking the advantage of "\n\n" for tables.

In yaml in the pre argument you set up a prefix for every table and a separator between the number and the title itself.

---
title: "Untitled"
author: "Test"
date: "11/8/2020"
output:
  officedown::rdocx_document:
    tables:
      caption:
        pre: 'Table  '
        sep: ''
---

With

tab.cap.fp_text = officer::fp_text_lite()

you can define the formatting of the caption prefix. In APA style it would be bold without italics, preferrably configured in the global knitr options, e.g.

knitr::opts_chunk$set(tab.cap.fp_text = officer::fp_text_lite(italic = FALSE, bold = TRUE))

And you add

"\n\n"

to the table caption to produce a new line

{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, tab.cap.fp_text = officer::fp_text_lite(italic = FALSE, bold = TRUE))
library(tidyverse)
library(flextable)


{r table, tab.cap="\n\nDescriptives", echo=FALSE, message=FALSE, warning=FALSE}
psych::describe(cars) %>%
as.data.frame() %>%
flextable() %>%
set_table_properties(layout = "autofit", width = 1)

The last thing is to create a reference_doxc in which you add italics to the table caption style. The caption prefix is not affected, as you control it with the knitr options.

Caban
  • 25
  • 4