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

如何在R Markdown中居中图片标题

解决R Markdown中knitr图片标题居中的问题

以下是不同输出场景下的解决方案:

针对HTML输出

全局统一设置(推荐)

在文档中嵌入CSS代码块,一次性让所有图片标题居中:

.caption {
  text-align: center;
}

同时确保YAML头部开启图片标题功能:

---
title: "你的文档标题"
output: html_document
fig_caption: true
---

单张图片临时设置

直接在fig.cap里用HTML标签包裹标题:

knitr::include_graphics("tmap01.png")

针对PDF输出

在YAML头部添加LaTeX依赖,并配置标题居中规则:

---
title: "你的文档标题"
output:
  pdf_document:
    fig_caption: true
    extra_dependencies:
      - caption
---

然后在文档开头插入LaTeX命令:

\captionsetup[figure]{justification=centering}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 17:10:19