如何调整R Markdown生成PDF时标题与正文的间距?
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
titlingpackage 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 the1cmvalue to whatever works for you—try0.5cmfor tighter spacing or2cmfor 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

