DeltaIV
DeltaIV

Reputation: 5646

Changing the font size of figure captions in RMarkdown Word output

I recently asked

Changing the font size of figure captions in RMarkdown HTML output

and I got a very nice answer which uses this CSS method. I wanted to try the same, but this time with Word output. If you don't want to read my former question, I summarize the issue here: I'd like to make the font size of all figure captions in my R Markdown document smaller. The final output is Word,this time, and I'm working in R Studio. To load the picture, I use the include_graphics function from knitr, because I've been told it's the best way (see here). My .Rmd file is:

---
title: "ppp"
author: "ppp"
date: "`r Sys.Date()`"
output: 
  word_document: 
    fig_caption: yes
  html_document: 
    fig_caption: yes
---

<style>
p.caption {
  font-size: 0.8em;
}
</style>


```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = FALSE)
```


```{r foo, fig.cap="$f_{p}$ as a function of $g$ for various values of $r=\\frac{\\rho_{w}}{\\rho_{a}}$"}
# All defaults
include_graphics("download.jpg")
```

This is regular text.

The corresponding output is:

enter image description here

Clearly, this CSS method doesn't work (I guess it's something related to HTML, so it doesn't render in Word). In Word I can manually change the font size for each caption, but I'd rather set some global R Markdown parameter. Is that possible?

Upvotes: 3

Views: 5351

Answers (1)

Martin Schmelzer
Martin Schmelzer

Reputation: 23889

Almost as easy as in the HTML case. The following applies to the workflow using LibreOffice. But in Word it should be almost the same:

  1. Produce your docx output file.

  2. Open it in LibreOffice (or Word, or Pages, ...)

  3. In LibreOffice, right-click the caption and choose Edit Style (in Word you can open the styles pane with Ctrl+Shift+Alt+S)

enter image description here

  1. In the menu that popped up you can modify the style for Image Captions

enter image description here

  1. When you are done editing the style, click Apply and just save the file as a docx called template.docx

Finally, add a style reference in the YAML header of your Rmd document like

title: "ppp"
author: "ppp"
date: "July 4, 2017"
output: 
  word_document:
    reference_docx: template.docx
    fig_caption: yes

And the captions should be smaller now according to how you changed the style in your reference document.

Upvotes: 4

Related Questions