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

如何缩小Rmarkdown PDF输出中标题与目录的间距?

Hey there! Let's sort out that annoying spacing between your "Supplementary Material" line and the table of contents (TOC) in your R Markdown PDF.

The issue here is that the TOC generated by pdf_document has its own default spacing above it, so your existing \vspace{-5truemm} only affects the space before the first section, not the TOC itself. Here's a straightforward fix by adding a LaTeX command to adjust the spacing right above the TOC:

Modified R Markdown Code

---
title: |
  | This is title
  | \vspace{-5truemm}Supplementary Material\vspace{-5truemm}
author: 'xxx^[Correspondence to: xxx. Email: xxx]'
geometry: margin=0.3in
fontfamily: mathpazo
fontsize: 11pt
documentclass: article
urlcolor: blue
output:
  pdf_document:
    toc: true
    toc_depth: 4
    number_sections: yes
    subparagraph: yes
header-includes: |
  \usepackage{titlesec}
  \titlespacing{\title}{0pt}{\parskip}{-\parskip}
  % Add this line to reduce space above TOC
  \addtocontents{toc}{\vspace{-8truemm}}
---
% Keep this existing vspace to adjust space after the supplementary material line
\vspace{-5truemm}
# Introduction
This is introduction...
# Installation
This is installation...
## test1
### subtest1

How It Works

  • The \addtocontents{toc}{\vspace{-8truemm}} command inserts a negative vertical space directly at the start of the TOC. You can tweak the -8truemm value (try -6truemm or -10truemm) until you get the exact tight spacing you want.
  • Your original \vspace{-5truemm} after the YAML frontmatter helps shrink the gap between the "Supplementary Material" line and the TOC area, working with the TOC-specific adjustment to achieve your desired layout.

If you still notice extra space, you can experiment with adjusting the titlespacing parameters further, but the \addtocontents trick is usually the most direct fix for TOC-related spacing.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 12:02:37