Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 219
DeltaIV Avatar asked Jul 10 '17 17:07

DeltaIV


People also ask

How do I change the font size in R markdown in HTML?

To change the font size, you don't need to know a lot of html for this. Open the html output with notepad ++. Control F search for "font-size". You should see a section with font sizes for the headers (h1, h2, h3,...).

How do I change the output size in R markdown?

To change the output size you can use the corresponding LaTeX commands, set just before the code junk. The smallest option would be \tiny . For a full overview consider e.g. this. After the code junk it's important to set back to the size you used before, e.g. \normalsize .

How do you make words bold in R markdown?

To make the formatted text into bold type, you can simply use a pair of ** around the marked up text with no space. For example **bold** in the . Rmd file generates bold in the output document.


1 Answers

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

like image 155
Martin Schmelzer Avatar answered Oct 19 '22 01:10

Martin Schmelzer