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

R Markdown生成PDF时章节标题上色失效,求原因排查

R Markdown章节标题上色失败的原因及修复
  • 核心问题:配置层级错误
    header-includes 是 pdf_document 的子配置项,你把它和 pdf_document 放在了同一层级,导致LaTeX的颜色设置命令根本没被加载到PDF输出流程里。
  • 次要语法问题
    第一个LaTeX命令-\usepackage{color}的减号与内容间缺少空格,虽不致命,但破坏了YAML列表的语法规范。

修复后的完整代码:

---
title: "number_sections"
output:
  pdf_document:
    number_sections: yes
    toc_depth: 4
    toc: true
    header-includes:
      - \usepackage{color}
      - \usepackage{sectsty}
      - \allsectionsfont{\color{red}}  
---

# Main Section

The above section and the ones below should be red

## 2nd Level

### 3rd Level

# Second Section

## another section

### yet another

#### and the last one please

修改说明:

  • 把 header-includes 缩进至 pdf_document 下方,让R Markdown能正确识别这些LaTeX配置
  • 统一所有列表项的格式(减号后加空格),保证YAML语法正确

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 23:40:48