如何缩小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-8truemmvalue (try-6truemmor-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
相关产品推荐
相关产品推荐

