我正在处理一个复杂的表格,有多列和多行,每列都需要固定宽度。当一个单元格有一个长句子时,我希望单元格宽度固定,而同一行中的其他单元格垂直居中对齐。我找到了一种方法,使用
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
但如果单元格是多列的,则此方法不起作用。
以下是我的示例:
\usepackage{multirow, hhline}
\usepackage{array,booktabs}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage{colortbl}
\begin{table}[htbp]
\centering
\caption{Add caption}
\begin{tabular}{rp{0.15\textwidth}p{0.15\textwidth}M{0.35\textwidth}M{0.25\textwidth}}
\multicolumn{1}{c}{\multirow{2}[0]{*}{}} & \multicolumn{2}{c}{MultiColumn1} & \multicolumn{2}{c}{MultiColumn2} \\
\hhline{~----}
\multicolumn{1}{c}{} & column1 & column2 & column3 & column4 \\
\hhline{-----}
row1 & \multicolumn{2}{p{0.3\textwidth}}{\cellcolor{blue!25} I want this sentence vertically aligned in center} & Here comes a long sentence. Here comes a long sentence. Here comes a long sentence. & Here comes a long sentence. Here comes a long sentence. Here comes a long sentence. \\
\hhline{-----}
row2 & Contents 1 & Contents 2 & Here comes a long sentence. Here comes a long sentence. Here comes a long sentence. & Here comes a long sentence. Here comes a long sentence. Here comes a long sentence. \\
\end{tabular}%
\label{tab:addlabel}%
\end{table}%
如下所示。 我希望彩色单元格中的句子垂直居中对齐。有人有想法吗?
谢谢。
答案1
您几乎已经达到目标 :-)。要使蓝色单元格居中,您只需使用。我允许自己删除表格中不必要的代码。另外,不要使用“hhline˙”包(我还没有安装它),无论如何,对我来说,使用包\multicolumn{2}{M{0.3\textwidth}}{....}
中定义的规则更好。booktab
\documentclass{article}
%\usepackage{multirow, hhline}
\usepackage{array,makecell,booktabs}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage[svgnames,table]{xcolor}
%\usepackage{ragged2e}
\begin{document}
\begin{table}
\centering
\caption{Add caption}
\begin{tabular}{rp{0.15\textwidth}p{0.15\textwidth}
M{0.35\textwidth}M{0.25\textwidth}}
\cmidrule[1pt]{2-5} \cmidrule(l){4-5}
& \multicolumn{2}{c}{MultiColumn1}
& \multicolumn{2}{c}{MultiColumn2} \\
\cmidrule(r){2-3} \cmidrule(l){4-5}
& column1 & column2 & column3 & column4 \\
\hline
row 1
& \multicolumn{2}{M{0.3\textwidth}}{\cellcolor{blue!25}
I want this sentence vertically aligned in center}
& Here comes a long sentence. Here comes a long sentence.
Here comes a long sentence.
& Here comes a long sentence. Here comes a long sentence.
Here comes a long sentence. \\
\hline
row 2
& Contents 1 & Contents 2 & Here comes a long sentence.
Here comes a long sentence.
Here comes a long sentence.
& Here comes a long sentence.
Here comes a long sentence.
Here comes a long sentence. \\
\bottomrule
\end{tabular}%
\label{tab:addlabel}%
\end{table}%
\end{document}
如果您喜欢使用 进行单元格格式化,我建议您查看,\Centering
因为它提供了包ragged2e
。使用它,文本将居中,但长单词仍会用连字符连接。使用它,您可以获得:
编辑:考虑到@LaRiFaRi 的评论,您可以获得替代的、更好的表格宽度:
\documentclass{article}
\usepackage{array,makecell,booktabs}
\newcolumntype{M}[1]{>{\Centering\arraybackslash}m{#1}}
\usepackage[svgnames,table]{xcolor}
\usepackage{calc,ragged2e}
\usepackage{microtype}
\begin{document}
\begin{table}
\centering
\caption{Add caption}
\begin{tabular}{rp{0.15\textwidth}p{0.15\textwidth}
M{0.35\textwidth}M{0.25\textwidth}}
\cmidrule[1pt]{2-5} %\cmidrule(l){4-5}
& \multicolumn{2}{c}{MultiColumn1}
& \multicolumn{2}{c}{MultiColumn2} \\
\cmidrule(r){2-3} \cmidrule(l){4-5}
& column1 & column2 & column3 & column4 \\
\hline
row 1
& \multicolumn{2}{M{0.3\textwidth+2\tabcolsep}}{\cellcolor{blue!25}
I want this sentence vertically aligned in center}
& Here comes a long sentence. Here comes a long sentence.
Here comes a long sentence.
& Here comes a long sentence. Here comes a long sentence.
Here comes a long sentence. \\
\hline
row 2
& Contents 1 & Contents 2 & Here comes a long sentence.
Here comes a long sentence.
Here comes a long sentence.
& Here comes a long sentence.
Here comes a long sentence.
Here comes a long sentence. \\
\bottomrule
\end{tabular}%
\label{tab:addlabel}%
\end{table}%
\end{document}
答案2
对我来说,M
似乎有效\multicolumn
,还是我理解错了?
% arara: pdflatex
\documentclass{article}
\usepackage{hhline}
\usepackage{array,booktabs}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage[table]{xcolor}
\usepackage{caption} % better spacing around the caption
\begin{document}
\setcounter{table}{1}
\begin{table}[htbp]
\centering
\caption{Add caption}\label{tab:addlabel}
\begin{tabular}{rM{0.15\textwidth}M{0.15\textwidth}M{0.35\textwidth}M{0.25\textwidth}}
& \multicolumn{2}{c}{MultiColumn1} & \multicolumn{2}{c}{MultiColumn2} \\
\hhline{~----}
& column1 & column2 & column3 & column4 \\
\hhline{-----}
row1 & \multicolumn{2}{M{0.3\textwidth}}{\cellcolor{blue!25} I want this sentence vertically aligned in center} & Here comes a long sentence. Here comes a long sentence. Here comes a long sentence. & Here comes a long sentence. Here comes a long sentence. Here comes a long sentence. \\
\hhline{-----}
row2 & Contents 1 & Contents 2 & Here comes a long sentence. Here comes a long sentence. Here comes a long sentence. & Here comes a long sentence. Here comes a long sentence. Here comes a long sentence. \\
\end{tabular}
\end{table}
\end{document}
不过,我建议加载该包calc
并执行\multicolumn{2}{M{.3\textwidth+2\tabcolsep}}
此操作。