16

I am trying to color a multirow in my table, but without success until now:

\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{colortbl}
\usepackage{multicol}
\begin{document}
\begin{table}
\begin{tabular}{|l|c|c|c|c|}
\hline
\multicolumn{5}{|c|}{Type} \\
\hline
  & A  &    B & C & D \\
    \hline
    X & 53,2\%  &   51,6\%  &  \multirow{2}{*}{\cellcolor[gray]{.9}49\%} & \multirow{2}{*}{\cellcolor[gray]{0.9}49\% }\\
    Y  & 53,8\%  &  52,2\%  &  & \\
\hline
\end{tabular}
\end{table}
\end{document}

enter image description here

What should I do, so that the two last multirow-cells get completely grey-colored?

Olivier
  • 208
DatamineR
  • 711

2 Answers2

28
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{colortbl}
\usepackage{multicol}
\begin{document}
\begin{table}
\begin{tabular}{|l|c|c|c|c|}
\hline
\multicolumn{5}{|c|}{Type} \\
\hline
  & A  &    B & C & D \\
    \hline
    X & 53,2\%  &   51,6\%  & \cellcolor[gray]{0.9}  & \cellcolor[gray]{0.9}\\
    Y  & 53,8\%  &  52,2\%  & \multirow{-2}{*}{\cellcolor[gray]{.9}49\%} & \multirow{-2}{*}{\cellcolor[gray]{0.9}49\% }\\
\hline
\end{tabular}
\end{table}
\end{document}

enter image description here

First color the upper cells by issuing \cellcolor[gray]{0.9} and then use \multirow n the second row. Note -2 in \multirow{-2}{*}{...}. This will make multi row to grow upwards.

3

You do that easily with {NiceTabular} of nicematrix.

\documentclass{scrartcl}
\usepackage{nicematrix}
\begin{document}
\begin{table}
\begin{NiceTabular}{lcccc}[hvlines]
\CodeBefore
  \rectanglecolor[gray]{0.9}{3-4}{4-5}
\Body
  \Block{1-5}{Type} \\
  & A  &    B & C & D \\
  X & 53,2\%  &   51,6\%  & \Block{2-1}{49\%}  & \Block{2-1}{49\%} \\
  Y  & 53,8\%  &  52,2\%  & \\
\end{NiceTabular}
\end{table}
\end{document}
  • You need two compilations.

  • You won't have artefacts in some PDF viewers as you have sometimes with PDF created by the tools of colortbl.

Output of the above code

It's also possible to use the key fill of the command \Block.

\documentclass{scrartcl}
\usepackage{nicematrix}
\begin{document}
\begin{table}
\begin{NiceTabular}{lcccc}[hvlines,color-inside]
  \Block{1-5}{Type} \\
  & A  &    B & C & D \\
  X & 53,2\%  &   51,6\%  & \Block[fill=[gray]{0.9}]{2-1}{49\%}  & \Block[fill=[gray]{0.9}]{2-1}{49\%} \\
  Y  & 53,8\%  &  52,2\%  & \\
\end{NiceTabular}
\end{table}
\end{document}

The output is the same.

F. Pantigny
  • 40,250