7

I am a LaTeX beginner and facing an issue that I can't seem to find the answer for... I am creating a table (using booktabs but my issue also occurs when not using the package), and whenever I remove the space on the edges of the table (with @{}) and try to colour individual rows at the same time, the space taken by the colour is wider than the table. See the following example:

\usepackage{array}
\usepackage{colortbl}
\usepackage[table,xcdraw]{xcolor}
% [...]

\fbox{\begin{tabular}{@{}ll@{}} foo & bar\ \rowcolor[HTML]{BFBFBF} bar & foo \end{tabular}}

Which results in the following:

rowcolor is too wide!

Thanks in advance for any help/pointer!

Laura
  • 71

2 Answers2

5

You could use NiceTabular from the nicematrix package instead:

enter image description here

\documentclass{article}
\usepackage[table,xcdraw]{xcolor}

\definecolor{mygray}{HTML}{BFBFBF}

\usepackage{nicematrix} \begin{document}

\fbox{\begin{NiceTabular}{@{}ll@{}}[code-before=\rowcolor{mygray}{2}] foo & bar\ bar & foo \end{NiceTabular}}

\end{document}

leandriis
  • 62,593
  • 1
    Remark: It's not mandatory to define a color mygray. It's possible to write code-before = \rowcolor[HTML]{BFBFBF}{2}. – F. Pantigny Mar 19 '21 at 17:45
5

I suggest setting \fboxsep to $0$ instead, and keeping the spaces at both ends of the table, loading cellspace for the vertical spacing at the top and bottom of the table:

\documentclass{article}
\usepackage{array}
\usepackage[table,xcdraw]{xcolor}
\usepackage{cellspace}
\setlength{\cellspacetoplimit}{4pt}
\setlength{\cellspacebottomlimit}{4pt}
% [...]
\begin{document}

{\setlength{\fboxsep}{0pt} \fbox{% \begin{tabular}{Sll} foo & bar\ \rowcolor[HTML]{BFBFBF} bar & foo \end{tabular}} } \end{document}

enter image description here

Bernard
  • 271,350