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

Bookdown生成PDF时无编号前言页眉异常问题求助

Fixing "Preface" Header Issue in Bookdown PDF Output

I’ve run into this exact problem before with Bookdown—when generating a PDF, the unnumbered preface doesn’t show "Preface" in the header, instead sticking with the previous section’s header (like "Contents") even across multiple pages. Here are a few reliable fixes I’ve tested:

1. Manually Set Header with LaTeX’s fancyhdr

Since Bookdown relies on LaTeX under the hood, you can directly tweak the header styling for the preface section.

  • At the start of your preface Rmd file (or the top of index.Rmd if your preface is there), add this LaTeX code to override the header:
    \pagestyle{fancy}
    \fancyhead[LE,RO]{Preface} % Left-even, Right-odd pages show "Preface"
    \fancyhead[LO,RE]{} % Clear the opposite side header
    \fancyfoot[C]{\thepage} % Keep page numbers centered at the bottom
    
  • Once your preface ends (right before your first numbered chapter), reset the header back to Bookdown’s default style:
    \pagestyle{bookdown}
    

2. Use \chapter* + \markboth for Explicit Header Marking

Bookdown’s default unnumbered headings might not trigger the LaTeX header update properly. Instead, use the raw LaTeX chapter command and explicitly set the header text:

  • Replace your preface’s markdown heading (like # Preface) with:
    \chapter*{Preface}
    \markboth{Preface}{Preface}
    

The \markboth command tells LaTeX to use "Preface" for both left and right page headers, which will persist across all pages of the preface.

3. Adjust YAML Configuration and Include External LaTeX

If you prefer keeping your Rmd files clean, you can handle the header via Bookdown’s YAML settings:

  1. Create a separate preface-header.tex file with:
    \chapter*{Preface}
    \markboth{Preface}{Preface}
    
  2. Update your index.Rmd YAML to include this file before the main body:
    output:
      bookdown::pdf_book:
        toc: true
        includes:
          before_body: preface-header.tex
    
  3. Place your preface content right after this included section in your Rmd file.

Quick Test with Minimal Example

To verify, use this minimal index.Rmd setup:

---
title: "Test Book"
author: "Test Author"
output:
  bookdown::pdf_book:
    toc: true
    number_sections: false
documentclass: book
---

\chapter*{Preface}
\markboth{Preface}{Preface}

This is the preface content. Add enough lines here to make it span multiple pages—for example, repeat this sentence 20 times. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

# Introduction

This is the first chapter of your book.

When you render this, the preface pages should correctly show "Preface" in the header.

内容的提问来源于stack exchange,提问作者Jeffrey S. Racine

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:04:07