我相信许多人在使用Latex制作表格的时候,都遇到过表格中的文字不能垂直居中的问题。
本例子是在表格中插入图片作为例子的:
\begin{figure}
\centering
\begin{tabular}{cccc} % 表格中的PXX代表图片
\hline
panorama & \includegraphics{p11} & \includegraphics{p12} & \includegraphics{p13} \\
picture vision & \includegraphics{p21} & \includegraphics{p22} & \includegraphics{p23} \\
base & \includegraphics{p31} & \includegraphics{p32} & \includegraphics{p33} \\
self-attention & \includegraphics{p41} & \includegraphics{p42} & \includegraphics{p43} \\
SE & \includegraphics{p51} & \includegraphics{p52} & \includegraphics{p53} \\
CBAM & \includegraphics{p61} & \includegraphics{p62} & \includegraphics{p63} \\
Real Value & \includegraphics{p71} & \includegraphics{p72} & \includegraphics{p73} \\
\hline
\end{tabular}
\end{figure}
代码效果如下图:
如图,左侧一列,字体基本靠到最底端,非常影响表格的美观。
在网上查到设置m{}参数,可以使表格垂直居中,代码需要在导言区引入\usepackage{array}, 代码如下:
\begin{figure}
\centering
\begin{tabular}{m{3cm}|m{4cm} m{4cm} m{4cm}} %这样做可以垂直居中,但是第二行图片又靠左了,还是不美观。
\hline
panorama & \includegraphics{p11} & \includegraphics{p12} & \includegraphics{p13} \\
picture vision & \includegraphics{p21} & \includegraphics{p22} & \includegraphics{p23} \\
base & \includegraphics{p31} & \includegraphics{p32} & \includegraphics{p33} \\
self-attention & \includegraphics{p41} & \includegraphics{p42} & \includegraphics{p43} \\
SE & \includegraphics{p51} & \includegraphics{p52} & \includegraphics{p53} \\
CBAM & \includegraphics{p61} & \includegraphics{p62} & \includegraphics{p63} \\
Real Value & \includegraphics{p71} & \includegraphics{p72} & \includegraphics{p73} \\
\hline
\end{tabular}
\end{figure}
代码效果如下,第一列表格中汉字虽垂直居中,但是字体和图片明显靠左,尤其是第二行,还是不美观。
我尝试了好多方法,要么方法太难,要么不能做到垂直居中。在网上查阅了许多资料最终找到了一种垂直居中的简单方法
只有同时设置表格的垂直居中和水平居中才可以使得达到完美的效果。
代码需要在导言区引入\usepackage{array}
将\begin{tabular}{m{3cm}|m{4cm} m{4cm} m{4cm}}换为
\begin{tabular}{m{2.5cm}<{\centering}|m{4cm}<{\centering} m{4cm}<{\centering} m{4cm}<{\centering}} 即可完美解决。
%导言区 \usepackage{array} 一定要设置,不然会报错
\begin{figure}
\centering
%最重要的也就是调整下边这一行,m{arg} m中的arg参数带表设置表格这一列的宽度
\begin{tabular}{m{2.5cm}<{\centering}|m{4cm}<{\centering} m{4cm}<{\centering} m{4cm}<{\centering}} \hline
panorama & \includegraphics{p11} & \includegraphics{p12} & \includegraphics{p13} \\
picture vision & \includegraphics{p21} & \includegraphics{p22} & \includegraphics{p23} \\
base & \includegraphics{p31} & \includegraphics{p32} & \includegraphics{p33} \\
self-attention & \includegraphics{p41} & \includegraphics{p42} & \includegraphics{p43} \\
SE & \includegraphics{p51} & \includegraphics{p52} & \includegraphics{p53} \\
CBAM & \includegraphics{p61} & \includegraphics{p62} & \includegraphics{p63} \\
Real Value & \includegraphics{p71} & \includegraphics{p72} & \includegraphics{p73} \\ \hline
\end{tabular}
\end{figure}
代码效果如下,我们看到问题已经完美解决,无论是字体还是图片都已经处在表格表格中做到垂直居中,水平居中。
如果表格中放置的是打断的文字,也是可以通过这种方式来解决。
这边我使用网上能查到最多的一个表格居中例子为例说明:网上可以搜到表格居中最多的例子人家使用\multicolumn实现,我觉得那种方式不是很好理解,而且我使用到我前边查图片的例子上根本就不奏效。
例子:
\begin{center}
\begin{tabular}{ | l | l | l | p{5cm} |}
\hline
Day & Min Temp & Max Temp & Summary \\ \hline
Monday & 11C & 22C & A clear day with lots of sunshine.
However, the strong breeze will bring down the temperatures. \\ \hline
Tuesday & 9C & 19C & Cloudy with rain, across many northern regions. Clear spells
across most of Scotland and Northern Ireland,
but rain reaching the far northwest. \\ \hline
Wednesday & 10C & 21C & Rain will still linger for the morning.
Conditions will improve by early afternoon and continue
throughout the evening. \\
\hline
\end{tabular}
\end{center}
代码效果如下:
我们看到前三列的文字顶格,非常不美观:
调整后的代码如下:
\begin{center}
\begin{tabular}{ | m{2cm}<{\centering}|m{2cm}<{\centering} | m{2cm}<{\centering} | m{5cm} |}
\hline
Day & Min Temp & Max Temp & Summary \\ \hline
Monday & 11C & 22C & A clear day with lots of sunshine.
However, the strong breeze will bring down the temperatures. \\ \hline
Tuesday & 9C & 19C & Cloudy with rain, across many northern regions. Clear spells
across most of Scotland and Northern Ireland,
but rain reaching the far northwest. \\ \hline
Wednesday & 10C & 21C & Rain will still linger for the morning.
Conditions will improve by early afternoon and continue
throughout the evening. \\
\hline
\end{tabular}
\end{center}
代码效果如下:
我们看到,同样达到了想要的效果。
如果有更好,更简单的方式实现,欢迎留言交流。