Rmarkdown Font Size and Header

Rmarkdown font size and header

This is what I used to control font size and color in an R-markdown file. It basically overrides the CSS style sheets without having to create a new file. The example changes the sizes of the headers and titles, as well as the inline text and the R-code text, and sets some colors as well.

In my case I needed to pack more information into a document that had a specified number of pages so I made everything smaller.

---
title: "This is a title"
date: 25 May 2015
output:
html_document:
theme: cerulean
---

<style type="text/css">

body{ /* Normal */
font-size: 12px;
}
td { /* Table */
font-size: 8px;
}
h1.title {
font-size: 38px;
color: DarkRed;
}
h1 { /* Header 1 */
font-size: 28px;
color: DarkBlue;
}
h2 { /* Header 2 */
font-size: 22px;
color: DarkBlue;
}
h3 { /* Header 3 */
font-size: 18px;
font-family: "Times New Roman", Times, serif;
color: DarkBlue;
}
code.r{ /* Code block */
font-size: 12px;
}
pre { /* Code block - determines code spacing between lines */
font-size: 14px;
}
</style>

# H1 Header

Some body text

## H2 Header

More body text

### H3 Header

blah blah blah

```{r echo=T}
n <- 100
df <- data.frame(x=rnorm(n),y=rnorm(n))
```

### Another H3

Update:

Added more more styles, comments, and a bit of color to make this answer more useful. And a screen shot:

Sample Image

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

Manipulating the font size of headings, footnote and table description in bookdown (for pdf_book)

Here is a solution to change the default font size locally.

---
title: "Untitled"
author: "bttomio"
date: "4/26/2021"
output: pdf_document
---

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

## \scriptsize Use one slash for headings (\\scriptsize)

For the footnote, it works similarly. Check here^[\tiny Using \\tiny.]

## \huge Table description with {kableExtra} (\\huge)

For example:

```{r pressure, echo=FALSE}
library(kableExtra)
kbl(mtcars[1:2, 1:4], booktabs = T, linesep = "", caption = "\\huge Test with huge", escape = F) %>%
kable_styling(latex_options = "hold_position")
```

More styles, following LaTeX code: https://i2.wp.com/texblog.org/Wordpress/wp-content/uploads/2012/08/font-size21.png (Source).

-output

Sample Image

Reducing column header names font size in kable tables in Rmarkdown (ioslides)

You can change row sizes by using row_spec()

Since the header is interpreted as the row 0 you can change your header independently to fit your needings.

Try

kable(df, format= "html") %>%
kable_styling(full_width = F, bootstrap_options = c("striped", "hover", "condensed"), font_size = 12.5) %>%
row_spec(0, font_size=9)

for more settings check row_spec

Change size of section titles in R Markdown PDF

You can place

\usepackage[medium]{titlesec} % or small

in preamble.tex (or where ever you have special LaTeX commands) together with

subparagraph: yes

in th e YAML headers (c.f. this question).
For a more detailed answer we will need a minimal working example.

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

R-markdown - different font size in different sections

One solution would be to wrap your sections into fenced divs and to use CSS to style the sections.

::: {.large}
```{r psection 1}
# I would like to have font size 20
print("Hellow, World!")
```
:::

::: {.normal-size}
```{r psection 2}
# I would like to have font size 10
print("Hellow, World!")
```
:::

<style>
.normal-size pre {
font-size: 10pt;
}
.large pre {
font-size: 20pt;
}
</style>


Related Topics



Leave a reply



Submit