在制作LaTex表格的時候,常常需要合並行和列,本文介紹的是使用 multirow 包里面的命令完成合並行列的方法。
需要先導入以下庫:
\usepackage{multirow}
舉例:
\begin{table*} %\usepackage{multirow} \center \begin{tabular}{|c|c|c|c|c|} \hline % horizontal line \multirow{2}{*}{1 \& 2} & \multicolumn{2}{l|}{ 3 \& 4 } & 5 & 6\\ %end line \cline{2-5} %short partial horizontal lines from column 2 to column 5 & 7 & 8 & 9 & 10\\ %first cell is occupied by the multirow \hline \hline $ s_{1} $ & 2 & 3 & 4 & 5\\ $ s_{2} $ & 2 & 3 & 4 & 5\\ \hline \hline $ s_{3} $ & 2 & 3 & 4 & 5\\ $ s_{4} $ & 2 & 3 & 4 & 5\\ \hline \end{tabular} \end{table*}
效果
\begin{table}[h] \caption{Optimization summary.} \label{tab:summary} \begin{tabular}{cccc} \toprule \multirow{2}{*}{Design} & \multicolumn{2}{c}{Problem size} & \multirow{2}{*}{CPU time(s)} \\ \cline{2-3} & Binary variables & Constraints\\ \midrule OM1 & 199 & 13,017 & 000 \\ OM2 & 199 & 1,525 & 000 \\ \bottomrule \end{tabular} \label{tab:my_label} \end{table}
效果(后面有改進版)
舉例:
\begin{table}[h] \caption{Optimization summary.} \centering \vspace{2mm} \begin{tabular}{ccccccccc} \toprule \multirow{2}{*}{Model Name} & \multicolumn{4}{c}{Building Roof} & \multicolumn{4}{c}{Building}\\ \cline{2-9} & Precision & Recall & $F_1$ & IoU & Precision & Recall & $F_1$ & IoU\\ \midrule FCN-8s \cite{long2015fully} & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83\\ U-Net \cite{ronneberger2015u} & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83\\ Res-U-Net \cite{xu2018building} & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83\\ SegNet \cite{badrinarayanan2017segnet} & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83\\ DeepLabV3+ \cite{chen2018encoder} & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83\\ DeConvNet \cite{noh2015learning} & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83\\ Our Model & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83\\ \bottomrule \end{tabular} \label{tab:my_label} \end{table}
效果:
上面表格的問題在於兩行標題欄距離太近,通過下面解決,也可以參考網站 https://www.tablesgenerator.com (Booktabs table style)
修改代碼:
% Please add the following required packages to your document preamble: % \usepackage{booktabs} % \usepackage{multirow} \begin{table}[h] \caption{Optimization summary.} \label{tab:summary} \begin{tabular}{cccc} \toprule \multirow{2}{*}{Design} & \multicolumn{2}{c}{Problem size} & \multirow{2}{*}{CPU time(s)} \\ \cmidrule{2-3} & Binary variables & Constraints\\ \midrule OM1 & 199 & 13,017 & 78.7 \\ OM2 & 199 & 1,525 & 13.8 \\ \bottomrule \end{tabular} \label{tab:my_label} \end{table}
效果:(更協調了一些,目前最優)
- toprule:最上面的線(粗)
- cmidrule:標題中間的線(細)
- midrule:中間的線(中)
- bottomrule:最下面的線(粗)
添加表格注釋,參考:Latex如何在表格下方顯示注腳_小白皮皮-程序員宅基地_latex 表格下方注釋
導入以下庫:
\usepackage{threeparttable}
舉例:
\begin{table}[h] \centering \caption{Quantitative comparison of four conventionally used metrics obtained from the segmentation results by FCN-8s, U-Net, Res-U-Net, SegNet, DeepLabV3+, DeConvNet, and the proposed model, where the values in bold format are the highest numbers for corresponding metrics.} \vspace{2mm} \begin{threeparttable} % 增加部分 \begin{tabular}{ccccccccc} \toprule \multirow{2}{*}{Model Name} & \multicolumn{4}{c}{Building Roof} & \multicolumn{4}{c}{Building}\\ \cline{2-9} & Precision & Recall & $F_1$ & IoU & Precision & Recall & $F_1$ & IoU\\ \midrule FCN-8s \cite{long2015fully} & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83\\ U-Net \cite{ronneberger2015u} & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83\\ Res-U-Net \cite{xu2018building} & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83\\ SegNet \cite{badrinarayanan2017segnet} & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83\\ DeepLabV3+ \cite{chen2018encoder} & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83\\ DeConvNet \cite{noh2015learning} & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83\\ Our Model & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83 & 69.83\\ \bottomrule \end{tabular} \label{tab:my_label} \begin{tablenotes} %增加部分 \item The bolded numbers indicate the largest number in the column, easier to find out which one performs better in this format. \end{tablenotes} %增加部分 \end{threeparttable} %增加部分 \end{table}
效果: