[r] R Markdown - changing font size and font type in html output

I am using R Markdown in RStudio and the knit HTML option to create HTML output. However, the font used in the ouput for plain text blocks is rather small and I would like to change it to a differnt font and increase the font size. Can someone show an example how to set the output font - workable without a lot of knowledge in html?

So far I tried at the top of my markdown document, but this doesn't work.

---
fontsize: 24pt
---

This question is related to r knitr r-markdown

The answer is


These answers are overly complicated. You can change the main body font size (as well as any other CSS you might want to change) simply by embedding CSS directly into the Rmarkdown document using the html <style> tag. You do not need an entire CSS file for something so simple. If you are doing a lot of CSS then use a separate CSS file. If you are just modifying a couple of simple things I would do it like this.

---
title: "Untitled"
author: "James"
date: "9/29/2020"
output: html_document
---

<style type="text/css">
  body{
  font-size: 12pt;
}
</style>


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

I had the same issue and solved by making sure that 1. when you make the style.css file, make sure you didn't just rename a text file as "style.css", make sure it's really the .css format (e.g, use visual studio code); 2. put that style.css file in the same folder with your .rmd file. Hopefully this works for you.


I would definitely use html markers to achieve this. Just surround your text with <p></p> or <font></font> and add the desired attributes. See the following example:

<p style="font-family: times, serif; font-size:11pt; font-style:italic">
    Why did we use these specific parameters during the calculation of the fingerprints?
</p>

This will produce the following output

Font Output

compared to

Default Output

This would work with Jupyter Notebook as well as Typora, but I'm not sure if it is universal.

Lastly, be aware that the html marker overrides the font styling used by Markdown.


You can change the font size in R Markdown with HTML code tags <font size="1"> your text </font> . This code is added to the R Markdown document and will alter the output of the HTML output.

For example:

_x000D_
_x000D_
 <font size="1"> This is my text number1</font> _x000D_
_x000D_
 <font size="2"> This is my text number 2 </font>_x000D_
 _x000D_
 <font size="3"> This is my text number 3</font> _x000D_
 _x000D_
 <font size="4"> This is my text number 4</font> _x000D_
 _x000D_
 <font size="5"> This is my text number 5</font> _x000D_
 _x000D_
 <font size="6"> This is my text number 6</font>
_x000D_
_x000D_
_x000D_


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,...).

Add the following somewhere in this section.

body {
  font-size: 16px;
}

The font size above is 16 pt font. You can change the number to whatever you want.


Examples related to r

How to get AIC from Conway–Maxwell-Poisson regression via COM-poisson package in R? R : how to simply repeat a command? session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium How to show code but hide output in RMarkdown? remove kernel on jupyter notebook Function to calculate R2 (R-squared) in R Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph R multiple conditions in if statement What does "The following object is masked from 'package:xxx'" mean?

Examples related to knitr

How to show code but hide output in RMarkdown? R Markdown - changing font size and font type in html output Plot size and resolution with R markdown, knitr, pandoc, beamer How to set size for local image using knitr for markdown? Hiding the R code in Rmarkdown/knit and just showing the results R - Markdown avoiding package loading messages How to convert R Markdown to PDF?

Examples related to r-markdown

How to show code but hide output in RMarkdown? How to add new line in Markdown presentation? R Markdown - changing font size and font type in html output How to add \newpage in Rmarkdown in a smart way? Insert picture/table in R Markdown Centering image and text in R Markdown for a PDF report Plot size and resolution with R markdown, knitr, pandoc, beamer Hiding the R code in Rmarkdown/knit and just showing the results R - Markdown avoiding package loading messages How to convert R Markdown to PDF?