How to Separate Title Page and Table of Content Page from Knitr Rmarkdown PDF

How to place table of contents on separate page using Knitr for PDF without using separate files?

You could add \\clearpage at the end of your abstract. Therefore, it would become:

abstract: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.\\clearpage"

You need to add one aditional slash (\) to escape the LaTeX command \clearpage.

-output

Sample Image

How to force rmarkdown pdf output to start the table of contents in a new page?

The most easiest solution for you - changing documentclass to report (table of contents will be on the second page).

---
title: "test"

header-includes:
\usepackage{geometry}
\geometry{top=0.75in,left=3in,bottom=0.75in,right=0.80in}

output:
pdf_document:
toc: true
number_sections: true
documentclass: report
---

\chapter{First}

\chapter{Second}

How can I control the location of the table of contents in R Markdown (PDF output)?

If the first two pages shoud contain static content (not generated in the body of the R Markdown document), then moving the table of contents to page 3 can be achieved with small modifications in the LaTeX template used by Pandoc.

As explained in The Cookbook, the default LaTeX template is this (latest version).

  1. Download that file and save it in the directory of your RMD file. In my example below I named the file toc-at-page3-template.tex.

  2. Edit the template: For example, after line 476 (i.e., before $if(toc)$), add

     \begin{center}
    Custom Stuff
    \end{center}

    \clearpage

    \begin{center}
    More Custom Stuff
    \end{center}

    \clearpage
  3. In your RMD file, enable the custom template:



     output:
    pdf_document:
    toc: true
    template: toc-at-page3-template.tex
    ---

    Foo.

Output:
(click on thumbnails to enlarge)

Output page 1
Output page 2
Output page 3

How to place a table in a PDF via rmarkdown or format the header text more precisely?

This is one way, no doubt there are others...

  1. Create a new fancy header page style
  2. Define the table style header (and remove some of the default behaviours)
  3. remove the head rule
  4. for some reason the first page does not like a header so force the header on the first page with \thispagestyle(...)
  5. define the subsequent page styles as the tableheader.

Edit appearance as required...

---
title: "R Markdown Example With Numbered Sections"
output:
bookdown::pdf_document2:
toc: true
toc_depth: 4
number_sections: true

header-includes:
- \usepackage{fancyhdr}
- \usepackage{lastpage}
- \usepackage{array}
- \usepackage{lipsum}

---

\fancypagestyle{tableHeader}{

\fancyhf{}

\setlength\headheight{90pt}

\fancyhead[C]{
\centering{
\begin{minipage}{1.1\textwidth}
\renewcommand{\arraystretch}{2}
\begin{tabular}{|>{\centering\arraybackslash}m{0.2\textwidth}|>{\centering\arraybackslash}m{0.2\textwidth}|>{\centering\arraybackslash}m{0.2\textwidth}|>{\centering\arraybackslash}m{0.2\textwidth}|}
\hline
\multicolumn{4}{|c|}{\textbf{\LARGE{Title}}} \\
\hline
\textbf{\Large{Field 1:}} & \textbf{\Large{Field 2:}} & \textbf{\Large{Date:}} & \textbf{\Large{Page:}}\\
\textbf{\Large{Information}} & \textbf{\Large{Information}} & \textbf{\Large{`r Sys.Date()`}} & \textbf{\Large{{\thepage} of \pageref{LastPage}}}\\
\hline
\end{tabular}
\end{minipage}
}}
}

\renewcommand{\headrulewidth}{0pt}

\thispagestyle{tableHeader}

\pagestyle{tableHeader}

# Section 1

\lipsum[3]

## sub section

\newpage

\lipsum[2]

Sample Image

Sample Image

How to add table of contents in Rmarkdown?

The syntax is

---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
---

in the documentation. Make sure this is at the beginning of your document. Also make sure your document actually has headers otherwise R can't tell what you want in the table of contents.

Add Table of contents in different position in Rmd pdf

Using the code you provided I'm able to knit the pdf document and get the three plots, one for each Species in iris.

This code, however, produces two TOCs: one of them in the first page which is not desired according to your question. In order to get only the TOC produced by the function render_toc() set toc: no in the YAML header.

---
title: "test"
output:
pdf_document:
number_sections: yes
toc: no
toc_depth: 4
---

This way only one TOC will be generated and placed where render_toc() is.


Edit: After seeing your edit with the desired output, I think the following LaTeX code can help:

\newpage
\thispagestyle{plain}
\mbox{}

\setcounter{tocdepth}{2}
\renewcommand{\contentsname}{Table of Contents}
\tableofcontents

\newpage
\thispagestyle{plain}
\mbox{}

Note: here we are not using render_toc(). The code creates the TOC in the second page of the document. If you need another blank page, just introduce another block of:

\newpage
\thispagestyle{plain}
\mbox{}

These pages may or may not be numbered, which you control via the argument passed to \thispagestyle{}. See here for more details.

Full code:

---
title: "Document title"
output:
pdf_document:
number_sections: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\newpage
\thispagestyle{plain}
\mbox{}

\setcounter{tocdepth}{2}
\renewcommand{\contentsname}{Table of Contents}
\tableofcontents

\newpage
\thispagestyle{plain}
\mbox{}

# Test 1
## Subsection 1

# Test 2
## Subsection 2

# Test 3

```{r, results='asis', echo=FALSE}
library(ggplot2)
for(Species in levels(iris$Species)){
cat('\n#', Species, '\n')
p <- ggplot(iris[iris$Species == Species,],
aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point()
print(p)
cat("\n")
}
```

This way we end up with a TOC looking like this:

Sample Image



Related Topics



Leave a reply



Submit