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

如何在RMarkdown生成的PDF特定章节中居中副标题

问题原因

你用的<h3 align="center">是HTML标签,仅在RMarkdown输出HTML时生效。而生成PDF是基于LaTeX编译的,HTML标签会被忽略或直接以文本形式输出,所以达不到居中效果。

解决方案

改用LaTeX的居中命令即可,以下是两种可行的写法:

方法1:用\begin{center}包裹标题内容

---
title: "example"
author: "X"
date: "2023-09-23"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

Title

\begin{center}Title2\end{center}

summary(cars)
#### 方法2:用`\centering`命令
```yaml
---
title: "example"
author: "X"
date: "2023-09-23"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

Title

\centering Title2\par

summary(cars)
### 说明
两种方法都能让三级副标题`Title2`居中显示,同时保留原有的标题层级和编号。如果需要批量修改标题样式,可以在YAML的`header-includes`中引入LaTeX的`titlesec`包自定义格式,但单个标题居中用上述方法更直接。

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

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

最近更新时间:2026.07.10 10:52:12