7

I'm highlighting a table cell by shading it. However, the left-padding is exaggerated. How can I reduce that spacing, that leads to a larger grayed area on the left?

enter image description here

\documentclass{memoir}
\usepackage{xcolor}
\usepackage{colortbl}
\begin{document}
\begin{tabular}{rrr}
3.1544 & 1.9929     & 2.6229\\
3.1140 & 1.6107     & 2.4226 \\
12.40208    & 5.92132   & \cellcolor{gray!30} 9.41145 \\
\end{tabular}
\end{document}

In my actual document it's an siunitx S column, in case that matters.

2
  • 3
    Remove the space between {gray!30} and 9.41145.
    – Sandy G
    Feb 21 at 17:11
  • @SandyG Thank you! This made the spacing smaller, same size as the spacing to the right. I would like it even smaller, any idea how I could achieve that? Adding a negative space to each cell in the column seems a bit hacky.
    – Anna
    Feb 21 at 18:28

2 Answers 2

8

With nicematrix (and tikz) you can set precisely the inner ysep and inner xsep.

enter image description here

\documentclass{memoir}

\usepackage{nicematrix, tikz}
\usetikzlibrary{fit}

\tikzset{highlight/.style={fill=gray!30, inner ysep=1pt, inner xsep=2pt, fit=#1}}

\begin{document}
\begin{NiceTabular}{rrr}
\CodeBefore [create-cell-nodes]
    \tikz{\node[highlight=(3-3)]{};}
\Body
3.1544 & 1.9929     & 2.6229\\
3.1140 & 1.6107     & 2.4226 \\
12.40208 & 5.92132  & 9.41145
\end{NiceTabular}
\end{document}
1
  • Thank you! This both solved my problem and introduced me to nicematrix.
    – Anna
    Feb 22 at 8:31
8

You could use \colorbox instead of \cellcolor.

enter image description here

\documentclass{memoir}
\usepackage{xcolor}  % for '\colorbox' macro
\usepackage{siunitx} % for 'S' column type
\usepackage{booktabs}% for '\toprule' and '\bottomrule' macros

\begin{document}

\renewcommand\arraystretch{1.15}
\sisetup{round-mode=places,round-precision=4}

\begin{tabular}{@{} S[table-format=2.4] S[table-format=1.4] S[table-format=1.4] @{}}
\toprule
 3.1544   & 1.9929   & 2.6229\\
 3.1140   & 1.6107   & 2.4226 \\
12.40208  & 5.92132  & \colorbox{gray!30}{9.4115} \\
\bottomrule
\end{tabular}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .