Home > Enterprise >  Is it possible to not display headers in the pages of an R Markdown PDF document?
Is it possible to not display headers in the pages of an R Markdown PDF document?

Time:08-18

I have an R Markdown document that contains figures and tables that are separated and organized by headers and sub-headers. I very much like the organization that provides and like the ability to navigate the document using the headers. However, the presence of the header labels in the pages of the PDF document makes it look cluttered and disorganized in some cases, especially since the figures have their own titles and subtitles and there may be up to 3 headers shown on a single page (image below is a simplified example). Is it possible to set up the R Markdown document to not show the headers on the pages of the PDF itself? Thanks in advance.

enter image description here

CodePudding user response:

The trick here is to adding items in the table of contents and this can be done using \addcontentsline.


---
title: "Hidding Subsection header"
output: pdf_document
---

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

# R Markdown

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Sit amet cursus sit amet dictum sit amet justo donec. Donec massa sapien faucibus et molestie. Varius quam quisque id 

\addcontentsline{toc}{subsection}{plot one}

```{r cars}
plot(mtcars$mpg)
```

# Including Plots

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Sit amet cursus sit amet dictum sit amet justo donec. Donec massa sapien faucibus et molestie. Varius quam quisque id 

\addcontentsline{toc}{subsection}{plot two}

```{r pressure, echo=FALSE}
plot(pressure)
```

Then there will be no subsection header in the document,

document with a figure


But the header will appear in the toc,

table of content


See this answer on Tex StackExchange to know more about the syntax of \addcontentsline

  • Related