抑制阵列上方和下方的垂直空间?

抑制阵列上方和下方的垂直空间?

环境array使用\arraystretch\extrarowheight如果array已加载包)在行之间添加“前导” array。此外,此垂直空间被添加到第一行上方和最后一行下方。

有没有办法抑制第一行上方和最后一行下方的空间,同时仍然保留行间空间array

myarray使用and mysolidarray(设置\arraystretch为)比较输出,0如以下代码所定义。我在两者之间寻找某种东西。

在此处输入图片描述

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

答案1

您可以对数组进行两次排版测量,首先获取第一行的高度,然后获取最后一行的深度。

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

在此处输入图片描述

答案2

你是什​​么意思\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}

在此处输入图片描述

答案3

如果你不使用垂直规则(无论如何都推荐使用),你可以在 表格的第一个 \hline 之后和最后一个 \hline 之前使用\addlinespace[some value]from 来进行完全控制:booktabs

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

在此处输入图片描述

相关内容