[Tex/LaTex] Background color for rows and columns of a table

tables

I have a problem with changing background of the table cells/rows.

This is the code I use:

\documentclass{article}

\usepackage[skip=0.333\baselineskip]{caption}
\usepackage{booktabs,tabularx,ragged2e}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage[table]{xcolor}

\begin{document}
\renewcommand{\arraystretch}{1.3}
\newcolumntype{Y}{>{\hsize=.7\hsize\RaggedRight\arraybackslash}X}
\newcolumntype{B}{X}

\begin{table}
\caption{Sources and targets}
\begin{tabularx}{\textwidth}{p{1cm}@{}*{4}{Y}@{}}
%% table header
\toprule
\rowcolor{lightgray}\multicolumn{2}{r@{}}{List targets} $\rightarrow$ & \multicolumn{3}{c@{}}{Targets} \\
\cmidrule(l){3-5}
\rowcolor{lightgray}\multicolumn{2}{l@{}}{\cellcolor{lightgray}$\downarrow$ Sources}  & Target 1 & Target 2 & Target 3 \\
\midrule
%% body of table
\multirow{3}{2cm}{\rotatebox{90}{\parbox{\linewidth}{sources}}} &\cellcolor{lightgray}Source 1 & Description 10 
         & Description 11 
         & Description 12 \\
\addlinespace % <-- use whitespace as a simple yet very effective divider
&\cellcolor{lightgray}Source 2 (additional info) 
         & Description 20 
         & Description 21
         & Description 22, \par
           Description 23, \par
           Description 24 \\
\addlinespace % ditto
&\cellcolor{lightgray}Source 3 & Description 30 
         & Description 31
         & Description 32, \par 
           Description 33, \par 
           Description 34 \\    
\bottomrule
\end{tabularx}
\end{table}


\end{document}

And the result is this:
enter image description here

Can anyone help me to make it look better? Without this white space between cells, and also this vertical column (sources) should also have the same gray background.

Best Answer

Adaptations:

  • remove addlinespace, toprule, midrule and bottomrule to remove the whitespace (and use hline instead)
  • corrected heading: ...{List targets $\rightarrow$}
  • Select a background color for the entire column using \newcolumntype{a}{>{\columncolor{lightgray}}Y} (from How do I color table columns?)
  • For the first column with the multirow you have to write \multirow{-3}{...}{...} in the last line of the 3 lines, else the background gray will be over the text (see texblog: Coloring multi-row tables in LaTeX)

Code:

\documentclass{article}

\usepackage[skip=0.333\baselineskip]{caption}
\usepackage{booktabs,tabularx,ragged2e}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage[table]{xcolor}

\begin{document}
\renewcommand{\arraystretch}{1.3}
\newcolumntype{Y}{>{\hsize=.7\hsize\RaggedRight\arraybackslash}X}
%\newcolumntype{B}{X}
\newcolumntype{a}{>{\columncolor{lightgray}}Y}
\newcolumntype{C}[1]{>{\columncolor{lightgray}}p{#1}} % zentriert mit

\begin{table}
    \caption{Sources and targets}
    \begin{tabularx}{\textwidth}{C{5mm}@{}a*{3}{Y}@{}}
        %% table header
        \hline
        \rowcolor{lightgray}\multicolumn{2}{r@{}}{List targets $\rightarrow$} & \multicolumn{3}{c@{}}{Targets} \\
        \rowcolor{lightgray}\multicolumn{2}{l@{}}{$\downarrow$ Sources}  & Target 1 & Target 2 & Target 3 \\ \hline
        %% body of table
        & Source 1 & Description 10 
                 & Description 11 
                 & Description 12 \\
        &Source 2 (additional info) 
                 & Description 20 
                 & Description 21
                 & Description 22, \par
                   Description 23, \par
                   Description 24 \\
        \multirow{-3}{12mm}{\rotatebox{90}{\parbox{\linewidth}{sources}}} &Source 3 & Description 30 
                 & Description 31
                 & Description 32, \par 
                   Description 33, \par 
                   Description 34 \\ \hline
    \end{tabularx}
\end{table}

\end{document}

Result:

enter image description here

Related Question