Most of the places, examples for merging of cells in tables are given. But, I could not find a merging of cells in complicated fashion as shown below. This question may summarize all the possible methods of merging and may serve as the reference for all future questions on merging of cells. Experts can add much more complications if required.
2 Answers
Here is an extended duplication of your table, with the addition of using some \multirow
s (from the multirow
package) just because you asked about some "more elaborate" cell constructs:
\documentclass{article}
\usepackage{multirow}% http://ctan.org/pkg/multirow
\begin{document}
\sffamily
\begin{tabular}{|*{6}{p{2cm}|}}
\hline
1a & 2a & 3a & \multicolumn{2}{l|}{4a, 5a} & 6a \\ \hline
1b & \multicolumn{2}{l|}{2b, 3b} & 4b & 5b & 6b \\ \hline
1c & 2c & 3c & 4c, 4d & 5c & 6c \\ \cline{1-3}\cline{5-6}
1d, 1e & 2d & 3d & & 5d & 6d \\ \cline{2-6}
& 2e & 3e & 4e & 5e & \\ \cline{1-5}
1f & \multicolumn{2}{l|}{\multirow{2}*{2f, 3f, 2g, 3g}} & 4f & 5f & \multirow{-2}*{6e, 6f}\\ \cline{1-1}\cline{4-6}
1g & \multicolumn{2}{l|}{} & 4g & 5g & 6g \\ \hline
\multicolumn{2}{|l|}{1h, 2h} & 3h & 4h & 5h & 6h \\ \hline
\end{tabular}
\end{document}
Some things to note in such tabular
constructs:
- If your column types are very similar, you can use the
*{<num>}{<col spec>}
format to duplicate<col spec>
a total of<num>
times; - When using
\multicolumn{<num>}{<col spec>}{<stuff>}
, you need to include the "end" vertical rule in<col spec>
, since the "begin" vertical rule in a single-ruletabular
belongs to the previous cell (except if you're spanning from the first column); \multirow{<num>}
allows for a negative number<num>
, which will raise the entries vertically to the middle of<num>
rows. If<num>
is positive, the entries are dropped. Raising is useful when you have coloured rows, since the colour is set on a row-by-row basis. So, you may want to finish setting the colour of some upper row, and the\multirow
an entry "into" it. For this you would need a negative<num>
.
With {NiceTabular}
of nicematrix
.
\documentclass{article}
\usepackage{geometry}
\usepackage{nicematrix}
\begin{document}
\sffamily
\parindent0pt
\begin{NiceTabular}{*{6}{w{l}{2cm}}}[hvlines]
1a & 2a & 3a & \Block{1-2}{} 4a, 5a & & 6a \\
1b & \Block{1-2}{} 2b, 3b & & 4b & 5b & 6b \\
1c & 2c & 3c & \Block{2-1}{} 4c, 4d & 5c & 6c \\
\Block{2-1}{} 1d, 1e & 2d & 3d & & 5d & 6d \\
& 2e & 3e & 4e & 5e & \Block{2-1}{6e, 6f} \\
1f & \Block[l]{2-2}{2f, 3f, 2g, 3g} && 4f & 5f & \\
1g & & & 4g & 5g & 6g \\
\Block{1-2}{} 1h, 2h & & 3h & 4h & 5h & 6h \\
\end{NiceTabular}
\end{document}