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

无法为图表创建蓝色标题,LaTeX编译报错求助

问题原因及解决方法

问题根源

报错Undefined control sequence. \DeclareCaptionFont的核心原因是:你在加载caption宏包之前就调用了它提供的\DeclareCaptionFont命令。LaTeX的规则是必须先加载对应宏包,才能使用包内定义的命令。

修正后的代码

调整宏包加载顺序,把caption包移到\DeclareCaptionFont之前;另外考虑到你使用lualatex引擎,推荐用功能更完善的xcolor包替代color包,完整代码如下:

---
title: "test"
format: 
   pdf: 
     include-in-header:
      - text: | 
           \usepackage{geometry}
           \usepackage{xcolor}
           \usepackage[font=Large,labelfont=bf,textfont={blue,sf}]{caption}
           \DeclareCaptionFont{blue}{\color{blue}}
editor: visual
pdf-engine: lualatex
fig-cap-location: top
lang: nb
---

```{r, figure, fig.height=12, fig.width=10, fig.cap="Blue Caption"}
#| echo: false
library(tidyverse)

data.frame(Year=2000:2002, 
           length=c(4.2, 10, 29.5)) %>% 
  ggplot() + geom_line(mapping = aes(x = Year, y = length))
## 额外说明
- 宏包加载必须遵循「先加载,后使用命令」的逻辑,否则LaTeX无法识别未定义的控制序列
- `xcolor`是`color`包的扩展,支持更多颜色模型和高级功能,适配`lualatex`引擎的表现更好
- 代码中`fig-cap=paste("Blue Caption")`可以简化为`fig-cap="Blue Caption"`,效果完全一致

内容的提问来源于stack exchange,提问作者stenvik team
相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 23:52:32