Tables – Troubleshooting Multirow with Color Issues in LaTeX

multirowrowcolortables

First, there is not any error message, but I have this problem in which I want to merge rows horizontally, but I do not know why the text appears like that:

enter image description here

Code:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{graphicx,color}
\usepackage{array}
\usepackage{multirow}
\begin{document}

\begin{table}
\centering
\arrayrulecolor{gray}
\caption{Statistics}\label{tab8}
\rowcolors{2}{gray!50}{white!10}
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|} 
\hline  
 \multirow{2}{*}{\textbf{country}} &
\multirow{2}{*}{\textbf{GDP}} &
\multicolumn{2}{|c|}{\textbf{Sex}} \\
& & \textbf{male} & \textbf{female} \\
 A&B&C&D\\ 
 A&B&C&D\\ 
 A&B&C&D\\ 
 \end{tabular}
 \end{table}
\end{document}

I want the merged cells to be colored with only one color.

Thank you for your help.

Best Answer

By using \multirow{-2}{*}{text} you can properly colorize the background of the two rows.

Related question: \rowcolor for a \multirow

MWE:

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{multirow}

\begin{document}

\begin{table}
\centering
\arrayrulecolor{gray}
\caption{Statistics}\label{tab8}
\rowcolors{3}{white}{gray!20}
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|}

\hline

\rowcolor{gray!35}

& & \multicolumn{2}{c|}{\textbf{Sex}}\\

\rowcolor{gray!35}

\multirow{-2}{*}{\textbf{country}} &
\multirow{-2}{*}{\textbf{GDP}} &
\textbf{male} & \textbf{female}\\

\hline

Row 1 & & &\\
Row 2 & & &\\
Row 3 & & &\\
Row 4 & & &\\

\hline

\end{tabular}
\end{table}

\end{document}

Output: table preview

Related Question