# How to color a cell of a table using multirow and center the cell content?

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:

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:

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?

• never do &\multicolumn{1}{|c|}{} always &\multicolumn{1}{c|}{} the rules are just on the right edge of each cell (except the first column) otherwise you get spurious mis-aligned rule segments on the left which is why your vertical rules have thick and thin segments. if using multirow and color you need to put the text in the last row with a -ve row span so it overlaps the color rather than being covered. As for the last question, You are using colortbl already (xcolor loads that package) – David Carlisle May 18 '14 at 23:22
• @DavidCarlisle but if I use &\multicolumn{1}{c|}{} the color overlaps the vertical lines, that is why I use &\multicolumn{1}{|c|}{} to make it thick. Can you kindly provide a complete answer with the -ve row span example? This will be very helpful. Thank you. – BHamza May 18 '14 at 23:29

## 4 Answers

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}


With hhline, colortbl and makecell it is possible — and the syntax is a bit simpler:

\documentclass{article}

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

\usepackage{hhline}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{\Gape[6pt]}

\begin{document}

\begin{table}[!ht]
\centering
\caption{\label{tab:grid} The Grid.}
\begin{tabular}{|m{.4cm}| *{2}{m{0.9cm}|}*{2}{m{0.45cm}|}}
\hhline{~|-|-|-|-|}
\multicolumn{1}{c|}{} & \thead{A} & \cellcolor{blue!25}\thead{B}&\cellcolor{blue!25} \thead{C} & \thead{D} \\
\hline
\multirowthead{1}[10pt]{D} & \multirowcell{2}{A/CD} & \multirowcell{2}{B/CD} & \multicolumn{2}{c|}{\multirowcell{2}{CD}}\\[-13pt]%
\cline{1-1}
\thead{C} & & &\multicolumn{2}{c|}{} \\
\hline
\end{tabular}
\end{table}

\end{document}


Rather than multirow, here' a stacking alternative.

\documentclass{article}
\usepackage[usestackEOL]{stackengine}
\usepackage{xcolor}
\def\xfrbox(#1,#2)#3{\fboxsep=-.5\fboxrule%
\fbox{\makebox(#1,#2){#3}}\kern-\fboxrule}
\def\cfrbox#1(#2,#3)#4{\fboxsep=-.5\fboxrule%
\fbox{\fboxsep=0pt\colorbox{#1}{\makebox(#2,#3){#4}}}\kern-\fboxrule}
\begin{document}
\begin{table}[ht]
\setlength\unitlength{1mm}%
\setstackgap{S}{-\fboxrule}
\centering
\caption{The Grid.}\medskip
\Shortstack{\xfrbox(6,6){\bfseries D}\\\xfrbox(6,6){\bfseries C}}%
\Shortstack{\xfrbox(12,6){\bfseries A}\\\xfrbox(12,12){A/CD}}%
\Shortstack{\cfrbox{blue!30}(12,6){\bfseries B}\\\xfrbox(12,12){B/CD}}%
\Shortstack{\cfrbox{blue!30}(6,6){\bfseries C}\xfrbox(6,6){\bfseries D}\\\xfrbox(12,12){CD}}%
\end{table}
\end{document}


Frankly said, I do not understand the purpose for use \multicolumn{1}{c}{...} in each cell of your table. With it you just, as I can see, only override the column declarations in table. Similarly is with use of \scalebox{1}{...}. As is not serve to any table feature. If you like to have table width equal column width, it is better to use tabular* or tabularx environment. But maybe I'm wrong.

The same form of table you can achieve (behind the other above suggestions) simple with:

\documentclass[journal]{IEEEtran}
\usepackage[dvips]{graphicx}    % it works also without option
\usepackage[skip=5pt]{caption}  % Unifies the distance between caption and float
\usepackage[table]{xcolor}      % http://ctan.org/pkg/xcolor
\usepackage{multirow}

\begin{document}
\begin{table*}[ht]
\caption{\label{tab:grid} The Grid.}
\centering
\renewcommand{\arraystretch}{1.5}
\arrayrulewidth=0.5pt
\begin{tabular}{|*{5}{c|}}
\cline{2-5}\noalign{\vspace{0.5pt}}
\multicolumn{1}{c|}{}
&   \textbf{A}
&  \cellcolor{blue!25}  \textbf{B}
&   \cellcolor{blue!25} \textbf{C}
&   \textbf{D}                              \tabularnewline
\hline
\textbf{D}
&   \multirow{2}{*}{A/CD}
&   \multirow{2}{*}{B/CD}
&   \multicolumn{2}{c|}{\multirow{2}{*}{CD}}    \tabularnewline
\cline{1-1}
\textbf{C}
&   &   &   \multicolumn{2}{c|}{}                       \tabularnewline
\hline
\end{tabular}
\end{table*}
\end{document}