有什么方法可以防止 R Markdown 将 LaTeX 环境放入新段落中?

Any way to keep R Markdown from putting a LaTeX environment in a new paragraph?

aligngather 这样的环境显然是为在一段 LaTeX 文本中使用而设计的,因为文档文本和数学环境开头之间的两个换行符插入了一个惊人的两个段落的垂直白色 space。但是,Markdown 始终在其上方文本下方两行开始任何 LaTeX 环境,即使您在 markdown code/text 的同一行开始环境,即使您放置 2 spaces在它之前添加一个换行符。由于 markdown 没有原生的多行数学显示,这造成了一个两难选择。

运行 \vspace{-\baselineskip} 在环境补偿足够好之前,当然最好只是告诉 markdown 不要首先插入换行符。那可能吗?如果没有,那么在每个对齐(and/or 对齐*、收集、收集* 等)环境开始之前自动 运行 \vspace{-\baselineskip} 的最简单方法是什么?

MWE:

---
output:
  pdf_document:
    keep_tex: 1
---

The following environment will get marked up with an extra two lines between it and 
this text, putting it on a new paragraph and creating a lot of whitespace above it, 
whether or not there's any line breaks in the markdown code:
\begin{gather*}
A \ B
\end{gather*}

This can of course be hackily corrected by subtracting vertical space: 
\vspace{-\baselineskip} \begin{gather*}
A \ B
\end{gather*}

在这种情况下,您可以做的最好的事情是使用 etoolbox package:

在每个特定环境的开头自动插入 \vspace{-\baselineskip}
---
output:
  pdf_document:
    keep_tex: 1
header-includes:
  - \usepackage{etoolbox}
  - \AtBeginEnvironment{gather}{\vspace{-\baselineskip}}
  - \AtBeginEnvironment{gather*}{\vspace{-\baselineskip}}
---

The following environment will get marked up with an extra two lines between it and 
this text, putting it on a new paragraph and creating a lot of whitespace above it, 
whether or not there's any line breaks in the markdown code:
\begin{gather*}
A \ B
\end{gather*}

This can of course be hackily corrected by subtracting vertical space: 
\begin{gather*}
A \ B
\end{gather*}

然而,这并不是最佳选择,因为环境插入的间隙取决于前一段结尾的文本量。由于Pandoc的处理,金额始终不变(\abovedisplayskip),所以使用

可能是"better"
header-includes:
  - \usepackage{etoolbox}
  - \AtBeginEnvironment{gather}{\vspace{\dimexpr-\baselineskip-\abovedisplayskip}}
  - \AtBeginEnvironment{gather*}{\vspace{\dimexpr-\baselineskip-\abovedisplayskip}}

您必须对所有 amsmath 相关的显示对齐方式执行此操作。