[Tex/LaTex] Spurious space above align environment at top of page/minipage

alignspacing

It seems that the align environment adds additional vertical space if it starts at the top of a page or minipage. Below are two pages, and two minipages (on Page 1), where the first page/minipage has text followed by an align* environment, and the second page/minipage starts with an align* environment, and note that the equations are still aligned.

enter image description here

Attempted Solution:

The closest related question I found was:

I attempted to use the solution by Gonzalo Medina at the above by adding \setlength\abovedisplayskip{0pt} (after the preamble as per Vertical space before and after align environment). Although, this did move the equations up vertically, they were still vertically aligned:

enter image description here

And since the problem occurs at top of a page as well, I don't think this is related to Understanding minipages – aligning at top

Question:

  1. How can I adjust the align environment to automagically not have additional space at the top of a page or minipage?
  2. Is there a reason for this behavior, and perhaps should not be changed?

Code:

\documentclass{article}
\usepackage[showframe,paperwidth=3.5in,paperheight=2.2in,margin=0.5in]{geometry}
\usepackage{amsmath}

\begin{document}%\setlength\abovedisplayskip{0pt}
Top of Page 1
\begin{align*}% Text above at top of page
 e &= m c^2
\end{align*}

\hrule
\begin{minipage}[t]{0.45\linewidth}
    Top of minipage 1
    \begin{align*}% Text above this minipage
     e &= m c^2
    \end{align*}
\end{minipage}
\begin{minipage}[t]{0.45\linewidth}
    \begin{align*}% No text above in this minipage
     e &= m c^2
    \end{align*}
\end{minipage}

\newpage
\begin{align*}% No text above at top of page
 e &= m c^2
\end{align*}
\end{document}

Best Answer

The alignment environments of amsmath are not designed to be at the start of a paragraph. While this can be acceptable in normal situations, this has bad effects when we use a minipage for typesetting an alignment at a reduced width.

The minipage problem can be solved by patching the \start@align command (and also \start@gather and \start@multline) with

\usepackage{etoolbox}
\makeatletter
\pretocmd\start@align{%
  \if@minipage\kern-\topskip\kern-\abovedisplayskip\fi
}{}{}
\makeatother

This exploits the fact that at the start of a minipage the conditional \if@minipage is true, but upon starting a paragraph it becomes false, so subsequent align environments in the same minipage won't be affected.

There is a similar patch for "curing" the environments at start-of-paragraph, but I'm somewhat reluctant to show it, as I believe that a paragraph should never start with a displayed equation (with the above mentioned exception).

Related Question