表格自定义单元格垂直对齐方式

表格自定义单元格垂直对齐方式

我想创建一个表格,让单元格的内容垂直对齐到顶部,即默认行为,但我还希望某些单元格的内容垂直居中。以下是示例:

\begin{tabular}{p{4cm}p{5cm}p{5cm}p{5cm}}
    \toprule
    This cell is vertically centered. &
    This cell is aligned to the top. It's also going to be a lot longer than the rest, defining the maximum height of the row. &
    This cell is aligned to the top as well. &
    This is another vertically centered cell. \\
    \midrule
    This cell should be vertically centered. &
    This one too. &
    This one has regular alignment, i.e. to the top. It's also the longest cell on the row. &
    This one is regular too. \\
    \bottomrule
\end{tabular}

如何使第一行的第一个和最后一个单元格以及第二行的前两个单元格垂直居中(即位于中间)?以下方法似乎不起作用:

\begin{tabular}{p{4cm}p{5cm}p{5cm}p{5cm}}
    \toprule
    \multicolumn{1}{m{4cm}}{This cell is vertically centered.} &
    This cell is aligned to the top. It's also going to be a lot longer than the rest, defining the maximum height of the row. &
    This cell is aligned to the top as well. &
    \multicolumn{1}{m{4cm}}{This is another vertically centered cell.} \\
    \midrule
    \multicolumn{1}{m{5cm}}{This cell should be vertically centered.} &
    \multicolumn{1}{m{5cm}}{This one too.} &
    This one has regular alignment, i.e. to the top. It's also the longest cell on the row. &
    This one is regular too. \\
    \bottomrule
\end{tabular}

在此处输入图片描述

以下是我想要实现的大致内容(使用 Microsoft Word 制作的表格):

在此处输入图片描述

答案1

你可以用 来实现multirow。与其名称相反,\multirow命令需要计算线,而不是行数。您可能还需要进行一些小的调整才能获得精确的垂直对齐。

在下面的代码中,我必须更改表格列的宽度以避免溢出到右边距:

\documentclass{report}
\usepackage{geometry} %
\usepackage{array}
\usepackage[]{multirow, graphicx, hhline, rotating, makecell, booktabs}


\begin{document}

\noindent\begin{tabular}{@{}p{3cm}*{3}{p{4cm}}@{}}
    \toprule
    \multirow{5}{\linewidth}{This cell is vertically centered.} &
    This cell is aligned to the top. It's also going to be a lot longer than the rest, defining the maximum height of the row. &
    This cell is aligned to the top as well. &
    \multirow{5}{\linewidth}{This is another vertically centered cell.} \\
    \midrule
    \multirow{4}{\linewidth}{This cell should be vertically centered.} &
    \multirow{4}{\linewidth}{This one too.} &
    This one has regular alignment, i.e. to the top. It's also the longest cell on the row. &
    This one is regular too. \\
    \bottomrule
\end{tabular}

\end{document} 

在此处输入图片描述

相关内容