Fonts for Rmarkdown Document

rmarkdown PDF custom font

The line header-includes: is missing. Try with the following header:

---
title: "Untitled"
author: "Someone"
date: "7/8/2021"
output:
pdf_document:
latex_engine: lualatex
header-includes:
- \usepackage{fontspec}
---

\setmainfont{Round Style}

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

Changing font in R Markdown to Times New Roman

This code may work for you:

---
title: "Times"
author: "bttomio"
date: "October 18, 2017"
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \usepackage{fontspec}
- \setmainfont{Times New Roman}
---

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

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

Another option:

---
title: "Times"
author: "bttomio"
date: "October 18, 2017"
output:
pdf_document:
latex_engine: xelatex
mainfont: "Times New Roman"
---

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

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

-output

Sample Image

Change equations font in RMarkdown

I'm not all that savvy on latex. However, when I used mathfont: Arial it was ignored. (I tried using it with both xelatex and lualatex.)

I was able to get it to work, but some of the content was smashed together. This works so that it's readable, still math, and arial font.

---
title: "Untitled"
author: "me"
date: "2/19/2022"
output:
pdf_document:
latex_engine: lualatex
header-includes:
- \usepackage{amsmath}
- \usepackage{unicode-math}
- \usepackage{babel}
- \defaultfontfeatures{ Scale=MatchLowercase, Ligatures = TeX }
- \setmainfont{Arial}
- \setsansfont{Arial}
- \setmonofont{Andale Mono}
- \setmathfont{GFSNeohellenicMath.otf}
- \setmathfont[range=up]{Arial}
- \setmathfont[range=it]{Arial Italic}
- \setmathfont[range=bfup]{Arial Bold}
- \setmathfont[range=bfit]{Arial Bold Italic}
- \setmathfont[range=tt]{Andale Mono}
---

Here's some of the math I used to validate this:

$$A_{m,n} =
\begin{pmatrix}
a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
\vdots & \vdots & \ddots & \vdots \\
a_{m,1} & a_{m,2} & \cdots & a_{m,n}
\end{pmatrix}$$

$$f(data | \lambda) \pi ( \lambda)$$

$$ answer ~=~ \frac{\pi}{d}$$

The output:

Sample Image

With only mathfont: Arial in the YAML, regardless of the engine:

Sample Image



Related Topics



Leave a reply



Submit