7

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.

dexteritas
  • 9,161
marko3d
  • 351
  • 1
  • 2
  • 8

3 Answers3

8

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

dexteritas
  • 9,161
  • thank you very much, thats almost what I need... is is possible also to add the gray background to the left column "sources" ? – marko3d Aug 14 '17 at 11:14
  • Now also the left column "sources" is gray (see last comment under adaptations). – dexteritas Aug 14 '17 at 11:27
3

i rather would consider how to design nice table and would redesign your table as follows:

enter image description here

mwe:

\documentclass{article}
\usepackage[skip=0.33\baselineskip]{caption}
\usepackage{ragged2e}
\usepackage{booktabs, multirow, tabularx}
\newcolumntype{L}{>{\RaggedRight}X}
\usepackage{graphicx}

\begin{document}

\begin{table}
    \caption{Sources and targets}
    \begin{tabularx}{\textwidth}{*{4}{L} }
        \toprule
    \multicolumn{1}{c}{} & \multicolumn{3}{c}{Targets} \\
    \cmidrule{2-4}
    sources     & Target 1
                & Target 2
                & Target 3          \\
        \midrule
    %      
    Source 1
                & Description 10
                & Description 11
                & Description 12    \\
        \addlinespace
    Source 2\par  (additional info)
                & Description 20
                & Description 21
                & Description 22,  \par
                  Description 23,  \par
                  Description 24   \\
        \addlinespace
     Source 3   & Description 30
                & Description 31
                & Description 32,   \par
                  Description 33,   \par
                  Description 34    \\
        \bottomrule
    \end{tabularx}
\end{table}
\end{document}
Zarko
  • 296,517
3

This is due to the vertical padding of booktabs rules. A solution consists in neutralising this padding and compensate it with an increase of \arraystretch. For the specific two \addlinespaces, I replace them with \\[\adddefaultspace]. Finally I replaced the \cmidrule with an \hhline, at the cost of a supplementary (empty) column.

Another point is you misundertood the aim and the syntax of >{\hsize=coeff\hsize} for X columns: the sum of the coefficients must be equal to the total number of columns. If you want to reduce the size of equal X columns, you simply reduce the total size of the table.

    \documentclass{article}

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

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

    \begin{table}[!htb]
    \setlength{\aboverulesep}{0pt}
    \setlength{\belowrulesep}{0pt}
    \caption{Sources and targets}
    \begin{tabularx}{\textwidth}{p{1cm}@{}*{4}{Y}@{} >{}c}
    %% table header
    \cmidrule[\heavyrulewidth](r{-\tabcolsep}){1-5}
    \rowcolor{lightgray}\multicolumn{2}{r@{}}{List targets $\rightarrow$ }& \multicolumn{3}{c@{}}{Targets}& \\
   \hhline{>{\arrayrulecolor{lightgray}}-->{\arrayrulecolor{black}}--->{\arrayrulecolor{lightgray}}->{\arrayrulecolor{black}}}
    \rowcolor{lightgray}\multicolumn{2}{l@{}}{\cellcolor{lightgray}$\downarrow$ Sources} & Target 1 & Target 2 & Target 3 \\ \cmidrule[\lightrulewidth](r{-\tabcolsep}){1-5}
    %% body of table
    \multirow{7}{*}[\defaultaddspace]{\rotatebox[origin =tr]{90}{sources}} &\cellcolor{lightgray}Source 1 & Description 10
             & Description 11
             & Description 12 \\[2\defaultaddspace]
    &\cellcolor{lightgray}Source 2 (additional info)
             & Description 20
             & Description 21
             & Description 22, \par
               Description 23, \par
               Description 24 \\[\defaultaddspace]
    &\cellcolor{lightgray}Source 3 & Description 30
             & Description 31
             & Description 32, \par
               Description 33, \par
               Description 34 \\
    \cmidrule[\heavyrulewidth](r{-\tabcolsep}){1-5}
    \end{tabularx}
    \end{table}

    \end{document} 

enter image description here

Bernard
  • 271,350