# Suppress vertical space above and below array?

The array environment uses \arraystretch (and \extrarowheight if the array package is loaded) to add "leading" between the rows of the array. In addition, this vertical space is added above the first row and below the last row.

Is there a way to suppress the space above the first row and below the last row while still preserving the inter-row space of the array?

Compare the output using myarray and mysolidarray (which sets \arraystretch to 0) as defined in the following code. I am looking for something in between the two.

\documentclass{standalone}
\usepackage{array}
\usepackage{amsmath}

\newenvironment{myarray}%
{\begin{array}[t]{@{}l@{}}}%
{\end{array}}

\newenvironment{mysolidarray}%
{%
\def\arraystretch{0}%
\begin{array}[t]{@{}l@{}}
}%
{\end{array}}

\begin{document}
\fboxsep=0pt
\begin{tabular}{l}
\verb|$P + Q$|: \fbox{$P + Q$}
\\
\verb|myarray|:
\fbox{$\begin{myarray} P + Q \end{myarray}$}
\fbox{$\begin{myarray} P + {} \\ Q \end{myarray}$}
\\
\verb|mysolidarray|:
\fbox{$\begin{mysolidarray} P + Q \end{mysolidarray}$}
\fbox{$\begin{mysolidarray} P + {} \\ Q \end{mysolidarray}$}
\end{tabular}
\end{document}


You can measure the array typesetting it twice, first for getting the height of the first row, then for the depth of the last row.

\documentclass{article}
\usepackage{array}
\usepackage{environ}

\makeatletter
\NewEnviron{mysolidarray}[1]{%
\begingroup
\def\arraystretch{0}%
\extrarowheight=\z@
\sbox\z@{$\begin{array}[t]{#1}\BODY\end{array}$}%
\sbox\tw@{$\begin{array}[b]{#1}\BODY\end{array}$}%
\expandafter\endgroup\expandafter
\firstlineheight\the\ht\expandafter\z@\expandafter
\lastlinedepth\the\dp\tw@\relax
\sbox{\solidarraybox}{$\begin{array}[t]{#1}\BODY\end{array}\m@th$}
\ht\solidarraybox=\firstlineheight
\dp\solidarraybox=\dimexpr\dp\solidarraybox-\dp\@arstrutbox+\lastlinedepth\relax
\box\solidarraybox
}
\makeatother
\newdimen\firstlineheight
\newdimen\lastlinedepth
\newsavebox{\solidarraybox}

\begin{document}
\fboxsep=0pt
\begin{tabular}{lll}
\verb|$P + Q$| & \fbox{$P + Q$}
\\
\verb|array|&
\fbox{$\begin{array}[t]{@{}l@{}} P + Q \end{array}$}
\fbox{$\begin{array}[t]{@{}l@{}} P + {} \\ Q \end{array}$}
&\extrarowheight=2pt
\fbox{$\begin{array}[t]{@{}l@{}} P + Q \end{array}$}
\fbox{$\begin{array}[t]{@{}l@{}} P + {} \\ Q \end{array}$}
\\
\verb|mysolidarray|&
\fbox{$\begin{mysolidarray}{@{}l@{}} P + Q \end{mysolidarray}$}
\fbox{$\begin{mysolidarray}{@{}l@{}} P + {} \\ Q \end{mysolidarray}$}
&\extrarowheight=2pt
\fbox{$\begin{mysolidarray}{@{}l@{}} P + Q \end{mysolidarray}$}
\fbox{$\begin{mysolidarray}{@{}l@{}} P + {} \\ Q \end{mysolidarray}$}
\end{tabular}
\end{document}


• A bit hacky, but it deserved an upvote just for the chain of \expandafters (I’m especially delighted by ...\the\ht\expandafter\z@...)! – GuM Apr 23 '17 at 0:33

You mean something \setlength{\extrarowheight}{-1pt} ?

\documentclass{standalone}
\usepackage{array}
\usepackage{amsmath}

\newenvironment{myarray}%
{ \setlength{\extrarowheight}{-1pt}
\begin{array}[t]{@{}l@{}}}%
{\end{array}}

\newenvironment{mysolidarray}%
{%
\def\arraystretch{0}%
\begin{array}[t]{@{}l@{}}
}%
{\end{array}}

\begin{document}
\fboxsep=0pt
\begin{tabular}{l}
\verb|$P + Q$|: \fbox{$P + Q$}
\\
\verb|myarray|:
\fbox{$\begin{myarray} P + Q \end{myarray}$}
\fbox{$\begin{myarray} P + {} \\ Q \end{myarray}$}
\\
\verb|mysolidarray|:
\fbox{$\begin{mysolidarray} P + Q \end{mysolidarray}$}
\fbox{$\begin{mysolidarray} P + {} \\ Q \end{mysolidarray}$}
\end{tabular}
\end{document}


• Thanks for the answer, but this doesn't quite do what I want. First, it affects all rows -- compare the normal inter-row spacing with the inter-row spacing when \setlength{\extrarowheight}{-1pt} is used. Second, it doesn't affect the extra space added after the last row. Is there some way to use \extrarowheight to correct these two deficiencies? – Henry DeYoung Apr 22 '17 at 20:56

If you don't use vertical rules (which is recommended anyway), you can use \addlinespace[some value] from booktabs just after the first \hline and before the last \hline of the table to have full control:

\documentclass{article}
\usepackage{array, booktabs}
\usepackage{amsmath}
\usepackage[table]{xcolor}

\newenvironment{myarray}%
{\begin{array}[t]{@{}l@{}}}%
{\end{array}}

\newenvironment{mysolidarray}%
{%
\def\arraystretch{0}%
\begin{array}[t]{@{}l@{}}
}%
{\end{array}}

\begin{document}
\fboxsep=0pt
\begin{tabular}{l}
\verb|$P + Q$|: \fbox{$P + Q$}
\\
\verb|myarray|:
\fbox{$\begin{myarray} P + Q \end{myarray}$}
\fbox{$\begin{myarray} P + {} \\ Q \end{myarray}$}
\\
\verb|mysolidarray|:
\fbox{$\begin{mysolidarray} P + Q \end{mysolidarray}$}
\fbox{$\begin{mysolidarray} P + {} \\ Q \end{mysolidarray}$}
\\[3ex]
\verb|othersolidarray|:
$\begin{array}{@{}l@{}}% \arrayrulecolor{red} \hline\addlinespace[-1pt] P + Q \\ Q + P \\ \addlinespace[-1.25pt] \hline \end{array}$
$\begin{array}{@{}l@{}}% \arrayrulecolor{red} \hline\addlinespace[-1pt] P + {} \\ Q \\ \addlinespace[-1.25pt] \hline \end{array}$
\end{tabular}

\end{document}