我正在尝试创建一个具有以下外观的表格。
知道为什么第二行和第三行合并后的第一列没有像第三行那样在中间垂直对齐吗?
同样的问题也出现在第四行第三列和第五列,文本再次没有在中间对齐???
我不确定使用 XeLaTex(而不是使用 pdfLaTex)进行编译是否重要。
非常感谢
答案1
- 不要使用
\resizebox
,否则您将无法控制字体大小。字体可能会变得太小而无法阅读。 - 您的 MWE 生成的表格(未使用
\resizebox
)比文本宽度更宽。要解决此问题,您可以选择以下可能性之一:- 使其
\textwidth
更广泛,例如使用\geometry
包 - 减小字体和
\tabcol
大小 - 提前定义表格的宽度并使用列的类型,这样可以自动将文本分成多行(如果需要)。例如,
tabularxy
表格环境很有前景。
- 使其
- 使用包
\thead
中的指令makecell
,可以为列标题编写更短、更干净的代码 - 可以使用
multirow
包的新功能来定义列宽:- 使用选项
=
多行单元格超越列的定义宽度。 - 单元格
\renewcommand\multirowsetup{\centering}
中的文本水平居中multirow
- 使用选项
完成 MWE:
\documentclass{article}
\usepackage{makecell, multirow, tabularx}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\begin{document}
\begin{table}[ht] % <---
\small
\setlength\tabcolsep{3pt}
\setcellgapes{2pt}
\makegapedcells
\renewcommand\multirowsetup{\centering} % <-----
\begin{tabularx}{\linewidth}{ |p{3em}|c| *{3}{>{\centering\arraybackslash}X|} }
\hline
\thead{Two\\ Lines}
& \thead{Line}
& \thead{Two\\ Lines}
& \thead{Two\\ Lines}
& \thead{Two\\ Lines} \\
\hline
\multirow{7}{=}{Three\\ -\\ Lines}
& P1
& Text that I would wish to span in two Lines
& Text that I would wish to span in four Lines
& Text that I would wish to span in four Lines
\\
\cline{2-5}
& P2
& Text that I would wish to span in two Lines
& Text that I would wish to span in four Lines
& Text that I would wish to span in four Lines
\\
\hline
\multirow{13}{=}{Three\\ -\\ Lines}
& A1
& \multirow{13}{=}{Text common to all rows Vertically Centered in the Middle}
& Text that I would wish to span in two Lines
& \multirow{13}{=}{Text common to all rows Vertically Centered in the Middle} \\
\cline{2-2} \cline{4-4}
& A2
& & Text that I would wish to span in two Lines
& \\
\cline{2-2} \cline{4-4}
& A3
& & Text that I would wish to span in two Lines
& \\
\cline{2-2} \cline{4-4}
& A4
& & Text that I would wish to span in two Lines
& \\
\hline
\multirow{8}{=}{Three\\ -\\ Lines}
& A5
& \multirow{8}{=}{Text common\\ to all rows\\ Vertically Centered\\ in the Middle}
& One Line Text
& \multirow{8}{=}{Text common\\ to all rows\\ Vertically Centered\\ in the Middle} \\
\cline{2-2} \cline{4-4}
& A6
& & One Line Text & \\
\cline{2-2} \cline{4-4}
& A7
& & One Line Text & \\
\cline{2-2} \cline{4-4}
& A8
& & One Line Text & \\
\cline{2-2} \cline{4-4}
& A9
& & One Line Text & \\
\cline{2-2} \cline{4-4}
& A10
& & One Line Text & \\
\hline
\end{tabularx}%
%}
\caption{TEST}
\label{tab:my-table}
\end{table}
\end{document}
(红线表示文本边框)
答案2
你误解了多行。您必须跨越“正常”行数,因为多行不知道跨度框的高度,请参见手册第 6 页及其给出的示例:
- 中间一列是 \multirow。您可能希望它垂直居中,但事实并非如此。这是因为 \multirow 不知道框的高度。\multirow 唯一能估计的高度是行数和一行的正常高度。它试图将文本居中在该空间,但在此示例中该空间太低。因此文本位于框的顶部。如果您希望它居中,则必须提供 hvmovei 参数以将其向下移动。
只需增加跨越行的数量,如下所示,或利用可选参数进行微调[vmode=]
:
\documentclass{article}
\usepackage{geometry}
\usepackage{multirow}
\usepackage{graphicx}
\begin{document}
\begin{table}[]
\centering
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{|c|c|c|c|c|}
\hline
\textbf{\begin{tabular}[c]{@{}c@{}}Two\\ Lines\end{tabular}} &
\textbf{Line} &
\textbf{\begin{tabular}[c]{@{}c@{}}Two\\ Lines\end{tabular}} &
\textbf{\begin{tabular}[c]{@{}c@{}}Two\\ Lines\end{tabular}} &
\textbf{\begin{tabular}[c]{@{}c@{}}Two\\ Lines\end{tabular}} \\ \hline
\multirow{5}{*}{\begin{tabular}[c]{@{}c@{}}Three\\ -\\ Lines\end{tabular}} &
P1 &
\begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
\begin{tabular}[c]{@{}c@{}}Text that\\ I would\\ wish to span\\ in four Lines\end{tabular} &
\begin{tabular}[c]{@{}c@{}}Text that\\ I would \\ wish to span\\ in four Lines\end{tabular} \\ \cline{2-5}
&
P2 &
\begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
\begin{tabular}[c]{@{}c@{}}Text that\\ I would\\ wish to span\\ in four Lines\end{tabular} &
\begin{tabular}[c]{@{}c@{}}Text that\\ I would\\ wish to span\\ in four Lines\end{tabular} \\ \hline
\multirow{7}{*}{\begin{tabular}[c]{@{}c@{}}Three\\ -\\ Lines\end{tabular}} &
A1 &
\multirow{7}{*}{\begin{tabular}[c]{@{}c@{}}Text common\\ to all rows\\ Vertically Centered\\ in the Middle\end{tabular}} &
\begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
\multirow{7}{*}{\begin{tabular}[c]{@{}c@{}}Text common\\ to all rows\\ Vertically Centered\\ in the Middle\end{tabular}} \\ \cline{2-2} \cline{4-4}
&
A2 &
&
\begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
\\ \cline{2-2} \cline{4-4}
&
A3 &
&
\begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
\\ \cline{2-2} \cline{4-4}
&
A4 &
&
\begin{tabular}[c]{@{}c@{}}Text that I would wish\\ to span in two Lines\end{tabular} &
\\ \hline
\multirow{6}{*}{\begin{tabular}[c]{@{}c@{}}Three\\ -\\ Lines\end{tabular}} &
A5 &
\multirow{6}{*}{\begin{tabular}[c]{@{}c@{}}Text common\\ to all rows\\ Vertically Centered\\ in the Middle\end{tabular}} &
One Line Text &
\multirow{6}{*}{\begin{tabular}[c]{@{}c@{}}Text common\\ to all rows\\ Vertically Centered\\ in the Middle\end{tabular}} \\ \cline{2-2} \cline{4-4}
&
A6 &
&
One Line Text &
\\ \cline{2-2} \cline{4-4}
&
A7 &
&
One Line Text &
\\ \cline{2-2} \cline{4-4}
&
A8 &
&
One Line Text &
\\ \cline{2-2} \cline{4-4}
&
A9 &
&
One Line Text &
\\ \cline{2-2} \cline{4-4}
&
A10 &
&
One Line Text &
\\ \hline
\end{tabular}%
\caption{TEST}
\label{tab:my-table}
\end{table}
\end{document}