R Markdown 在标题和水平线之间减少 space

R Markdown reduce space between heading and horizontal rule

我想创建一条水平线来拉伸 pdf 的整个宽度,并且也适用于我的 HTML 从 R Markdown 编制的文件。我遵循如下所示的概述 ,效果非常好。

该解决方案的一个问题是它在标题和水平线之间创建了一个巨大的 'white space' 块。我如何消除或减小这个 'white space'?

这是我使用的方法。首先,一个tex file被称为header.tex,包含:

\let\oldrule=\rule
\renewcommand{\rule}[1]{\oldrule{\linewidth}}

然后在Rmd文件中输入:

---
output: 
  pdf_document:
    includes:
      in_header: header.tex
---

这是结果:

space.png

删除填充

如果你想去除线条周围的填充,你可以在线条定义中设置一个负 vspace 命令作为 shown here:

\let\oldrule=\rule
\renewcommand{\rule}[1]{\vspace{-25pt}\oldrule{\linewidth}}

和 .Rmd 文件:

---
output: 
  pdf_document:
    includes:
      in_header: header.tex
---


# Title

Test

Test

下划线 Header

您最好更改 header 的样式,将下划线作为 # Title 命令的一部分。 This answer 展示了如何在 LaTeX 中实现它

首先,header.tex 文件变为:

\usepackage{titlesec}

\let\oldrule=\rule
\renewcommand{\rule}[1]{\oldrule{\linewidth}}

\titleformat{\section}
  {\normalfont\Large\bfseries}{\thesection}{1em}{}[{\titlerule[0.8pt]}]

还有一个基本的例子。请注意,subparagraph: yes 已包含在内,没有它,titlesec 包将无法使用。参见 here 的解释。

---
output: 
  pdf_document:
    includes:
      in_header: header.tex
subparagraph: yes
---


# Title

Test

Test