\arraystretch 和 m 列定义的垂直对齐问题

\arraystretch 和 m 列定义的垂直对齐问题

由于某种原因,在m{}列中,一旦\arraystretch将行高推到文本高度之上,文本就不再绝对对齐。相反,第一行垂直居中,其余行位于其下方。有什么想法可以解释为什么会这样以及/或者如何修复它?

\documentclass{article}
\usepackage{array}
\begin{document}
\renewcommand{\arraystretch}{4}
\noindent\begin{tabular}{m{3in}c}
    \hline
    [19:21] time to take the tree outside\newline
    [19:21] but I just let him back in! & 
    [19:21] time to take the tree outside\\
\hline
\end{tabular}
\end{document}  

好的,这是更完整的版本。

\documentclass{article}
\usepackage{array}
\usepackage{tabularx}
\newcommand{\ccol}{\centering\arraybackslash}
\renewcommand{\tabularxcolumn}[1]{>{\ccol}m{#1}}
\begin{document}
\renewcommand{\arraystretch}{4}
\noindent\begin{tabularx}{\textwidth}{m{2.1in}X}
    \hline
    [19:21] time to take the tree outside\newline
    [19:21] but I just let him back in! & 
    [19:21] time to take the tree outside\\
    \hline
\end{tabularx}

\结束{文档}

答案1

这是使用 时的默认行为\arraystretch。原因是tabular(或array)中的内容是按照传统方式设置的\baselineskip,带有额外的水平线,使得条目看起来似乎未在每行中垂直居中。修改(增加)\arraystretch会夸大这种明显的错位。

我建议在需要“空气”的行上方/下方插入空行,如以下 MWE 所示:

在此处输入图片描述

\documentclass{article}
\usepackage{array}% http://ctan.org/pkg/array
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\setlength{\parindent}{0pt}% No paragraph indent
\newcommand{\ccol}{\centering\arraybackslash}
\renewcommand{\tabularxcolumn}[1]{>{\ccol}m{#1}}
\begin{document}
\textbullet~No modification of \verb|\arraystretch|: \par
  \begin{tabularx}{\linewidth}{m{2.1in}X}
    \hline
    [19:21] time to take the tree outside\newline
    [19:21] but I just let him back in! & 
    [19:21] time to take the tree outside\\
    \hline
  \end{tabularx} \par\medskip

\textbullet~\verb|\arraystretch=4| \par
  \renewcommand{\arraystretch}{4}%
  \begin{tabularx}{\linewidth}{m{2.1in}X}
    \hline
    [19:21] time to take the tree outside\newline
    [19:21] but I just let him back in! & 
    [19:21] time to take the tree outside\\
    \hline
  \end{tabularx} \par\medskip

\textbullet~\verb|\arraystretch=1| \par
  \renewcommand{\arraystretch}{1}%
  \begin{tabularx}{\linewidth}{m{2.1in}X}
    \hline \\ \\\mbox{}%
    [19:21] time to take the tree outside\newline
    [19:21] but I just let him back in! & 
    [19:21] time to take the tree outside \\ \\ \\
    \hline
  \end{tabularx}
\end{document}

请注意,在 中使用方括号开始新行tabular可能会导致插入空行时出现问题,因为\\是一个接受可选参数的控制序列: 。为了避免这种情况,我在 之后(最后一个)\\[<len>]插入了一个空行,否则 TeX 会尝试执行,并且不是有效的 TeX 长度。\mbox{}\\tabular\\[19:21]19:21

相关内容