Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

r - Changing the font size of figure captions in RMarkdown HTML output

I would like to make the font size of all figure captions in my R Markdown document smaller. The final output is HTML 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: "July 4, 2017"
output: 
  html_document: 
    fig_caption: yes
---

```{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

As you can see, the caption font size and the regular text font size are exactly the same, which doesn't look that nice. How can I solve this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just add the following CSS to your Rmd document (anywhere below the YAML header):

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

What are we doing here:

If you mark the caption in your browser and inspect that element (Chrome: right-click -> Inspect) you can see that the caption is actually a HTML paragraph with a class named caption:

<p class="caption"> ... </p>

With the above CSS code we change the font-size of exactly those elements (and only those) to 60% of the default size.


enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...