使用knitr从Rmarkdown创建PDF时,减少标题边距

3
我正在使用knitr + Rmarkdown创建PDF文档。我在yaml文件头中设置了geometry: margin=0.1in,该边距应用于文档正文,但是标题与文档顶部还有正文之间的距离很远。下面是截图:enter image description here。下面是创建上述文档的Rmd代码:
---
title: "test"
output: 
  pdf_document:
    latex_engine: xelatex
geometry: margin=0.1in
---

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

a bunch of text to demonstrate that the margin applies only to the body text of
this document, while the title (above) remains quite far from the top of the
document, and this is messing up my plan to make a document that's only one page
in length

我需要将此文档长度限制在一页内。标题的较大空白导致了问题。如何缩小标题、文档顶部和正文之间的空白?

注:由于某些原因,我只能在此处使用xelatex latex引擎。

1个回答

5
使用LaTeX代码,您可以:
  • 使用\vspace{-1cm}减少标题和文本之间的间距

  • 在YAML中的title中放置相同的代码以减少顶部边距

以下是您的示例:
---
title: \vspace{-1.5cm} test
output: 
  pdf_document:
  latex_engine: xelatex
geometry: margin = 0.1in
---

\vspace{-1cm}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

a bunch of text to demonstrate that the margin applies only to the body text of
this document, while the title (above) remains quite far from the top of the
document, and this is messing up my plan to make a document that's only one page in length

7
请注意,如果您的标题被引用,则 vspace 呼叫需要在引号内并带有额外的转义斜杠,例如 "\\vspace{-1.5cm} test"。 - DHW
感谢@DHW提供这个重要的提示。我只想补充一点,它只适用于花括号,这是我第一次尝试时忽略了的(来自R)。 - Agile Bean
@AgileBean 相对于什么?它在其他上下文中使用不同的字符也能工作吗?我不知道。 - DHW
我刚才视力不好,打了\vspace(-1in)。没注意到它们是花括号。 - Agile Bean
1
我想要的是一个解决方案,不修改标题,这样在编织到HTML时就不会出现问题。 - randy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接