level 4 and 5 headers rendering issues in r markdown
Asked Answered
R

2

1

The level 4 and 5 headers in Rmarkdown does not create a new line after they are rendered in the pdf file. I am just wondering how I could fix this issue.
The R markdown codes to generate pdf

enter image description here

The pdf output from the code shown above

enter image description here

Resnick answered 9/2, 2021 at 20:8 Comment(1)
Add new line tag <br />. For future add your code as chunk in your question not as a image. Make it as easy as possible for people to helpSpecious
D
4

This is hackish, but it seems to work:

---
title: "Untitled"
output: pdf_document
---

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

## R Markdown

### 3rd-level heading
Content 3

#### 4th-level heading
\hspace{-2.5em} Content 4

##### 5th-level heading
\hspace{-2.5em} Content 5
Dementia answered 9/2, 2021 at 23:30 Comment(0)
D
0

A reasonable approach is to add a LaTeX preamble to your document where you override the title format definitions of Level 4 (i.e., paragraph) and Level 5 (i.e., subparagraph) headers:

header.tex

First save a header.tex file alongside your Test.Rmd document with the following contents:

\usepackage{titlesec}

\titleformat{\paragraph}[display]
{\normalfont\normalsize\bfseries}{\paragraphtitlename \theparagraph}{1em}{}

\titleformat{\subparagraph}[display]
{\normalfont\normalsize\bfseries}{\subparagraphtitlename \thesubparagraph}{1em}{}

Test.Rmd

Then edit your Test.Rmd to add the following to your YAML:

---
title: "Test"
output: 
  pdf_document:
    includes:
      in_header: header.tex
---

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

## R Markdown

### 3rd-level heading
Content 3

#### 4th-level heading
Content 4

##### 5th-level heading
Content 5

Test.pdf

After knitting, you should have a PDF with the desired spacing.

enter image description here

Dementia answered 24/1 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.