4

I can highlight a row in a table (see below), but it's not centered on the text. Is there a way to fix this?

Code:

\documentclass[11pt]{article}
\usepackage{color, colortbl,booktabs}
\definecolor{Gray}{gray}{0.9}
\begin{document}
\begin{table}[!htbp]
    \centering
\begin{tabular}{llccc}
\midrule
&&Mean&Sigma&Total Points \& Costs\\ 
\midrule
2014    &   Unused  &   377 &   214 &    1,618,193\\ [0.5ex]
    &   Deducted    &   213 &   141 &    565,792\\ [0.5ex]
    &   Carryover   &   245 &   91  &    1,052,401  \\ [0.5ex]
    &   Used    &   308 &   187 &    1,115,463\\ [0.5ex]
    \rowcolor{Gray}
    &   Costs (dollars) &   590 &   343 &    2,162,652  \\ [0.5ex]
\bottomrule
\end{tabular} 
\end{table}
\end{document}

Output:

enter image description here

Output without [0.5ex]: Not much of a change - enter image description here

6
  • 1
    This is due to [0.5ex]. Either leave them away or if you want more space around the entries, take a look at the cellspace package. However, you might want to keep in mind that the gray color will not extend completely to the black line since horizontal lines from the booktabs package have a small white spcae around them. (See also: tex.stackexchange.com/q/266438/134144)
    – leandriis
    Commented Feb 1, 2020 at 14:59
  • Thanks. I removed the [0.5ex] and there was minimal difference. I'll check out the cellspace package.
    – Angus
    Commented Feb 1, 2020 at 15:07
  • there is .5ex difference if you remove the [0.5ex] compare the space from the tails of the commas in the last column to the coloured panel below. Commented Feb 1, 2020 at 15:19
  • After removing all [0.5ex] after row termination try with \setlength\extrarowheight{2pt} in the table preamble .
    – Zarko
    Commented Feb 1, 2020 at 15:29
  • @Zarko so cautious with your 2pt. I was somewhat more extravagant in my answer. Commented Feb 1, 2020 at 15:31

5 Answers 5

5

The text only looks relatively high in the coloured panel as you have tall characters like C and d and the digits and no characters with descenders other than a small part of the ()

However you can add a zero width rule with whatever height and depth you want to adjust the size of the content in the row:

enter image description here

\documentclass[11pt]{article}
\usepackage{color, colortbl,booktabs}
\definecolor{Gray}{gray}{0.9}
\begin{document}
\begin{table}[!htbp]
    \centering
\begin{tabular}{llccc}
\midrule
&&Mean&Sigma&Total Points \& Costs\\ 
\midrule
2014    &   Unused  &   377 &   214 &    1,618,193\\ [0.5ex]
    &   Deducted    &   213 &   141 &    565,792\\ [0.5ex]
    &   Carryover   &   245 &   91  &    1,052,401  \\ [0.5ex]
    &   Used    &   308 &   187 &    1,115,463\\ [0.5ex]
    \rowcolor{Gray}
   \rule[-1cm]{0pt}{2.4cm} &   Costs (dollars) &   590 &   343 &    2,162,652  \\ [0.5ex]
\bottomrule
\end{tabular} 
\end{table}
\end{document}
4

I propose this code, using makecell and loading xcolor with option table – needless to load colortbl, the option does it. I removed the thin white strip between the coloured row and the bottom, and used siunitx for a better alignment of the numbers. Last I propose another shade of gray that pleases me better… :-)

\documentclass[11pt]{article}
\usepackage{booktabs, makecell}
\usepackage[table, svgnames]{xcolor}
\usepackage{siunitx}
\definecolor{Gray}{gray}{0.9}

\begin{document}

\begin{table}[!htbp]
    \centering
\sisetup{table-format=3.0, table-number-alignment=center, group-separator={,}}
\setlength{\extrarowheight}{0.5ex}
\begin{tabular}{llSSS[table-format=7.0]}
\midrule
&&{Mean}&{Sigma}& {\makecell{Total Points \\[-0.5ex] \& Costs}} \\
\midrule
2014 & Unused & 377 & 214 & 1618193\\
    & Deducted & 213 & 141 & 565792\\
& Carryover & 245 & 91 & 052401 \\
    & Used & 308 & 187 & 1115463\\
    \rowcolor{WhiteSmoke!70!Lavender}
    & Costs (dollars) & 590 & 343 & 2162652 \\
\noalign{\vskip-\aboverulesep}
\bottomrule
\end{tabular}
\end{table}

\end{document} 

…

3

With new package tabularray:

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\begin{document}
    \begin{table}[htb]
    \centering
    \sisetup{table-format=3.0, 
             group-separator={,}}
\begin{tblr}{colspec={ll *{2}{Q[c,m,si]}
                              Q[c,m,si={table-format=7},wd=6em]},
             rows={rowsep=1pt},
             row{Z}={bg=gray9, abovesep+=3pt, belowsep+=3pt},
             }
    \toprule
        &   &   {{{Mean}}}
                &   {{{Sigma}}}
                    &   {{{Total Points \& Costs}}} 
                                                \\
    \midrule
2014    & Unused    & 377   & 214   & 1618193   \\
        & Deducted  & 213   & 141   & 565792    \\
        & Carryover & 245   & 91    & 052401    \\
        & Used      & 308   & 187   & 1115463   \\
        & Costs (dollars)
                    & 590   & 343   & 2162652   \\
    \bottomrule
\end{tblr}
    \end{table}
\end{document}

enter image description here

2

You can also set \arraystretch instead of add [0.5ex] to every row. I propose also some other changes unrelated with the question:

enter image description here

\documentclass[11pt]{article}
\usepackage{xcolor,colortbl,booktabs,array,siunitx}
\begin{document}
\begin{table}
\renewcommand\arraystretch{1.14}
\begin{tabular}{llcS[table-format=3.0]S[table-format=7.0]}
\midrule
&   &   Mean & {Sigma} & {Total Points \& Costs}\\ 
\midrule
2014    &   Unused      & 377 &   214 &    1618193\\
        &   Deducted    & 213 &   141 &    565792\\
        &   Carryover   & 245 &   91  &    1052401\\
        &   Used        & 308 &   187 &    1115463\\\rowcolor{gray!20}
        &   Costs (\$) &  590 &   343 &    2162652  \\
\bottomrule
\end{tabular} 
\end{table}
\end{document}
2

Here is what by setting \extrarowheight=0.5ex in {NiceTabular} which can color rows in a way compatible with booktabs.

\documentclass[11pt]{article}
\usepackage{booktabs}
\usepackage[svgnames]{xcolor}
\usepackage{siunitx}
\definecolor{Gray}{gray}{0.9}

\usepackage{nicematrix}

\begin{document}

\begin{table}[!htbp]
    \centering
\sisetup{table-format=3.0, table-number-alignment=center, group-separator={,}}
\setlength{\extrarowheight}{0.5ex}
\begin{NiceTabular}{llSSS[table-format=7.0]}[colortbl-like]
\midrule
&&{Mean}&{Sigma}& \Block{}{Total Points \\[-0.5ex] \& Costs} \\
\midrule
2014 & Unused & 377 & 214 & 1618193\\
    & Deducted & 213 & 141 & 565792\\
& Carryover & 245 & 91 & 052401 \\
    & Used & 308 & 187 & 1115463\\
    \rowcolor{WhiteSmoke!70!Lavender}
    & Costs (dollars) & 590 & 343 & 2162652 \\
\bottomrule
\end{NiceTabular}
\end{table}

\end{document} 

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

Output of the above code

You must log in to answer this question.

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