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

如何在R Markdown编织PDF时移除空白标题页?

问题

我当前使用的R Markdown YAML头配置如下:

---
title: ""
output: pdf_document
classoption: landscape
header-includes: 
- \pagenumbering{gobble}

---

但编织PDF时仍会生成空白首页,想找类似preserve_yaml=FALSE的方式移除该标题页,因为需要定期生成文档,希望避免手动删除空白页提升效率。

解决方案

方法1:通过LaTeX命令直接禁用标题页生成

默认的pdf_document模板会执行\maketitle命令生成标题页,哪怕title为空也会留下空白页。在YAML的header-includes中添加一行LaTeX命令,直接取消\maketitle的作用即可:

---
title: ""
output: pdf_document
classoption: landscape
header-includes: 
- \pagenumbering{gobble}
- \AtBeginDocument{\let\maketitle\relax}
---

方法2:使用无标题页的自定义模板

如果需要更灵活的格式控制,可以创建一个极简LaTeX模板(命名为no-title-page.tex),去掉默认模板中生成标题页的逻辑:

\documentclass[$classoption$]{article}
$if(fontfamily)$
\usepackage[$fontfamilyoptions$]{$fontfamily$}
$else$
\usepackage{lmodern}
$endif$
$if(geometry)$
\usepackage[$geometry$]{geometry}
$endif$
$for(header-includes)$
$header-includes$
$endfor$
$if(linestretch)$
\usepackage{setspace}
\setstretch{$linestretch$}
$endif$
$if(natbib)$
\usepackage{natbib}
\bibliographystyle{$natbiboptions$}
$endif$
$if(biblatex)$
\usepackage[$biblatexoptions$]{biblatex}
\addbibresource{$biblatexfile$}
$endif$
$if(listings)$
\usepackage{listings}
$endif$
$if(lhs)$
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
$endif$
$if(highlighting-macros)$
$highlighting-macros$
$endif$
$if(tables)$
\usepackage{longtable,booktabs}
$endif$
$if(graphics)$
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
$endif$
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
$if(indent)$
\setlength{\parindent}{$indent$}
$endif$
$if(verbatim-in-note)$
\usepackage{fancyvrb}
$endif$
$if(colorlinks)$
\usepackage{xcolor}
\definecolor{$linkcolor$}{HTML}{$linkcolor$}
\definecolor{$filecolor$}{HTML}{$filecolor$}
\definecolor{$citecolor$}{HTML}{$citecolor$}
\definecolor{$urlcolor$}{HTML}{$urlcolor$}
$endif$
\usepackage{hyperref}
$if(colorlinks)$
\hypersetup{colorlinks=true,linkcolor=$linkcolor$,filecolor=$filecolor$,citecolor=$citecolor$,urlcolor=$urlcolor$}
$else$
\hypersetup{hidelinks}
$endif$
$if(links-as-notes)$
\renewcommand{\href}[2]{#2\footnote{\url{#1}}}
$endif$
$if(strikeout)$
\usepackage[normalem]{ulem}
$endif$
$if(numbersections)$
\setcounter{secnumdepth}{$secnumdepth$}
$else$
\setcounter{secnumdepth}{0}
$endif$
$if(subparagraph)$
$else$
\setcounter{tocdepth}{3}
$endif$
$if(pagestyle)$
\pagestyle{$pagestyle$}
$endif$
\begin{document}
$body$
\end{document}

然后在YAML中指定这个模板:

---
title: ""
output: 
  pdf_document:
    template: no-title-page.tex
classoption: landscape
header-includes: 
- \pagenumbering{gobble}
---

方法3:自动化修改编译后的TeX文件

如果需要批量自动化处理,可以先保留TeX文件,再移除其中的\maketitle命令后重新编译。用R脚本实现的话:

# 编织Rmd生成TeX文件
rmarkdown::render("your-document.Rmd", 
                  output_format = rmarkdown::pdf_document(keep_tex = TRUE))

# 读取TeX内容并移除\maketitle行
tex_content <- readLines("your-document.tex")
tex_content <- tex_content[!grepl("^\\\\maketitle$", tex_content)]
writeLines(tex_content, "your-document-modified.tex")

# 编译修改后的TeX文件
tinytex::pdflatex("your-document-modified.tex")

内容的提问来源于stack exchange,提问作者Nico

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 08:20:32