1

I am trying to create a table with fractions in it and every other row being coloured. Because of the fractions, I am having to space out the rows in the table, but this seems to be conflicting with the colouring of the rows and now the colouring isn't working.

Here is where my code is at the moment:

\documentclass{article}
\usepackage{amsmath}
\usepackage{color, colortbl}
\definecolor{Gray}{gray}{0.9}
\def\xstrut{\rule[-2ex]{0pt}{5ex}}

\usepackage{booktabs}
\usepackage{cellspace}
\setlength\cellspacetoplimit{3pt}
\setlength\cellspacebottomlimit{3pt}
\usepackage{makecell}
\setcellgapes{3pt}

\begin{document}

\begin{table}[!htbp]
  \centering\makegapedcells
  \setlength{\tabcolsep}{1em}
  \begin{tabular}{c | cc | cc}
    \toprule
    \textbf{Parameters} & \multicolumn{2}{c|}{A} & \multicolumn{2}{c}{B} \\ \midrule
\rowcolor{Gray} $(2, n, 3)$ & $\displaystyle \dfrac{2}{3}$ & $\displaystyle \dfrac{3}{2}$ & $\displaystyle \dfrac{2}{3}$ & $\displaystyle \dfrac{3}{2}$ \\

$(2, n, 4)$ & $\displaystyle \dfrac{3}{4}$ & $\displaystyle \dfrac{4}{3}$ & $\displaystyle \dfrac{3}{4}$ & $\displaystyle \dfrac{4}{3}$ \\

\rowcolor{Gray} $(4, n, 8)$ & $\displaystyle \dfrac{1}{4}$ & $\displaystyle 4$ & $\displaystyle \dfrac{5}{8}$ & $\displaystyle \dfrac{8}{5}$ \\ \bottomrule
  \end{tabular}
  \caption{Comparison of A and B}
  \label{tab:results}
\end{table}


\end{document}

which produces the following table:

Picture of table produced

which I am happy with, but I would like the second and fourth rows shaded.

Any help would be really appreciated.

2
  • Thank you, but I have done multiple examples with coloured rows, I just can't get it working in this one particular example. I have just tried to combine my spaced table with the example in the link you just gave, and it compiles fine with no errors, yet the rows are still not shaded. May 4 '17 at 18:39
4

There's indeed a problem with (part of) makecell and colours in tables. There's also a problem with booktabs: rules have some vertical padding and this produces white strips at top and bottom of coloured rows. The simplest solution consists in suppressing this padding in coloured tables, and replacing it with a customised padding obtained with cellspace.

In the following code I replaced the old color package with xcolor and option [table], which makes loading colortbl needless, and adds new functionalities with the \rowcolorscommand:

\documentclass{article}
\usepackage{amsmath}
\usepackage[table, svgnames]{xcolor}
\definecolor{Gray}{gray}{0.9}
\def\xstrut{\rule[-2ex]{0pt}{5ex}}

\usepackage{booktabs}
\usepackage{cellspace}
\setlength\cellspacetoplimit{3pt}
\setlength\cellspacebottomlimit{3pt}

\begin{document}

\begin{table}[!htbp]
  \centering%
  \setlength{\tabcolsep}{1em}
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
\rowcolors{1}{}{Gray}
  \begin{tabular}{Sc | ScSc | cc}
    \toprule
    \textbf{Parameters} & \multicolumn{2}{c|}{A} & \multicolumn{2}{c}{B} \\ \midrule
  $(2, n, 3)$ & $\displaystyle \dfrac{2}{3}$ & $\displaystyle \dfrac{3}{2}$ & $\displaystyle \dfrac{2}{3}$ & $\displaystyle \dfrac{3}{2}$ \\

    $(2, n, 4)$ & $\displaystyle \dfrac{3}{4}$ & $\displaystyle \dfrac{4}{3}$ & $\displaystyle \dfrac{3}{4}$ & $\displaystyle \dfrac{4}{3}$ \\

     $(4, n, 8)$ & $\displaystyle \dfrac{1}{4}$ & $\displaystyle 4$ & $\displaystyle \dfrac{5}{8}$ & $\displaystyle \dfrac{8}{5}$ \\ \bottomrule
    \end{tabular}
  \caption{Comparison of A and B}
  \label{tab:results}
\end{table}

\end{document} 

enter image description here

7
  • Fantastic, exactly what I needed. Thank you! May 4 '17 at 19:32
  • Congrats to 100k!!!!
    – user31729
    May 4 '17 at 19:38
  • @Christian Hupfer: Thanks! Do I deserve a glass of champagne?
    – Bernard
    May 4 '17 at 19:40
  • @Bernard: Well, ... uhmm.... ask the other 100k users in chat please ;-)
    – user31729
    May 4 '17 at 19:41
  • 1
    @Bernard Please, if you can, make a visit to the chat! We should have a party for you! Congratulations!
    – egreg
    May 4 '17 at 20:07
1

You can do that table very easily with {NiceTabular} of nicematrix. With that environment you can draw vertical rules comptaible with the rules of booktabs (even if it's not at all in the spirit of booktabs to draw vertical rules). You have tools to color rows in a way compatible with booktabs and you have an built-in key cell-space-limits (no need of cellspace).

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}

\begin{document}

\begin{table}[!htbp]
\centering
\setlength{\tabcolsep}{1em}
\begin{NiceTabular}{c|cc|cc}[cell-space-limits=3pt]
\CodeBefore
  \rowcolors[gray]{1}{}{0.9}
\Body
  \toprule
  \textbf{Parameters} & \Block{1-2}{A} && \Block{1-2}{B} \\ \midrule
   $(2, n, 3)$ & $\dfrac{2}{3}$ & $\dfrac{3}{2}$ & $\dfrac{2}{3}$ & $\dfrac{3}{2}$ \\
   $(2, n, 4)$ & $\dfrac{3}{4}$ & $\dfrac{4}{3}$ & $\dfrac{3}{4}$ & $\dfrac{4}{3}$ \\
   $(4, n, 8)$ & $\dfrac{1}{4}$ & $4$ & $\dfrac{5}{8}$ & $\dfrac{8}{5}$ \\ \bottomrule
  \end{NiceTabular}
\caption{Comparison of A and B}
\label{tab:results}
\end{table}

\end{document}

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

Output of the above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.