You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R Markdown生成PDF出现双标题页,求移除多余页面方案

解决R Markdown生成PDF重复标题页的问题

问题原因

重复标题页是两种行为叠加导致的:

  • YAML配置里的title/author/date会触发pdf_document默认生成标题页(pandoc自动插入\maketitle命令)
  • 你自定义的titlingpage代码里又调用了\maketitle,导致标题内容被渲染两次

解决方案

方法1:禁用默认标题生成,手动自定义标题内容

修改YAML配置,将title/author/date设为空,避免pandoc自动生成标题页;同时在自定义代码里手动添加所有标题元素,不再依赖\maketitle。

修改后的YAML配置

---
title: ""
author: ""
date: ""
output: 
  pdf_document:
    keep_tex: true
header-includes:
  - \usepackage{titling}
  - \usepackage{fancyhdr}
---

修改后的R代码块

```{r, echo=FALSE, results='asis'}
cat('
\\pagenumbering{gobble}
\\begin{titlingpage}
\\vspace*{0.5in} % 按需调整间距
\\centering
\\includegraphics[width=6in]{Logo_COLOUR.png}\\\\
\\vspace*{0.5in}
{\\Huge My Organisation}\\\\
{\\Large 2023 Report}\\\\
\\vspace*{1in}
{\\Large My Name}\\\\
{\\small 2023-06-07}
\\end{titlingpage}

\\newpage
\\pagenumbering{arabic}
\\setcounter{page}{1}
')
### 方法2:保留YAML元数据,阻止自动生成标题页
如果想保留YAML里的`title`/`author`/`date`用于其他场景,可以在`header-includes`中添加命令让默认的`\maketitle`失效,同时自定义标题页时通过元数据引用内容,避免重复调用。

#### 修改后的YAML配置
```yaml
---
title: |
  | My Organisation
  | 2023 Report
author: "My Name"
date: "2023-06-07"
output: 
  pdf_document:
    keep_tex: true
header-includes:
  - \usepackage{titling}
  - \usepackage{fancyhdr}
  - \let\maketitle\relax % 让默认的\maketitle命令失效
---

修改后的R代码块

```{r, echo=FALSE, results='asis'}
cat('
\\pagenumbering{gobble}
\\begin{titlingpage}
\\vspace*{0.5in} % 按需调整间距
\\centering
\\includegraphics[width=6in]{Logo_COLOUR.png}\\\\
\\vspace*{0.5in}
{\\Huge ', rmarkdown::metadata$title, '}\\\\
\\vspace*{1in}
{\\Large ', rmarkdown::metadata$author, '}\\\\
{\\small ', rmarkdown::metadata$date, '}
\\end{titlingpage}

\\newpage
\\pagenumbering{arabic}
\\setcounter{page}{1}
')
内容的提问来源于stack exchange,提问作者lalu thomas
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 20:13:10