A Guide to Adjusting Font Size in R Markdown

Adjusting font size in R Markdown can be done through basic Markdown syntax, inline R code, CSS styles, Pandoc Markdown, or global parameters. Choose the method that best fits your document's needs for a polished and professional look.

A Guide to Adjusting Font Size in R Markdown

"Why struggle with Markdown formatting? Our free tools make it easy to create beautiful, professional-looking documents in seconds."

In the realm of data analysis and report creation, R Markdown is an incredibly powerful tool. It allows users to generate dynamic documents by combining Markdown syntax with R code. However, for beginners, adjusting the font size of text can be somewhat tricky. This article will detail how to adjust font sizes in R Markdown documents.

1. Basic Markdown Syntax

R Markdown supports basic Markdown syntax, including headings and paragraphs. You can adjust the size of headings as follows:

# Heading Level 1
## Heading Level 2
### Heading Level 3
#### Heading Level 4
##### Heading Level 5
###### Heading Level 6

The number of hash symbols (#) determines the size of the heading, with one hash being the largest and six hashes being the smallest.

2. Using Inline R Code

In R Markdown, you can use inline R code to dynamically adjust font sizes. For example, if you want to set the font size of a paragraph to 14 points, you can use the following code:

`r paste0("<span style='font-size:14pt;'>", "This is text with a 14pt font size", "</span>")`

3. Using CSS Styles

For more complex formatting adjustments, you can use CSS styles. Add CSS styles in the YAML header of your R Markdown document, like so:

---
title: "Adjusting Font Size"
output: html_document
css: styles.css
---

Then define your styles in the styles.css file:

p {
  font-size: 14pt;
}

h1 {
  font-size: 24pt;
}

h2 {
  font-size: 20pt;
}

4. Using Pandoc Markdown

Since R Markdown is based on Pandoc, you can leverage Pandoc's Markdown extensions to adjust font sizes. For example, using Span and Div attributes:

This is normal text.

::: {style="font-size: 14pt;"}
This is text with a 14pt font size.
:::

5. Using R Markdown Parameters

You can also define global parameters in the YAML header and reference these parameters in your document:

---
title: "Adjusting Font Size"
output: html_document
params:
  font_size: 14
---

Then use these parameters in your document:

This is text with a `r params$font_size`pt font size.

Summary

There are multiple methods to adjust font sizes in R Markdown, ranging from basic Markdown syntax to using inline R code, CSS styles, Pandoc Markdown extensions, and defining global parameters. Choose the method that best suits your needs to make your documents more aesthetically pleasing and professional.