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

如何在RMD模板所有页面(含标题页)添加表格页眉?

解决RMD模板标题页显示表格样式页眉的问题

你当前的问题出在book文档类的标题页默认使用独立的titlepage页样式,不会自动应用你设置的fancy页眉,所以需要强制给标题页指定fancy样式,或者修改titlepage样式的定义。

下面是两种可行的修改方案:

方案一:强制标题页使用fancy样式

在你的header-includes里添加一行代码,利用\AtBeginDocument命令在文档开头(也就是标题页生成后)设置当前页样式为fancy:

修改后的完整YAML头部代码:

---
output:
  bookdown::pdf_document2: 
    keep_tex: true
documentclass: book
classoption: demo   
header-includes:
  - \usepackage{array}
  - \usepackage{lastpage}
  - \usepackage{lipsum}
  - \usepackage{fancyhdr}
  - \usepackage{geometry}
  - \geometry{hmargin=2cm,top=4cm,headheight=65pt,footskip=65pt}
  - \setlength{\parindent}{0.95cm}
  - \pagestyle{fancy}
  - \renewcommand{\headrulewidth}{0pt}
  - \fancyhead[CE,CO,LE,LO,RE,RO]{}
  - \fancyhead[C]{\begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|} \hline\includegraphics[height=1.5cm,width=2.5cm]{logo.png} &           \centering \Huge{TITLE} & \centering \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\ Data{:} 17/05/2013\\  Rev. 0}\\tabularnewline \hline \end{tabular}}
  - \fancyfoot[CE,CO,LE,LO,RE,RO]{}
  - \fancyfoot[C]{\begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|} \hline \includegraphics[height=1.5cm,width=2.5cm]{logo.png} & \centering \Huge{TITLE} & \centering \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\ Data{:} 17/05/2013\\ Rev. 0}\\tabularnewline \hline \end{tabular}}
  - \AtBeginDocument{\thispagestyle{fancy}} % 新增该行,强制标题页应用fancy样式
---

方案二:重新定义titlepage样式

如果你想让标题页的页眉逻辑和其他页面完全统一,可以直接把titlepage页样式重定义为fancy:

修改后的完整YAML头部代码:

---
output:
  bookdown::pdf_document2: 
    keep_tex: true
documentclass: book
classoption: demo   
header-includes:
  - \usepackage{array}
  - \usepackage{lastpage}
  - \usepackage{lipsum}
  - \usepackage{fancyhdr}
  - \usepackage{geometry}
  - \geometry{hmargin=2cm,top=4cm,headheight=65pt,footskip=65pt}
  - \setlength{\parindent}{0.95cm}
  - \pagestyle{fancy}
  - \renewcommand{\headrulewidth}{0pt}
  - \fancyhead[CE,CO,LE,LO,RE,RO]{}
  - \fancyhead[C]{\begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|} \hline\includegraphics[height=1.5cm,width=2.5cm]{logo.png} &           \centering \Huge{TITLE} & \centering \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\ Data{:} 17/05/2013\\  Rev. 0}\\tabularnewline \hline \end{tabular}}
  - \fancyfoot[CE,CO,LE,LO,RE,RO]{}
  - \fancyfoot[C]{\begin{tabular}{|m{3.0cm}|m{10.0cm}|m{2.5cm}|} \hline \includegraphics[height=1.5cm,width=2.5cm]{logo.png} & \centering \Huge{TITLE} & \centering \tiny{P\'ag. \thepage\ de \pageref{LastPage}\\ Data{:} 17/05/2013\\ Rev. 0}\\tabularnewline \hline \end{tabular}}
  - \makeatletter
  - \let\oldtitlepage\titlepage
  - \renewcommand{\titlepage}{\oldtitlepage\thispagestyle{fancy}}
  - \makeatother
---

额外说明

  • 两种方案都能实现标题页显示表格页眉的需求,方案一更简洁,方案二更适合后续需要修改标题页其他样式的场景。
  • 确保headheight=65pt的设置足够容纳你的表格页眉,避免出现LaTeX警告。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 15:45:39