3

When space on either side of a table is removed with @{}, then the color added with \rowcolors will expand beyond the width of the table. In the answer provided to a similar question, this is fixed by putting @{\kern\tabcolsep} instead of @{}. But that's equivalent to just removing @{} from the code, so I don't really consider that a solution. Is there another way to limit the row colors to the widt of the tabel?

\documentclass{article}
\usepackage[table]{xcolor}
\begin{document}
\rowcolors{2}{}{gray}
\begin{tabular}{@{}*4{l}@{}}
\hline
1 & 2 & 3 & 4\\
5 & 6 & 7 & 8\\
1 & 2 & 3 & 4\\
5 & 6 & 7 & 8\\
\end{tabular}
\end{document}

enter image description here

Sverre
  • 20,729

2 Answers2

5
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{array}
\begin{document}
\rowcolors{2}{}{gray}
\begin{tabular}{@{}>{\columncolor{white}[0pt][\tabcolsep]}lll>{\columncolor{white}[\tabcolsep][0pt]}l@{}}
\hline
1 & 2 & 3 & 4\\
5 & 6 & 7 & 8\\
1 & 2 & 3 & 4\\
5 & 6 & 7 & 8\\
\end{tabular}
\end{document}

enter image description here

Ulrike Fischer
  • 327,261
0

With {NiceTabular} of nicematrix, you will have directly the expected output (and you won't see the thin white lines you see in Ulrike Fisher's answer).

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}
\begin{document}

\begin{NiceTabular}{@{}*4{l}@{}} \CodeBefore \rowcolors{2}{}{gray} \Body \hline 1 & 2 & 3 & 4\ 5 & 6 & 7 & 8\ 1 & 2 & 3 & 4\ 5 & 6 & 7 & 8\ \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