R Markdown - Changing Font Size and Font Type in HTML Output

R Markdown - changing font size and font type in html output

I think fontsize: command in YAML only works for LaTeX / pdf. Apart, in standard latex classes (article, book, and report) only three font sizes are accepted (10pt, 11pt, and 12pt).

Regarding appearance (different font types and colors), you can specify a theme:. See Appearance and Style.

I guess, what you are looking for is your own css. Make a file called style.css, save it in the same folder as your .Rmd and include it in the YAML header:

---
output:
html_document:
css: style.css
---

In the css-file you define your font-type and size:

/* Whole document: */
body{
font-family: Helvetica;
font-size: 16pt;
}
/* Headers */
h1,h2,h3,h4,h5,h6{
font-size: 24pt;
}

How to change font size and type in the middle of a R markdown document?

We may use LaTeX codes inside your rmarkdown script. Set the font type with \fontfamily{}, some font codes can be found here. (Note that I am using Palatino here because Garamond is probably not installed on my system.) Font size can be set with \fontsize{}{}, where the first {} stands for the size, the second for the spacing. To make this just temporary I suggest to embed it into a \begingroup and \endgroup. Leave your YAML header as it is.

This line is normal text.

\begingroup
\fontfamily{ppl}\fontsize{14}{16}\selectfont
This line uses Garamond font and font size 14.
\endgroup

\begingroup
\fontfamily{phv}\fontsize{16}{18}\selectfont
This line uses arial font and font size 16.
\endgroup

This line is normal text.

Result

Sample Image

Rmarkdown font size and header

This is what I used to control font size and color in an R-markdown file. It basically overrides the CSS style sheets without having to create a new file. The example changes the sizes of the headers and titles, as well as the inline text and the R-code text, and sets some colors as well.

In my case I needed to pack more information into a document that had a specified number of pages so I made everything smaller.

---
title: "This is a title"
date: 25 May 2015
output:
html_document:
theme: cerulean
---

<style type="text/css">

body{ /* Normal */
font-size: 12px;
}
td { /* Table */
font-size: 8px;
}
h1.title {
font-size: 38px;
color: DarkRed;
}
h1 { /* Header 1 */
font-size: 28px;
color: DarkBlue;
}
h2 { /* Header 2 */
font-size: 22px;
color: DarkBlue;
}
h3 { /* Header 3 */
font-size: 18px;
font-family: "Times New Roman", Times, serif;
color: DarkBlue;
}
code.r{ /* Code block */
font-size: 12px;
}
pre { /* Code block - determines code spacing between lines */
font-size: 14px;
}
</style>


# H1 Header

Some body text

## H2 Header

More body text

### H3 Header

blah blah blah

```{r echo=T}
n <- 100
df <- data.frame(x=rnorm(n),y=rnorm(n))
```

### Another H3

Update:

Added more more styles, comments, and a bit of color to make this answer more useful. And a screen shot:

Sample Image

How to set up font style and size for different sections of RMarkdown document?

We may use LaTeX code in Rmarkdown. \fontfamily for font family: phv is Helvetica and similar to Arial but doesn't need an extra package. \fontseries for font type: we use b for bold and bc for medium condensed (for other values see this tex.stackexchange answer). The font size we define with \small and \footnotesize which should correspond to 9pt and 8pt. To revert everthing we use \rmfamily\normalsize.

---
title: "R Notebook"
output: pdf_document
---
## 1. First section with "First car name"

\fontfamily{phv}\fontseries{b}\small

```{r echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
# 1. Data
fist_car_name <- rownames(mtcars)[[1]]

# 2. Print name of the first car
cat(fist_car_name)
```

## 2. Second section with "Data about all cars"

\fontfamily{phv}\fontseries{bc}\footnotesize

```{r echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
library(knitr)
library(kableExtra)

kable(mtcars)
```
## 3. Let's switch back to defaults

\rmfamily\normalsize

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.

Yielding

Sample Image

R-markdown - different font size in different sections

One solution would be to wrap your sections into fenced divs and to use CSS to style the sections.

::: {.large}
```{r psection 1}
# I would like to have font size 20
print("Hellow, World!")
```
:::


::: {.normal-size}
```{r psection 2}
# I would like to have font size 10
print("Hellow, World!")
```
:::

<style>
.normal-size pre {
font-size: 10pt;
}
.large pre {
font-size: 20pt;
}
</style>


Related Topics



Leave a reply



Submit