简体   繁体   English

如何在不使用单独文件的情况下使用 Knitr for PDF 将目录放在单独的页面上?

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

I am writing a report using Knitr and Rmarkdown.我正在使用 Knitr 和 Rmarkdown 编写报告。 I have a table of contents that, annoyingly, ends up on the same page as the abstract, title etcetera.我有一个目录,令人讨厌的是,它最终与摘要、标题等在同一页上。

How can I place it on a separate page?如何将其放在单独的页面上?

I found this solution , but for one it doesn't seem to work for me and it is bothersome to start using separate files.我找到了这个解决方案,但是对于我来说它似乎不起作用,并且开始使用单独的文件很麻烦。 It feels as this should be easy to do.感觉这应该很容易做到。 Reproducible example below.下面的可重现示例。

---
output:
  pdf_document:
    number_sections: true
    toc: true
    
header-includes:
  - \usepackage[justification = centering, labelfont = bf, singlelinecheck = false]{caption}
  - \renewcommand{\and}{\\}
  - \usepackage{setspace}
  - \doublespacing

title: "Some Fancy Title"
subtitle: "Some Less Fancy Subtitle"
author:
  - "Author1^[author1@example.com]"
  - "Author2^[author2@example.com]"
date: "2021"

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." 

---

```{r Knitr and options set up, include = FALSE}

# Default option for echo is FALSE meaning that we don't want to print the code.
knitr::opts_chunk$set(echo = FALSE)

```

\newpage
# Some header

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.

## Sub-header

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.

Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut nonummy. Fusce aliquet pede non pede. Suspendisse dapibus lorem pellentesque magna. Integer nulla. Donec blandit feugiat.

You could add \\clearpage at the end of your abstract.您可以在摘要末尾添加\\clearpage 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 .您需要添加一个额外的斜杠 ( \ ) 来转义 LaTeX 命令\clearpage

-output -输出

在此处输入图像描述

To place the toc wherever needed without messing with tex files,将 toc 放置在任何需要的地方而不会弄乱tex文件,

Turn off automatic toc insertion first in the YAML metadata.首先在 YAML 元数据中关闭自动 toc 插入。

---
title: "myTitle"
date: "`r Sys.Date()`"
output: 
  pdf_document:
    toc: no
    number_sections: true
urlcolor: blue
editor_options:
  chunk_output_type: console
documentclass: report
---

Then, wherever you want the toc to be in your document, add然后,无论您希望目录位于文档中的哪个位置,添加

```
{=latex}
\setcounter{tocdepth}{4}
\tableofcontents
```

You can then place this toc anywhere using latex macros such as \newpage or \hfill\break for example.然后,您可以使用 latex 宏(例如\newpage newpage 或\hfill\break )将此目录放置在任何位置。

---
title: "myTitle"
date: "`r Sys.Date()`"
output: 
  pdf_document:
    toc: no
    number_sections: true
urlcolor: blue
editor_options:
  chunk_output_type: console
---
\newpage
```{=latex}
\setcounter{tocdepth}{4}
\tableofcontents
```
\newpage

Note: documentclass: report in the metadata will automatically separate the toc from the title, but won't allow to separate it from the remainder of the document.注意:元数据中的documentclass: report会自动将目录与标题分开,但不允许将其与文档的其余部分分开。

分开的

Source资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM