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.

2
  • You should provide a complete MWE (minimal working example) with the \document{classe}, \usepackage{...}, etc. Mar 9, 2022 at 10:08
  • I already added all packages
    – Hamzah
    Mar 9, 2022 at 10:11

2 Answers 2

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

2
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .