1

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.

F. Pantigny
  • 40,250
Hamzah
  • 301

2 Answers2

2

The environment {NiceTabular} of nicematrix has a command \rowcolors able to color rows with alternance while respecting the blocks (with the key respect-blocks).

\documentclass{article}
\usepackage{graphicx,color}
\usepackage{nicematrix}
\usepackage{caption}
\begin{document}

\begin{table} \centering \arrayrulecolor{gray} \caption{Statistics} \begin{NiceTabular}{|cccc|} \CodeBefore \rowcolors{1}{gray!50}{white!10}[respect-blocks] \Body \Hline \RowStyle[bold,nb-rows=2]{} \Block{2-1}{country} & \Block{2-1}{GDP} & \Block{1-2}{Sex} \ & & male & female \ A&B&C&D\ A&B&C&D\ A&B&C&D\ \Hline \end{NiceTabular} \end{table} \end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

F. Pantigny
  • 40,250
1

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