3

I am using this template for making tables. I tried to add \rowcolor[gray]{.8} between \toprules and \midrules but the the effect is bad in the sense that it highlight on 3/4 of the row leaving the edges white.

Any solution?

\documentclass{article}
\usepackage{colortbl, xcolor}
\usepackage{booktabs,caption,fixltx2e}
\usepackage[flushleft]{threeparttable}
\begin{document}
\definecolor{lightgray}{rgb}{.9,.9,.9}
\begin{table}
  \begin{threeparttable}
    \caption{Sample ANOVA table}
     \begin{tabular}{lllll}
        \toprule
        Stubhead & \( df \) & \( f \) & \( \eta \) & \( p \) \\
        \midrule
        Row 1    & 1        & 0.67    & 0.55       & 0.41    \\
        Row 2    & 2        & 0.02    & 0.01       & 0.39    \\
        Row 3    & 3        & 0.15    & 0.33       & 0.34    \\
        Row 4    & 4        & 1.00    & 0.76       & 0.54    \\
        \bottomrule
     \end{tabular}
    \begin{tablenotes}
      \small
      \item This is where authors provide additional information about
      the data, including whatever notes are needed.
    \end{tablenotes}
  \end{threeparttable}
\end{table}

\end{document}
2
  • At the beginning of the post there is an "Hi All,". Just wanted to point it out. It is the second time that this happens and it honestly looks like a big impolite. Jul 10, 2014 at 19:40
  • Greetings and thanks are assumed: we're quite a friendly community and we trust in newcomers to be as friendly as everybody here. We believe it's simpler to directly state the problem; the better way to thank is upvoting useful answers and accepting the most useful one, provided it solves the problem or leads to a full solution.
    – egreg
    Jul 10, 2014 at 19:59

3 Answers 3

4

I think the problem comes from booktabs, which adds some vertical space above and below its rules. This vertical space seems not to be considered as part of the row cells, and hence is not coloured.

A workaround consists in using the cellspace package, which defines a minimal vertical vertical space between the top of a row and the above \hline and symmetrically a minimal vertical space between the bottom of a row and the below \hline. This has more or less the same effect as \addlinespace from booktabs, but is taken into consideration when colouring the cells.

Another feature of booktabs, namely horizontal rules of different thicknesses, can be achieved with the makecell package, which has an \Xhline{thickness} command. So I defined a \thickhline command, that has the same thickness as the default for \top/\bottomrules in booktabs (0.08em).

Here is a code:

\documentclass{article}
\usepackage{colortbl, xcolor}
\usepackage{booktabs,caption,fixltx2e}
\usepackage[flushleft]{threeparttable}

\usepackage{cellspace}
\setlength\cellspacetoplimit{6pt}
\setlength\cellspacebottomlimit{6pt}

\usepackage{makecell} 
\newcommand\thickhline{\Xhline{0.08em}}

\begin{document}
\definecolor{lightgray}{rgb}{.9,.9,.9}
\begin{table}[!ht]
  \begin{threeparttable}
    \caption{Sample ANOVA table}
     \begin{tabular}{*{6}{Sl}}
        %\toprule
\thickhline
 \rowcolor[gray]{.8} Stubhead & \( df \) & \( f \) & \( \eta \) & \( p \) \\
\hline
        Row 1 & 1 & 0.67 & 0.55 & 0.41 \\
        Row 2 & 2 & 0.02 & 0.01 & 0.39 \\
        Row 3 & 3 & 0.15 & 0.33 & 0.34 \\
        Row 4 & 4 & 1.00 & 0.76 & 0.54 \\
\thickhline
     \end{tabular}\smallskip
    \begin{tablenotes}
      \small
      \item This is where authors provide additional information about
      the data, including whatever notes are needed.
    \end{tablenotes}
  \end{threeparttable}
\end{table}

\end{document} 

enter image description here

7
  • You can use features of \booktabs, for instance \specialrule{\heavyrulewidth}{0pt}{0pt}.
    – egreg
    Jul 10, 2014 at 20:15
  • But then you don't have any vertical space added, I guess? Which makes ugly tables.
    – Bernard
    Jul 10, 2014 at 20:18
  • The second and third arguments are for spaces above and below the rule, respectively. My advice is not shading the table header, the heavier rules already show it clearly.
    – egreg
    Jul 10, 2014 at 20:19
  • @egreg: That's another question. The problem remains if you have, say, a table with many rows and want to alternate colours in rows.
    – Bernard
    Jul 10, 2014 at 20:34
  • That's a different problem; but in that case the shading need not touch the rules.
    – egreg
    Jul 10, 2014 at 20:38
1

Similar to @Bernard answer ... Difference are in use of packages boldline and treepartablex. Instead of packages colortbl and xcolor is used only xcolor with option table. Also it is declared only five columns as they are used in table:

\documentclass{article}
\usepackage[table]{xcolor}
\definecolor{lightgray}{rgb}{.9,.9,.9}
\usepackage{caption}
\usepackage{boldline, cellspace}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}
\usepackage[referable,flushleft]{threeparttablex}

\begin{document}
    \begin{table}[!ht]
\begin{threeparttable}
\caption{Sample ANOVA table}
\label{tab:my table}
    \begin{tabular}{*{5}{Sl}}
    \hlineB{2}
    \rowcolor[gray]{.8} Stubhead & \( df \) & \( f \) & \( \eta \) & \( p \) \\
    \hlineB{1.5}
Row 1 & 1 & 0.67 & 0.55 & 0.41 \\
Row 2 & 2 & 0.02 & 0.01 & 0.39 \\
Row 3 & 3 & 0.15 & 0.33 & 0.34 \\
Row 4 & 4 & 1.00 & 0.76 & 0.54 \\
    \hlineB{2}
     \end{tabular}
    \begin{tablenotes}\footnotesize
\note This is where authors provide additional information about
      the data, including whatever notes are needed.
    \end{tablenotes}
\end{threeparttable}
    \end{table}
\end{document}

enter image description here

2
  • [table]{colorx} internally loads colortbl... see here Apr 11, 2021 at 19:20
  • 1
    @MatthiasArras, (i) correct is [table]{xcolor} (ii) it load internally colortbl (iii) to colortbl add some functionality as \rowcolors and \rowcolors* etc (see 2.12 Color in tables, page 28 in package documentation), (iv) I'm very aware what xcolor offer, regardless that I didn't mentioned explicitly what you noted in in your comment :-). Anyway, thank you for your comment.
    – Zarko
    Apr 11, 2021 at 19:36
0

The environment {NiceTabular} of nicematrix has tools to color rows (cells, columns, blocks, etc.) in a way compatible with the rules of booktabs.

However, in order to use {NiceTabular} in {threeparttable}, you have first to tell it to threeparttable with the dedicated tool.

\makeatletter
\AddToHook{env/threeparttable/begin}
    {\TPT@hookin{NiceTabular}\TPT@hookin{NiceTabular*}}
\makeatother

Here is the full MWE.

\documentclass{article}
\usepackage{xcolor}
\usepackage{booktabs,caption}
\usepackage[flushleft]{threeparttable}
\usepackage{nicematrix}
\begin{document}

\makeatletter
\AddToHook{env/threeparttable/begin}
    {\TPT@hookin{NiceTabular}\TPT@hookin{NiceTabular*}}
\makeatother


\begin{table}
  \begin{threeparttable}
    \caption{Sample ANOVA table}
     \begin{NiceTabular}{lllll}
     \CodeBefore
       \rowcolor[gray]{0.8}{1}
     \Body
        \toprule
        Stubhead & \( df \) & \( f \) & \( \eta \) & \( p \) \\
        \midrule
        Row 1    & 1        & 0.67    & 0.55       & 0.41    \\
        Row 2    & 2        & 0.02    & 0.01       & 0.39    \\
        Row 3    & 3        & 0.15    & 0.33       & 0.34    \\
        Row 4    & 4        & 1.00    & 0.76       & 0.54    \\
        \bottomrule
     \end{NiceTabular}
    \begin{tablenotes}
      \small
      \item This is where authors provide additional information about
      the data, including whatever notes are needed.
    \end{tablenotes}
  \end{threeparttable}
\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 .