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

如何调整R Markdown生成PDF时标题与正文的间距?

Adjusting Title-Body Spacing in R Markdown PDF Output (YAML-Only Approach)

Hey there! I get that you want to tweak the spacing between your title and the main body text in your PDF without messing around with complex TeX or pandoc commands. Lucky for you, you can do this directly in your YAML header with just a few lines of simple TeX snippets (don’t worry—you don’t need to understand them fully to use them!).

Here’s how to modify your existing YAML:

---
title: "Predicting Diabetes Onset"  # Your existing title
output:
  pdf_document:
    # Keep any existing settings you have here (like toc: true, etc.)
header-includes:
  - \usepackage{titling}  # This LaTeX package helps adjust title formatting
  - \posttitle{\vspace{1cm}}  # Adds 1cm of space AFTER the title
---

What this does:

  • The titling package is a lightweight LaTeX tool designed specifically for customizing title elements.
  • \posttitle{\vspace{1cm}} adds a vertical space of 1 centimeter right after your title. You can change the 1cm value to whatever works for you—try 0.5cm for tighter spacing or 2cm for more breathing room.

If you also have a subtitle and want to adjust the space between the subtitle and the body, just add this line to the header-includes section:

- \postsubtitle{\vspace{0.8cm}}  # Adjusts space after the subtitle

Just drop this modified YAML into your R Markdown file, knit to PDF, and you’ll see the spacing change immediately. No need to dive into the nitty-gritty of TeX or pandoc configuration!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:11:52