0

Based on this answer you know how to easily create rows with background colors. This also works for booktabs.

Edit: This is how it looks by default with \begin{tabular}{lccc}:
with padding

But we want to change that…
So there is a problem if you intend to use @{}. @{} makes the table a little smaller at the left and right side and it looks very professional.

Usually… Because with colored rows, it does not, because they (horizontally) "overflow" the table.

Here is an example:

\documentclass{article}
\usepackage{array,graphicx}
\usepackage{booktabs}
\usepackage[dvipsnames, table]{xcolor}

\begin{document}

\begin{table} \centering
    \rowcolors{1}{white}{yellow}

    \begin{tabular}{@{}l ccc@{}}
        & Head & Head & Head \\
        \cmidrule{1-4}
        line 1 & Y & N & Y \\
        line 2 & Y & N & Y \\
        line 3 & Y & N & Y \\
        line 4 & Y & N & Y \\
        \rowcolor{red}
        line 5 & Y & N & Y \\
        \cellcolor{green}%
        line 6 & Y & N & 
        \cellcolor{green}%
        Y \\
        \cmidrule{1-4}
    \end{tabular}
\end{table}

\end{document}

exaggerated example with green, yellow an red rows

rugk
  • 1,090
  • 1
  • 10
  • 27

1 Answers1

1

Oh sorry for the dupe, just found this answer, which looks awfully complicated (and maybe there are better solutions?), but here it is adjusted to my example:

\documentclass{article}
\usepackage{array,graphicx}
\usepackage{booktabs}
\usepackage[dvipsnames, table]{xcolor}

\begin{document}

\begin{table} \centering
    \rowcolors{1}{white}{yellow}

    \begin{tabular}{>{\kern-\tabcolsep}l ccc<{\kern-\tabcolsep}}
        & Head & Head & Head \\
        \cmidrule{1-4}
        line 1 & Y & N & Y \\
        line 2 & Y & N & Y \\
        line 3 & Y & N & Y \\
        line 4 & Y & N & Y \\
        \rowcolor{red}
        line 5 & Y & N & Y \\
        \cellcolor{green}%
        line 6 & Y & N & 
        \cellcolor{green}%
        Y \\
        \cmidrule{1-4}
    \end{tabular}
\end{table}

\end{document}

works now

rugk
  • 1,090
  • 1
  • 10
  • 27
  • the simpler version is just to go with the flow and allow colortbl to add the column padding either side, so just use {lccc} it is the @{} that make things tricky. That usually looks better anyway rather than have the line text flush to the edge of the colour panel. – David Carlisle Aug 29 '20 at 12:07
  • Hmm added that to the question for comparison. And I think, hmm. Naw. Depends on the style you like and maybe even on the content. – rugk Aug 29 '20 at 12:30