目标效果
Latex_5">Latex代码
\begin{table}[h]
\centering
\caption{Example Table with Merged Cells}
\begin{tabular}{|c|c|c|c|}
\hline
\textbf{Row 1, Col 1} & \multicolumn{2}{c|}{\textbf{Merged Cells}} & \textbf{Row 1, Col 4} \\
\hline
\textbf{Row 2, Col 1} & \multirow{2}{*}{\textbf{Merged}} & \textbf{Row 2, Col 3} & \textbf{Row 2, Col 4} \\
\cline{1-1} \cline{3-4}
\textbf{Row 3, Col 1} & & \textbf{Row 3, Col 3} & \textbf{Row 3, Col 4} \\
\hline
\end{tabular}
\end{table}
Problem
To merge cells in LaTeX tables and center the content within the merged cells, you can use the \multicolumn
command to merge cells horizontally and \multirow
for vertical merging. In your case, you want to merge the second column of the second row and the third column of the second row.
Here’s an example of how to implement this:
LaTeX Code:
\documentclass{article}
\usepackage{multirow} % To use \multirow for vertical cell merging
\begin{document}
\begin{table}[h]
\centering
\caption{Example Table with Merged Cells}
\begin{tabular}{|c|c|c|c|}
\hline
\textbf{Row 1, Col 1} & \multicolumn{2}{c|}{\textbf{Merged Cells}} & \textbf{Row 1, Col 4} \\
\hline
\textbf{Row 2, Col 1} & \multirow{2}{*}{\textbf{Merged}} & \textbf{Row 2, Col 3} & \textbf{Row 2, Col 4} \\
\cline{1-1} \cline{3-4}
\textbf{Row 3, Col 1} & & \textbf{Row 3, Col 3} & \textbf{Row 3, Col 4} \\
\hline
\end{tabular}
\end{table}
\end{document}
Explanation:
-
Merging Cells Horizontally:
- The
\multicolumn{2}{c|}{\textbf{Merged Cells}}
command merges the second and third columns in the first row, with the content centered (c
) within the merged cells.
- The
-
Merging Cells Vertically:
- The
\multirow{2}{*}{\textbf{Merged}}
command vertically merges the second column in the second and third rows. The*
allows the content to span the necessary number of rows.
- The
-
Centering Content:
- By default, the content of the merged cells is centered as specified by
\multicolumn
and\multirow
. If you need further customization (e.g., left-justification, right-justification), you can adjust the alignment within the respective\multicolumn
or\multirow
.
- By default, the content of the merged cells is centered as specified by
Resulting Output:
- Row 1, Column 2 and Column 3 are merged and contain the text “Merged Cells.”
- Row 2, Column 2 and Row 3, Column 2 are merged vertically, containing the text “Merged.”