-
\centering在复合列multicolumn中直接使用有时候是无效的。
-
\multirow以及\multicolumn自从latex2.0后只接收10进制的line调整,所以我们需要手动调整具体的位置。
-
在不设置parbox的情况下,multirow的宽带会自动调整,而其高度会默认为最小
这是一个既垂直居中,同时也是水平居中,同时还有不居中的例子:
\renewcommand\arraystretch{1.5}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\begin{table}[t]
\caption{Caption.}
\label{table:label}
\begin{tabularx}{\columnwidth}{| Y | Y | X | X |}
\hline
& Test & Test & Time [minutes] \\
\hline
\multirow{2}{*}{A} & \multirow{2}{*}{100\%} & 100\% & x \\
\cline{3-4}
& & 100\% & y \\
\hline
\multirow{2}{*}[-6pt]{B} & & 100\% & 100\% \\ % ---> [-6pt]
\cline{3-3}
& 100\% & Success rate: 100\% & \\
\hline
\end{tabularx}
\end{table}
latex本身就是一种解释型语言,所以我们可以定义单元格中的字段,并使用X以及Y进行指代,\newcolumntype{Y}{>{\centering\arraybackslash}X}
则定义Y为垂直+水平居中,而X则为普通格式。
但是实际写表格的时候,有时候我们会在其中加入大量的语句用于说明,这时候就不可避免的遇到换行的问题。很多博客提出了不少解决方案,但都只是针对单纯的len进行换行。而对于multirow,一般是不适用的。
主要原因是multirow这一命令在调用的过程中会重新定义宽,高以及文格式。
单纯在\begin{tabular}旁边使用{p{0.3\textwidth}<{\centering}|p{0.3\textwidth}<{\centering}|p{0.3\textwidth}<{\centering}}只会影响原有的line,一旦你使用新的multirow后会覆写原有的行列格式。这回带来一个很麻烦的问题,就算换行了,如果你只给了2行的合并,它最终的高度也只有默认两行的高度总和,这就会导致其出格。
所以,完美的解决方案,就是自己再次修改multirow的形状大小。
分为两步:
- 调整宽度:加上{0.X\textwidth}
- 调整高度:在\后加上[Xex]
一个简单的例子如下:
\documentclass[12pt,a4paper]{article}
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\usepackage{multirow}
\usepackage{array}
\begin{document}
\noindent
\begin{table*}
\begin{tabular}%
{>{\centering\arraybackslash}p{0.3\textwidth}%
>{\centering\arraybackslash}p{0.3\textwidth}%
>{\centering\arraybackslash}p{0.3\textwidth}}
\toprule
\multirow{2}{0.3\textwidth}{\centering On the other hand, cloud-based IoT has also provided a convincing platform to guarantee distributed location-based service (LBS) by periodically collecting and broadcasting certain kinds of passing service information such as all the restaurants satisfying the user's query conditions in the neighborhood and the traffic conditions for specific road sections during required time periods. }
& \multicolumn{2}{c}{Logic of rule adoption} \\[21ex]
\cmidrule{2-3}
& Logic of consequences
& Logic of appropriateness \\
\midrule
EU-driven & External incentives model & Social learning model \\
CEE-driven & Lesson-drawing model & Lesson-drawing model \\
\bottomrule
\end{tabular}
\end{table*}
\end{document}
实际效果:
Tips:
1.如果只想要单独的单元格水平居中,使用\multicolumn{1}{c}{content}
即可。