Leonhardt Guass
Leonhardt Guass

Reputation: 793

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

Example:

---
output: pdf_document
mainfont: Times New Roman
fontsize: 12pt
spacing: 1.1
geometry: "left=1in,right=1in,top=0.5in,bottom=1in"
vspace: 4pt
---

This line is normal text.

This line uses Garamond font and font size 14.

This line uses arial font and font size 16.

This line is normal text.

I tried a few options and none worked. Please let me know if there is a way to do it.

Upvotes: 6

Views: 12022

Answers (1)

jay.sf
jay.sf

Reputation: 73712

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

enter image description here

Upvotes: 7

Related Questions