Karen Lau
Karen Lau

Reputation: 35

Subtitles mixing with text

I've tried to separate my subtitles from plain text using two blank space.
The first part of my .Rmd document looks like this


---
title: 'Script de Limpieza: errores de digitalizacion y division de base madre'
author: "Leonardo Doig & Karen Lau"
date: "10/9/2020"
output:
  html_document: default
  word_document: default
  pdf_document: default
---


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


some random words should be here

#### First subtitle  

there are 2 blank spaces after the first subtitle


{r message=FALSE}
library(lubridate)

#### Second Subtitle   

plain text (after the second subtitle there are 2 blanks spaces too)

but the final output it's always like this:

plain text
First subtitle there are 2 blank spaces after the first subtitle

library(lubridate)

Second Subtitle plain text (after the second subtitle there are 2 blanks spaces too)


It only happens when I Knit my .Rmd file to .pdf. When I Knit to .html all this mess with the subtitles and plain text seems to be okay.

Upvotes: 3

Views: 1041

Answers (1)

Alexlok
Alexlok

Reputation: 3134

The reason of this behavior is that this markdown:

# Level 1
Text 1
## Level 2
Text 2
### Level 3
Text 3
#### Level 4
Text 4
##### Level 5

is transformed into that LaTeX code:

\hypertarget{level-1}{%
\section{Level 1}\label{level-1}}

Text 1

\hypertarget{level-2}{%
\subsection{Level 2}\label{level-2}}

Text 2

\hypertarget{level-3}{%
\subsubsection{Level 3}\label{level-3}}

Text 3

\hypertarget{level-4}{%
\paragraph{Level 4}\label{level-4}}

Text 4

\hypertarget{level-5}{%
\subparagraph{Level 5}\label{level-5}}

Text 5

With the common LaTex hierarchy of section-subsection-subsubsection-paragraph-subparagraph.

So the Rmarkdown formatting of #### is defined by the LaTeX formatting of \paragraph{}. And in the standard LaTeX styles, paragraph doesn't get a line break. It's a classic question, there are several workarounds (e.g. here or there), Here I'll use that one. For that solution, we need to include this code in the LaTeX header:

\makeatletter
\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}%
  {-3.25ex\@plus -1ex \@minus -.2ex}%
  {1.5ex \@plus .2ex}%
  {\normalfont\normalsize\bfseries}}
\makeatother

Let's just save that code by itself into a file, say reformat_paragraph.tex. Now in our Rmarkdown document, in the header we can include a reference to that LaTeX file:

---
title: "My Rmarkdown document"
author: "me"
date: "9/19/2020"
output:
  pdf_document:
    includes:
      in_header: reformat_paragraph.tex
---

And that should work!

Upvotes: 5

Related Questions