30

I have created a rather complicated table with some columns that I want to color. For the table, I chose the booktabs and tabularx packages. For coloring the columns, I use colortbl.

The problem that I have is that \midrules show a white space, and the result does not please me at all. Here is a MWE:

\documentclass[a5paper]{article}
\usepackage{xcolor}
\usepackage{booktabs,colortbl,tabularx}
\begin{document}
\pagestyle{empty}
\begin{table*}[!htb]
  \centering
  \begin{tabularx}{4cm}{>{\columncolor{gray}}cX>{\columncolor{gray}}c}
    \toprule
    1&2&3\\
    \midrule
    one&two&three\\
    one&two&three\\
    \cmidrule{2-3}
    one&two&three\\
    \midrule
    un&deux&trois\\
    \bottomrule
  \end{tabularx}
  \caption{caption}
\end{table*}
\end{document}

latex result

Looking around I found that booktabs supports colortbl in its latest version. However, it seems that it only supports colored horizontal lines.

I tried not using booktabs and replacing midrules for hlines. My huge table looks too crowded (once you booktab, you never go back, I guess).

My question is: is it possible to achieve a correct coloring in midrules ? Since I fear that the response is no: what low-cost recommendation do you have in order to color the columns of my table?

(When I say low-cost, I mean that I do not want to re-write my table contents)

gernot
  • 49,614
YuppieNetworking
  • 5,919
  • 7
  • 36
  • 40
  • 6
    from typographers view it makes no sense to have lines and colored cells –  Feb 16 '11 at 11:07

3 Answers3

24

There's no way to do this given the way booktabs inserts space between above and below its rules. As Herbert notes inthe comments, it's not clear that rules and colours are really a logical combination. So probably the best you can do is to increase the row height and set the space above and below the rules to 0.

\documentclass{article}
\usepackage{xcolor}
\usepackage{booktabs,colortbl,tabularx}

\begin{document}
% Your original table
  \begin{tabularx}{4cm}{>{\columncolor{gray}}cX>{\columncolor{gray}}c}
    \toprule
    1&2&3\\
    \midrule
    one&two&three\\
    one&two&three\\
    \cmidrule{2-3}
    one&two&three\\
    \midrule
    un&deux&trois\\
    \bottomrule
  \end{tabularx}
% same table with booktab rules but no above space and making rows bigger
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\setlength{\extrarowheight}{.75ex}
  \begin{tabularx}{4cm}{>{\columncolor{gray}}cX>{\columncolor{gray}}c}
    \toprule
    1&2&3\\
    \midrule
    one&two&three\\
    one&two&three\\
    \cmidrule{2-3}
    one&two&three\\
    \midrule
    un&deux&trois\\
    \bottomrule
  \end{tabularx}
% same table without booktab rules
\setlength{\extrarowheight}{.75ex}
  \begin{tabularx}{4cm}{>{\columncolor{gray}}cX>{\columncolor{gray}}c}
    \hline
    1&2&3\\
    \hline
    one&two&three\\
    one&two&three\\
    \cline{2-3}
    one&two&three\\
    \hline
    un&deux&trois\\
    \hline
  \end{tabularx}
\end{document}

The middle table still looks marginally better than the third one.

three tables

gernot
  • 49,614
Alan Munn
  • 218,180
2

Rules and colour in a tabular is not necessary, as it is said in the note, but if you nevertheless insist using it, the package cals is able to handle both:

\documentclass{article}
\usepackage{cals}
\usepackage{xcolor}

\begin{document}

\begin{table*}[!htb]
\begin{calstable}[c]
% Defining columns 
\colwidths{{1.5cm}
            {2.25cm}
            {1.75cm}
            }

% Set up the tabular
\makeatletter
\def\cals@framers@width{0.8pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0pt}
\def\cals@bodyrs@width{0.4pt}    % "midrule" - line between heading and body
\cals@setpadding{Ag}
\cals@setcellprevdepth{Al}
\def\cals@cs@width{0pt}             % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.6pt}
\def\cals@bgcolor{}

\def\gray{\ifx\cals@bgcolor\empty     % "Switch" to turn on and off colour
    \def\cals@bgcolor{gray!50}
\else \def\cals@bgcolor{} \fi}

\def\bb{\ifx\cals@borderB\relax     % Bottom border switch (off-on)
\def\cals@borderB{0pt}
\else \let\cals@borderB\relax\fi}

% R1B1
\thead{%
\brow
    \gray\alignC\cell{1}\gray
    \alignL\cell{2}
    \gray\alignC\cell{3}\gray
\erow
}
%R2B2
\brow
    \gray\alignC\cell{\vfil one}\gray
    \alignL\cell{\vfil two}
    \gray\alignC\cell{\vfil three}\gray
\erow
%R3B3
\brow
    \gray\bb\alignC\cell{\vfil one}\bb\gray
    \alignL\cell{\vfil two}
    \gray\alignC\cell{\vfil three}\gray
\erow
%R4B4
\brow
    \gray\alignC\cell{\vfil one}\gray
    \alignL\cell{\vfil two}
    \gray\alignC\cell{\vfil three}\gray
\erow
%R4B4
\brow
    \gray\alignC\cell{\vfil un}\gray
    \alignL\cell{\vfil deux}
    \gray\alignC\cell{\vfil trois}\gray
\erow
\makeatletter
\end{calstable}\par
  \caption{caption}
\end{table*}
\end{document}

enter image description here

Sveinung
  • 20,355
2

The environment {NiceTabular} of nicematrix provides tools to color cells, rows and columns in a way compatible with the horizontal rules of booktabs.

\documentclass[a5paper]{article}
\usepackage{xcolor}
\usepackage{booktabs,nicematrix}
\usepackage{lipsum}

\begin{document}

\pagestyle{empty}

\lipsum[1]

\begin{table}[!htb] \centering \begin{NiceTabular}[width=4cm,colortbl-like]{>{\columncolor{gray}}cX>{\columncolor{gray}}c} \toprule 1&2&3\ \midrule one&two&three\ one&two&three\ \cmidrule{2-3} one&two&three\ \midrule un&deux&trois\ \bottomrule \end{NiceTabular} \caption{caption} \end{table}

\end{document}

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

Output of the above code

F. Pantigny
  • 40,250