[Tex/LaTex] How to color a cell of a table using multirow and center the cell content

colormultirowtables

I have checked for other similar questions. I followed the suggested solution in the question Color in multirows. After implementing this solution, I still have a problem.

I want to develop the following table:

enter image description here

I want to color specific cells, but I am using multirow, so I used the following code,

\documentclass[journal]{IEEEtran}

\usepackage{tabularx}
\usepackage[dvips]{graphicx}
\usepackage{multirow}
\usepackage[skip=5pt]{caption}   % Unifies the distance between caption and float
\usepackage{array}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor

\begin{document}

\begin{table*}[ht]
\center{\caption{\label{tab:grid} The Grid.}
\scalebox{1}
{{
\begin{tabular}{m{2.25cm} m{3cm} m{3cm} m{3cm} m{3cm}}
\cline{2-5}
\multicolumn{1}{c|}{} & \multicolumn{1}{c|}{\multirow{2}{*}{\textbf{A}}}&\multicolumn{1}{|c|}{\multirow{2}{*}{\cellcolor{blue!25}}} & \multicolumn{1}{|c|}{\multirow{2}{*}{\cellcolor{blue!25} \textbf{C}}}& \multicolumn{1}{|c|}{\multirow{2}{*}{\textbf{D}}} \tabularnewline
\multicolumn{1}{c|}{} & \multicolumn{1}{c|}{}&\multicolumn{1}{c|}{\cellcolor{blue!25} \textbf{B}} & \multicolumn{1}{c|}{\cellcolor{blue!25}}&\multicolumn{1}{c|}{} \tabularnewline
\cline{1-5}
\multicolumn{1}{|c|}{\multirow{2}{*}{\textbf{D}} }& \multicolumn{1}{|c|}{\multirow{4}{*}{A/CD}} & \multicolumn{1}{|c|}{\multirow{4}{*}{B/CD}} &  \multicolumn{2}{|c|}{\multirow{4}{*}{CD}}   \tabularnewline
\multicolumn{1}{|c|}{}&\multicolumn{1}{|c|}{}&\multicolumn{1}{|c|}{}& \multicolumn{2}{|c|}{}   \tabularnewline
\cline{1-1}
\multicolumn{1}{|c|}{\multirow{2}{*}{\textbf{C}}} &\multicolumn{1}{|c|}{} &\multicolumn{1}{|c|}{}& \multicolumn{2}{|c|}{}  \tabularnewline
\multicolumn{1}{|c|}{}&\multicolumn{1}{|c|}{}&\multicolumn{1}{|c|}{}& \multicolumn{2}{|c|}{}   \tabularnewline
\cline{1-5}
\end{tabular}
}}}
\end{table*}

\end{document} 

and here is what I got:

enter image description here

You can notice that I used two different methods for C and B. My questions are:

1- In case of C, I colored the first row of the two-row cell, inserted C, then colored the second half, so the color in the second half overlapped the C. To solve this, I colored the upper row, then the lower row, and then inserted B, which gives a better result, but now B is not centered in the cell, how can I do that?

2- In both cases, the color overlaps the cell boarders. I could overcome the problem in the vertical lines around C and B by adding "|" in C and D to thicken the line, however, the horizontal lines above and below B and C disappeared.

Could colortbl provide a better solution?

Best Answer

You have to colour the cell in the first row, then put the content in the second row using

\multicolumn{1}{|c}{\multirow{-2}{*}{\cellcolor{blue!25} \textbf{C}}}  %% note -2

as in

\multicolumn{1}{c}{} & \multicolumn{1}{|c}{\multirow{2}{*}{\textbf{A}}}&\multicolumn{1}{|c}{\cellcolor{blue!25}} & \multicolumn{1}{|c}{\cellcolor{blue!25}}&  \multicolumn{1}{|c|}{}\\
\multicolumn{1}{c}{} & \multicolumn{1}{|c}{}& \multicolumn{1}{|c}{\multirow{-2}{*}{\cellcolor{blue!25} \textbf{B}}} & \multicolumn{1}{|c}{\multirow{-2}{*}{\cellcolor{blue!25} \textbf{C}}}& \multicolumn{1}{|c|}{\multirow{-2}{*}{\textbf{D}}} \\
\cline{1-5}

Full code:

\documentclass[journal]{IEEEtran}

\usepackage{tabularx}
\usepackage[dvips]{graphicx}
\usepackage{multirow}
\usepackage[skip=5pt]{caption}   % Unifies the distance between caption and float
\usepackage{array}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
%\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}   %% may be better to learn doing this
\begin{document}

\begin{table*}[ht]
%\arrayrulewidth=1% %% this is how you increase the thickness of rules
\centering\caption{\label{tab:grid} The Grid.}    %% don't use \center{...}
\scalebox{1}
{{
\begin{tabular}{|m{2.25cm} |m{3cm} |m{3cm} |m{3cm} |m{3cm}|}
\cline{2-5}\noalign{\vspace{0.4pt}}     %% add this space and adjust suitably
\multicolumn{1}{c}{} & \multicolumn{1}{|c}{\multirow{2}{*}{\textbf{A}}}&\multicolumn{1}{|c}{\cellcolor{blue!25}} & \multicolumn{1}{|c}{\cellcolor{blue!25}}&  \multicolumn{1}{|c|}{}\\
\multicolumn{1}{c}{} & \multicolumn{1}{|c}{}& \multicolumn{1}{|c}{\multirow{-2}{*}{\cellcolor{blue!25} \textbf{B}}} & \multicolumn{1}{|c}{\multirow{-2}{*}{\cellcolor{blue!25} \textbf{C}}}& \multicolumn{1}{|c|}{\multirow{-2}{*}{\textbf{D}}} \\
\cline{1-5}
\multicolumn{1}{|c}{\multirow{2}{*}{\textbf{D}} }& \multicolumn{1}{|c}{\multirow{4}{*}{A/CD}} & \multicolumn{1}{|c}{\multirow{4}{*}{B/CD}} &  \multicolumn{2}{|c|}{\multirow{4}{*}{CD}}   \\
\multicolumn{1}{|c}{}&\multicolumn{1}{|c}{}&\multicolumn{1}{|c}{}& \multicolumn{2}{|c|}{}   \\
\cline{1-1}
\multicolumn{1}{|c}{\multirow{2}{*}{\textbf{C}}} &\multicolumn{1}{|c}{} &\multicolumn{1}{|c}{}& \multicolumn{2}{|c|}{}  \\
\multicolumn{1}{|c}{}&\multicolumn{1}{|c}{}&\multicolumn{1}{|c}{}& \multicolumn{2}{|c|}{}   \\
\cline{1-5}
\end{tabular}
}}
\end{table*}

\end{document}

enter image description here

Related Question