Latex 表格技巧 - 合并单元格

    xiaoxiao2021-03-25  169

    简介

    Latex的表格功能非常强大,但是在初学的过程中可能遇到很多棘手的问题,这里给出了如何合并单元格的几种做法,重点是合并多行多列的做法


    合并一行多列单元格

    合并1行多列可以使用\multicolumn{cols}{pos}{text}来实现 \documentclass[a4paper,12pt]{report} \usepackage[UTF8,nopunct]{ctex} \begin{document} \begin{table} \centering \begin{tabular}{|c|c|c|c|} \hline \multicolumn{2}{|c|}{合并一行两列} &&\\ \hline 1 & 2 & 3 & 4 \\ \hline \end{tabular} \end{table} \end{document}

    合并多行一列单元格

    合并多行1列单元格可以用multirow包中的\multirow{rows}{width}{text}来实现注意这里的第2个参数是{width},与\multicolumn第2个参数不同。如果不确定{width}需要填什么,就将其替换为*,如代码中所示

    注意:下述代码中第2行表格第1列填入了~,这个符号放在这里表示这个单元格什么都不填,但是一定要保留这个空位,不然会产生文字叠加与表格不对齐,各位可以自行尝试,暂时不在这里演示效果,以免混淆。

    \documentclass[a4paper,12pt]{report} \usepackage[UTF8,nopunct]{ctex} \usepackage{multirow} \begin{document} \begin{table} \centering \begin{tabular}{|c|c|c|c|} \hline \multirow{2}*{合并两行一列} &&&\\ ~ & 2 & 3 & 4 \\ \hline \end{tabular} \end{table} \end{document}

    注意到这里并没有进行划线,如果直接在第1行和第2行之间插入一个\hline,这条划线会穿过第1个单元格 \begin{table} \centering \begin{tabular}{|c|c|c|c|} \hline \multirow{2}*{合并两行一列} &&&\\ ~ & 2 & 3 & 4 \\ \hline \end{tabular} \end{table}

    解决方法是划一条从第2列开始到末尾的横线,使用命令\cline{start-end} \begin{table} \centering \begin{tabular}{|c|c|c|c|} \hline \multirow{2}*{合并两行一列} &&&\\ \cline{2-4} ~ & 2 & 3 & 4 \\ \hline \end{tabular} \end{table}

    合并多行多列单元格

    合并多行多列有多种实现方式,这里仅提供一种个人使用感觉比较方便的方法,即组合\multicomumn和\multirow来实现例如我们要插入一个合并2行2列的单元格 \documentclass[a4paper,12pt]{report} \usepackage[UTF8,nopunct]{ctex} \usepackage{multirow} \begin{document} \begin{table} \centering \begin{tabular}{|c|c|c|c|} \hline \multicolumn{2}{|c|}{\multirow{2}*{合并两行两列}} & 三 & 四 \\ \cline{3-4} \multicolumn{2}{|c|}{~} & 3 & 4 \\ \hline \end{tabular} \end{table} \end{document}

    注意:这里在第二行采用\multicolumn来进行空白占位,这样可以避免一些奇怪的划线行为,如果直接采用~ & ~ & ...的方式来占位,会受到表格划线方式{|c|c|c|c|}的影响而多划一条竖线,如下

    \begin{table} \centering \begin{tabular}{|c|c|c|c|} \hline \multicolumn{2}{|c|}{\multirow{2}*{合并两行两列}} &&\\ \cline{3-4} ~ & ~ & 3 & 4 \\ \hline \end{tabular} \end{table}

    转载请注明原文地址: https://ju.6miu.com/read-3647.html

    最新回复(0)