Bärbel
Bärbel

Reputation: 1035

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

Upvotes: 88

Views: 303014

Answers (6)

jamesguy0121
jamesguy0121

Reputation: 1254

You can change the main body font size (as well as any other CSS you might want to change) 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 I'm doing a lot of CSS I use a separate CSS file. If I'm just modifying a couple of simple things I 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)
```

Upvotes: 48

Eric Olberding
Eric Olberding

Reputation: 1

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.

Upvotes: 0

YujieCui
YujieCui

Reputation: 11

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.

Upvotes: 1

RRuiz
RRuiz

Reputation: 2228

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:

 <font size="1"> This is my text number1</font> 

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

Upvotes: 75

KareemJ
KareemJ

Reputation: 804

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.

Upvotes: 16

jmjr
jmjr

Reputation: 2160

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;
}

Upvotes: 77

Related Questions