Reficul
Reficul

Reputation: 11

level 4 and 5 headers rendering issues in r markdown

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

Upvotes: 1

Views: 799

Answers (2)

the-mad-statter
the-mad-statter

Reputation: 8886

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

Upvotes: 0

the-mad-statter
the-mad-statter

Reputation: 8886

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

Upvotes: 4

Related Questions