优雅解决LATEX中\multirow及\multicolumn的单元格的居中及换行问题

首先解释一下Mr-Cat伍可猫为何会遇到这个问题

  1. \centering在复合列multicolumn中直接使用有时候是无效的。

  2. \multirow以及\multicolumn自从latex2.0后只接收10进制的line调整,所以我们需要手动调整具体的位置。

  3. 在不设置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的形状大小。
分为两步:

  1. 调整宽度:加上{0.X\textwidth}
  2. 调整高度:在\后加上[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} 即可。

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值