in Latex, Learning

Multi-row and Multi-column table with background color in Latex

Recently, I wanted to create a table like this in \textrm{\LaTeX} where the header has partially two rows and two columns are grouped to form a larger column with a background colour:

 \centering \begin{tabular}{ c  c  c  c  c}    \toprule   \rowcolor{gray!20}& \multicolumn{2}{c}{Lower Limit} & \multicolumn{2}{c}{Upper Limit} \\ \cmidrule{2-5}   \rowcolor{gray!20}\multirow{-2}{*}{Season} & Avg. & Max. & Avg. & Max. \\    \midrule      Spring & 0.2 & 1.2 & 0.31 & 0.89 \\  \bottomrule \end{tabular}

Well, I thought it should be fairly easy but I was wrong especially when you have multi-row and multi-column with a specific background colour. It took me an hour to solve it. So, I thought I would create this post to tell what I have done. First, you should include the following packages in your permeable:

\usepackage{multirow, booktabs}
\usepackage[table]{xcolor}
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}

The first line loads up multirow and booktabs packages which are essential for having multiple rows and special formatting of upper, middle, and lower horizontal line. The second provides predefined colours to paint the rows. The last two lines reset the white space above and below the horizontal lines drawn by booktabs package. Let’s start building step-by-step. First, we create a single row with multi-column features as follows:

\centering
\begin{tabular}{ c c c c c} 
  \toprule
  Season & \multicolumn{2}{c}{Lower Limit} & \multicolumn{2}{c}{Upper Limit} \\ 
   & Avg. & Max. & Avg. & Max. \\
   \midrule  
   Spring & 0.2 & 1.2 & 0.31 & 0.89 \\
 \bottomrule
\end{tabular}
 \centering \begin{tabular}{c c c c c}    \toprule   Season & \multicolumn{2}{c}{Lower Limit} & \multicolumn{2}{c}{Upper Limit} \\     & Avg. & Max. & Avg. & Max. \\    \midrule      Spring & 0.2 & 1.2 & 0.31 & 0.89 \\  \bottomrule \end{tabular}

Line 4 is the important line here. to merge two columns together for “Lower Limit” and “Upper Limit” tags, we use \multicolumn{2}{c}{TAG} where the first parameter takes the number of merged columns, the second parameter handle justification which in this case is “center” where you can specify vertical line for the cell as well, and the tag which appears inside of the merged cell. Now, we add the line beneath the two merged column at the end of line 4 using \cmidrule from booktabs package:

  Season & \multicolumn{2}{c}{Lower Limit} & \multicolumn{2}{c}{Upper Limit} \\ \cmidrule{2-5}
 \centering \begin{tabular}{c c c c c}    \toprule   Season & \multicolumn{2}{c}{Lower Limit} & \multicolumn{2}{c}{Upper Limit} \\ \cmidrule{2-5}    & Avg. & Max. & Avg. & Max. \\    \midrule      Spring & 0.2 & 1.2 & 0.31 & 0.89 \\  \bottomrule \end{tabular}

\cmidrule{2-5} draws a line from column 2 to 5 and it comes after the end of the first row, i.e., after \\. Now, we add multiple rows to the code:

  \multirow{2}{*}{Season} & \multicolumn{2}{c}{Lower Limit} & \multicolumn{2}{c}{Upper Limit} \\ \cmidrule{2-5}
 \centering \begin{tabular}{c c c c c}    \toprule   \multirow{2}{*}{Season} & \multicolumn{2}{c}{Lower Limit} & \multicolumn{2}{c}{Upper Limit} \\ \cmidrule{2-5}    & Avg. & Max. & Avg. & Max. \\    \midrule      Spring & 0.2 & 1.2 & 0.31 & 0.89 \\  \bottomrule \end{tabular}

where \multirow{2}{*}{Season} is added to the beginning of line 4. The first argument specifies the number of rows which has to be merged, the second is for text justification (here in the middle of the cell), and the last one is the tag appears in the cell. Now, if I want to add a background color to both merged rows using \rowcolor{gray!20} from \usepackage[table]{xcolor} package:

  \rowcolor{gray!20} \multirow{2}{*}{Season} & \multicolumn{2}{c}{Lower Limit} & \multicolumn{2}{c}{Upper Limit} \\ \cmidrule{2-5}
  \rowcolor{gray!20} & Avg. & Max. & Avg. & Max. \\
 \centering \begin{tabular}{c c c c c}    \toprule   \rowcolor{gray!20} \multirow{2}{*}{Season} & \multicolumn{2}{c}{Lower Limit} & \multicolumn{2}{c}{Upper Limit} \\ \cmidrule{2-5}    \rowcolor{gray!20} & Avg. & Max. & Avg. & Max. \\    \midrule      Spring & 0.2 & 1.2 & 0.31 & 0.89 \\  \bottomrule \end{tabular}

As you can see, the colour of the second row is overcasting the tag inside of the merged cell. To fix this, and this is where the headache was, you have to modify lines 4 and 5 like this:

  \rowcolor{gray!20} \multirow{2}{*}{Season} & \multicolumn{2}{c}{Lower Limit} & \multicolumn{2}{c}{Upper Limit} \\ \cmidrule{2-5}
  \rowcolor{gray!20} & Avg. & Max. & Avg. & Max. \\
 \centering \begin{tabular}{c c c c c}    \toprule   \rowcolor{gray!20}  & \multicolumn{2}{c}{Lower Limit} & \multicolumn{2}{c}{Upper Limit} \\ \cmidrule{2-5}    \rowcolor{gray!20} \multirow{-2}{*}{Season} & Avg. & Max. & Avg. & Max. \\    \midrule      Spring & 0.2 & 1.2 & 0.31 & 0.89 \\  \bottomrule \end{tabular}

So, you should move \multirow{2}{*}{Season} from line 4 to line 5 while changing the number of line merging argument to a negative value to refer back to the previous rows, i.e., \multirow{-2}{*}{Season}.

Share if you liked:

Comments

Comment

  1. Thank you.

    I’m new in this LaTeX world and this is exactly what I was looking.
    Within an hour I was able to make a table with merged rows and merged collums for my project about game theory.