Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
2  
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 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 at 23:29

4 Answers 4

up vote 7 down vote accepted

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

share|improve this answer
    
Thank you so much @HarishKumar. One last question is, how can I make table lines bold and clear because for the solution you provided, the lines above the colored cells are covered. –  BHamza May 19 at 0:18
    
Perfect! I appreciate it. Now, it looks neat! :) –  BHamza May 19 at 0:26
    
@BHamza Glad it helped :) –  Harish Kumar May 19 at 0:26

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}

enter image description here

share|improve this answer

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} 

enter image description here

share|improve this answer

Frankly said, I do not understand the purpose for use \multicoulmn{1}{c}{} in each cell of your table. With it you just, as I can see, you only override the column declarations in table. 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}
\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
\scalebox{1}
{{    \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} 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.