3

So I'm trying to color the rows in my table, but in the rows where I use \makecell, the colouring doesn't work properly. Somebody knows a solution for this?

\usepackage{xcolor, colortbl} %kleurtjes voor table
\definecolor{TopRow}{rgb}{0.4,0.7,1}
\definecolor{NormalRow}{rgb}{0.8,0.9,1}
\usepackage{makecell}

    \begin{center}
    \begin{tabular}{|l|l|l|}
    \hline
    \rowcolor{TopRow}
    \textbf{Peptide Enrichtments} & \textbf{Hits} & \textbf{Found Genes} \\
    \hline
    \rowcolor{NormalRow}
    Nothing &3 & \makecell[lt]{RAB5C \\ TNKS1BP1 \\ WDR43}\\
    \hline
    20X dilution& 1 & AHCY \\
    \hline
    \rowcolor{NormalRow}
    TiO2& 2 & \makecell[lt]{TNKS1BP1\\ITPR2}\\
    \hline
    TiO2-3 &0& -\\
    \hline
    \rowcolor{NormalRow}
    Fe-NTA & 0&- \\
    \hline
    \end{tabular}
    \end{center}

rowcolor doesn't fill whole cell

EDIT:

So I did the following:

Nothing &3 & {\colorbox{NormalRow}{\makecell[lt]{RAB5C \\ TNKS1BP1 \\ WDR43}}}\\

But now the alignment is good anymore. anyone has solutions for that? After using \colorbox the alignment isn't correct anymore

Leo
  • 33

2 Answers2

2

Instead of trying to make makecell and colortbl compatible, I'd suggest dropping the usage of makecell. You can achieve the same layout without it as shown in the following MWE:

\documentclass{article}

\usepackage[table]{xcolor}
\definecolor{TopRow}{rgb}{0.4,0.7,1}
\definecolor{NormalRow}{rgb}{0.8,0.9,1}

\begin{document}

\begin{tabular}{|l|l|l|}
  \hline
  \rowcolor{TopRow}
    \textbf{Peptide Enrichtments} & \textbf{Hits} & \textbf{Found Genes} \\
  \hline
  \rowcolor{NormalRow}
    Nothing & 3 & RAB5C \\
  \rowcolor{NormalRow}
            &   & TNKS1BP1 \\
  \rowcolor{NormalRow}
            &   & WDR43 \\
  \hline
    20X dilution& 1 & AHCY \\
  \hline
  \rowcolor{NormalRow}
    TiO2 & 2 & TNKS1BP1\\
  \rowcolor{NormalRow}
         &   & ITPR2 \\
  \hline
    TiO2-3 &0& -\\
  \hline
  \rowcolor{NormalRow}
    Fe-NTA & 0&- \\
  \hline
\end{tabular}

\end{document}

enter image description here


As you use color as a structuring element in your table, you might also consider dropping all the horizonal and vertical rules. In the following example, I have also added a bit of extra vertical space:

\documentclass{article}

\usepackage[table]{xcolor}
\definecolor{TopRow}{rgb}{0.4,0.7,1}
\definecolor{NormalRow}{rgb}{0.8,0.9,1}

\begin{document}
\begingroup
\renewcommand{\arraystretch}{1.2}
\begin{tabular}{lll}
  \rowcolor{TopRow}
    \textbf{Peptide Enrichtments} & \textbf{Hits} & \textbf{Found Genes} \\
  \rowcolor{NormalRow}
    Nothing & 3 & RAB5C \\
  \rowcolor{NormalRow}
            &   & TNKS1BP1 \\
  \rowcolor{NormalRow}
            &   & WDR43 \\
    20X dilution& 1 & AHCY \\
  \rowcolor{NormalRow}
    TiO2 & 2 & TNKS1BP1\\
  \rowcolor{NormalRow}
         &   & ITPR2 \\
    TiO2-3 &0& -\\
  \rowcolor{NormalRow}
    Fe-NTA & 0&- \\
\end{tabular}
\endgroup

\end{document}

enter image description here


This last example shows a more minimalistic approach without colors and bold text, but with white space and rules from the booktabs packages as structural elements:

\documentclass{article}
\usepackage{booktabs}

\begin{document}
\begin{tabular}{lll}
\toprule
    Peptide Enrichtments & Hits & Found Genes \\
\midrule
    Nothing      & 3 & RAB5C    \\
                 &   & TNKS1BP1 \\
                 &   & WDR43    \\\addlinespace
    20X dilution & 1 & AHCY     \\\addlinespace
    TiO2         & 2 & TNKS1BP1 \\
                 &   & ITPR2    \\\addlinespace
    TiO2-3       & 0 & -        \\\addlinespace
    Fe-NTA       & 0 & -        \\
\bottomrule
\end{tabular}

\end{document}

enter image description here

leandriis
  • 62,593
1

Here is a solution with {NiceTabular} of nicematrix. That environment provides a command \rowcolors (in the so-called \CodeBefore) which respect the blocks, created by \Block, when the key respect-blocks is used.

You will have a perfect output in all the PDF viewers, at all the levels of zoom.

\documentclass{article}
\usepackage{nicematrix,xcolor}

\begin{document}

\begin{NiceTabular}{|l|l|l|} \CodeBefore \rowcolor[rgb]{0.4,0.7,1}{1} \rowcolors[rgb]{2}{}{0.8,0.9,1}[respect-blocks] \Body \Hline \textbf{Peptide Enrichtments} & \textbf{Hits} & \textbf{Found Genes} \ \Hline \Block{3-1}{} Nothing & 3 & RAB5C \ & & TNKS1BP1 \ & & WDR43 \ \Hline 20X dilution& 1 & AHCY \ \Hline \Block{2-1}{} TiO2 & 2 & TNKS1BP1\ & & ITPR2 \ \Hline TiO2-3 &0& -\ \Hline Fe-NTA & 0&- \ \Hline \end{NiceTabular}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

F. Pantigny
  • 40,250