Latex table tips - merging cells

brief introduction

The table function of Latex is very powerful, but it may encounter many thorny problems in the process of learning. Here are several methods of how to merge cells, focusing on the method of merging multiple rows and columns

Merge cells in one row and multiple columns

  • Merging one row and multiple columns can be realized by using \ multicolumn {columns} {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|}{Merge one row and two columns} & three & four \\
		\hline
		1 & 2 & 3 & 4 \\
		\hline
	\end{tabular}
\end{table}

\end{document}

 

 

Merge multiple rows and one column of cells

  • Merging multiple rows and 1 column cells can be realized by \ multirow{rows}{width}{text} in the multirow package
  • Note that the second parameter here is {width}, which is different from the second parameter of \ multicolumn. If you are not sure what {width} needs to be filled in, replace it with *, as shown in the code

Note: in the following code, the first column of the table in line 2 is filled with ~. This symbol here means that nothing is filled in this cell, but this space must be reserved, otherwise the text overlay will be out of alignment with the table. You can try it by yourself and don't demonstrate the effect here for the time being to avoid confusion.  

 

\documentclass[a4paper,12pt]{report}
\usepackage[UTF8,nopunct]{ctex}
\usepackage{multirow}

\begin{document}

\begin{table}
	\centering
	\begin{tabular}{|c|c|c|c|}
		\hline
		\multirow{2}*{Merge two rows and one column} & two & three & four \\
		~ & 2 & 3 & 4 \\
		\hline
	\end{tabular}
\end{table}

\end{document}

 

  • Note that there is no underline here. If you insert a \ hline directly between line 1 and line 2, the underline will pass through the first cell
\begin{table}
	\centering
	\begin{tabular}{|c|c|c|c|}
		\hline
		\multirow{2}*{Merge two rows and one column} & two & three & four \\
		~ & 2 & 3 & 4 \\
		\hline
	\end{tabular}
\end{table}

 

  • The solution is to draw a horizontal line from the beginning to the end of column 2 and use the command \ cline {start end}
\begin{table}
	\centering
	\begin{tabular}{|c|c|c|c|}
		\hline
		\multirow{2}*{Merge two rows and one column} & two & three & four \\
		\cline{2-4}
		~ & 2 & 3 & 4 \\
		\hline
	\end{tabular}
\end{table}

 

Merge multi row and multi column cells

  • There are many ways to merge multiple rows and columns. Here is only one convenient method for personal use, that is, combining \ multicomumn and \ multirow
  • For example, we want to insert a cell that combines 2 rows and 2 columns
\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}*{Merge two rows and two columns}}  & three & four \\
		\cline{3-4}
		\multicolumn{2}{|c|}{~} & 3 & 4 \\
		\hline
	\end{tabular}
\end{table}

\end{document}

  Note: in the second row, use \ multicolumn to occupy the blank space, which can avoid some strange scribing behavior. If you directly use ~ & ~ &... To occupy the space, you will draw an additional vertical line under the influence of the table scribing method {| C | C | C}, as shown below

\begin{table}
	\centering
	\begin{tabular}{|c|c|c|c|}
		\hline
		\multicolumn{2}{|c|}{\multirow{2}*{Merge two rows and two columns}}  & three & four \\
		\cline{3-4}
		~ & ~ & 3 & 4 \\
		\hline
	\end{tabular}
\end{table}

  Original text: latex table tips - merging cells_ Netadmin CSDN blog_ latex table merge cells

Keywords: html css Latex

Added by batman on Sun, 19 Sep 2021 11:43:06 +0300