1
votes

Environments like align and gather are pretty clearly designed for use within a paragraph of LaTeX text, as two line breaks between the document text and the start of the math environment inserts an egregious two paragraph's worth of vertical white space. Markdown, though, always starts any LaTeX environment two lines below the text that's above it, even if you begin the environment on the very same line of the markdown code/text, and even if you put 2 spaces before it in order to add a single line break. Since there's no multiline math dislay native to markdown, this poses a dilemma.

Running \vspace{-\baselineskip} before the environment compensates well enough, but of course it would be better to just tell markdown not to insert the line breaks in the first place. Is that possible? And if not, then what would be the easiest way to automatically run \vspace{-\baselineskip} before the beginning of each align (and/or align*, gather, gather*, etc.) environment?

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*}
1

1 Answers

0
votes

The best you can do in this situation is to automatically insert \vspace{-\baselineskip} at the start of every specific environment using the etoolbox package:

---
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*}

This, however, is not optimal, as the gap inserted by the environment depends on the amount of text ending the preceding paragraph. As a result of Pandoc's processing, the amount is always the same (\abovedisplayskip), so it may be "better" to use

header-includes:
  - \usepackage{etoolbox}
  - \AtBeginEnvironment{gather}{\vspace{\dimexpr-\baselineskip-\abovedisplayskip}}
  - \AtBeginEnvironment{gather*}{\vspace{\dimexpr-\baselineskip-\abovedisplayskip}}

You'll have to do this for all amsmath-related display alignments.