4

I am trying to construct a table in which some columns are displayed normally and others are greyed out. I am using booktabs rules throughout the document and need this table to be formatted consistently with the others.

I can easily grey out the content of the relevant columns. However, I'm having trouble greying out the rules while maintaining vertical alignment. I can use \cmidrule with a specific width (e.g. \heavyrulewidth or lightrulewidth to emulate toprule/boittomrule or midrule), but using \arrayrulecolor{} changes the vertical spacing.

How can I understand and solve the problem?

Initially, I thought that Werner's answer at https://tex.stackexchange.com/a/32366/ held the key. However, I do not really understand what is happening and have been unable to adapt it successfully. I tried drawing a regular rule with the \specialrule and then drawing a partial rule over the top in grey, but this did not work out any better than any of my other attempts.

Here are the results of my current experiments. The table on the left should look just like the one on the right, except that the second column should be greyed out. Needless to say, it is not supposed to look like Jacob's ladder.

results

Code:

\documentclass{article}
\usepackage{booktabs,array}
\usepackage[table]{xcolor}
\begin{document}
\begin{tabular}{c>{\color{gray}\arraybackslash}c}
%   \toprule
  \cmidrule[\heavyrulewidth]{1-1}%
  \arrayrulecolor{gray}%
  \cmidrule[\heavyrulewidth]{2-2}%
  \arrayrulecolor{black}%
  Black Header & Grey Header \\
%   \midrule
  \cmidrule[\lightrulewidth]{1-1}%
  \arrayrulecolor{gray}%
  \cmidrule[\lightrulewidth]{2-2}%
  \arrayrulecolor{black}%
  Black content & Grey content \\
  \specialrule{\lightrulewidth}{0pt}{-\lightrulewidth}% ateb Werner: https://tex.stackexchange.com/a/32366/
  \arrayrulecolor{gray}%
  \cmidrule[\lightrulewidth]{2-2}%
  \arrayrulecolor{black}%
  Black content & Grey content \\
  Black content & Grey content \\
  \cmidrule(lr){1-1}
  Black content & Grey content \\
  \arrayrulecolor{gray}%
  \cmidrule(lr){2-2}
  \arrayrulecolor{black}%
  Black content & Grey content \\
  \cmidrule(lr){1-1}%
  \arrayrulecolor{gray}%
  \cmidrule(lr){2-2}
  \arrayrulecolor{black}%
  Black content & Grey content \\
%   \bottomrule
  \cmidrule[\heavyrulewidth]{1-1}%
  \arrayrulecolor{gray}%
  \cmidrule[\heavyrulewidth]{2-2}%
  \arrayrulecolor{black}%
\end{tabular}
\begin{tabular}{cc}
  \toprule
  Black Header & Grey Header \\
  \midrule
  Black content & Grey content \\
  \cmidrule[\lightrulewidth]{1-2}%
  Black content & Grey content \\
  Black content & Grey content \\
  \cmidrule(lr){1-1}
  Black content & Grey content \\
  \cmidrule(lr){2-2}
  Black content & Grey content \\
  \cmidrule(lr){1-1}%
  \cmidrule(lr){2-2}
  Black content & Grey content \\
  \bottomrule
\end{tabular}
\end{document}

Note: For various reasons, it will cause considerable complications if I have to recode the other tables. In particular, the typeset version of those tables needs to remain identical in the PDF to avoid being incorrectly marked as changed. I will be using latexdiff to mark changes, so it would be easiest if their coding stayed the same too, but I could correct for this manually, if necessary.

EDIT

To clarify, the problem is not just the widths of the rules, but also the spacing inserted above and below them in a booktabs table.

I could do this:

\begin{tabular}{c}
  \toprule
  Black Header \\
  \midrule
  Black content \\
  \cmidrule[\lightrulewidth]{1-1}
  Black content \\
  Black content \\
  \cmidrule(lr){1-1}
  Black content \\
  \arrayrulecolor{white}\cmidrule(lr){1-1}
  Black content \\
  \arrayrulecolor{black}
  \cmidrule(lr){1-1}
  Black content \\
  \bottomrule
\end{tabular}%
{\color{gray}%
\begin{tabular}{c}
  \toprule
  Grey Header \\
  \midrule
  Grey content \\
  \cmidrule[\lightrulewidth]{1-1}
  Grey content \\
  Grey content \\
  \arrayrulecolor{white}\cmidrule(lr){1-1}
  Grey content \\
  \arrayrulecolor{gray}
  \cmidrule(lr){1-1}
  Grey content \\
  \cmidrule(lr){1-1}
  Grey content \\
  \bottomrule
\end{tabular}}
\begin{tabular}{c>{\color{gray}\arraybackslash}c}
  \toprule
  Black Header & Grey Header \\
  \midrule
  Black content & Grey content \\
  \cmidrule[\lightrulewidth]{1-2}%
  Black content & Grey content \\
  Black content & Grey content \\
  \cmidrule(lr){1-1}
  Black content & Grey content \\
  \cmidrule(lr){2-2}
  Black content & Grey content \\
  \cmidrule(lr){1-1}%
  \cmidrule(lr){2-2}
  Black content & Grey content \\
  \bottomrule
\end{tabular}

manually split table

This gives the correct output - or almost the correct output, at any rate. However, it destroys the structure of the table, would not be easy to maintain or adapt and is extremely error prone. So I'd rather not, if I can help it.

I've been trying to decipher the code for colortbl.sty and booktabs.sty in order to try to figure out what exactly adds the space and what, if anything, I might be able to do about it. However, I feel as if I'm wrapping myself in a giant roll of sellotape which has gone wild after the stress of seasonal gift-wrapping.

  • Maybe you could simply cheat: place \tikzmarks inside the table and overlay the column with a semi-transparent white rectangle? – user36296 Dec 29 '16 at 12:35
  • @samcarter Thanks. I wondered about that. However, I'm not sure what might happen to it when my PDF is later made into a PDF by the submission system, which already overlays and underlays various things on the pages. And I wasn't sure if it would interfere with copy-paste-ability. – cfr Dec 29 '16 at 22:12
3

You could do something like this (using red rather than grey for ease of debugging)

enter image description here

\documentclass{article}
\usepackage{booktabs,array}
\usepackage[table]{xcolor}
\begin{document}
\makeatletter


\def\CT@arc@{\ifnum\@cmidla=1 \color{red}\fi}%
\begin{tabular}{c>{\color{red}\arraybackslash}c}
%   \toprule
  \cmidrule[\heavyrulewidth]{1-1}%
  \cmidrule[\heavyrulewidth]{2-2}%
  Black Header & Grey Header \\
%   \midrule
  \cmidrule[\lightrulewidth]{1-1}%
  \cmidrule[\lightrulewidth]{2-2}%
  Black content & Grey content \\
  \cmidrule[\lightrulewidth]{1-1}%
  \cmidrule[\lightrulewidth]{2-2}%
  Black content & Grey content \\
  Black content & Grey content \\
  \cmidrule(lr){1-1}
  Black content & Grey content \\
  \cmidrule(lr){2-2}
  Black content & Grey content \\
  \cmidrule(lr){1-1}%
  \cmidrule(lr){2-2}
  Black content & Grey content \\
%   \bottomrule
  \cmidrule[\heavyrulewidth]{1-1}%
  \cmidrule[\heavyrulewidth]{2-2}%
\end{tabular}
\def\CT@arc@{}%
\begin{tabular}{cc}
  \toprule
  Black Header & Grey Header \\
  \midrule
  Black content & Grey content \\
  \cmidrule[\lightrulewidth]{1-2}%
  Black content & Grey content \\
  Black content & Grey content \\
  \cmidrule(lr){1-1}
  Black content & Grey content \\
  \cmidrule(lr){2-2}
  Black content & Grey content \\
  \cmidrule(lr){1-1}%
  \cmidrule(lr){2-2}
  Black content & Grey content \\
  \bottomrule
\end{tabular}
\end{document}
  • Wow! And thanks! Er ... what does it do exactly? I saw that \CT@arc was crucial from colortbl.sty, but didn't get much further with it. [Question status is definitely NfT (Not For Tonight).] – cfr Dec 29 '16 at 0:49
  • Should I just use \def\CT@arc@{}% to reverse the effect afterwards? I can't limit the effect to a group or save the original definition in the usual way, can I? This solution doesn't perfectly match the standard alignment, but it is so close that I think that doesn't matter. – cfr Dec 29 '16 at 20:38
  • @cfr yes. (alignment shouldn't be affected by this as far as I can see) – David Carlisle Dec 29 '16 at 21:09
  • I think the alignment may be due to a difference between \toprule and \cmidrule, though I'm not certain. – cfr Dec 29 '16 at 22:00
  • @cfr oh yes quite likely. but the misalignment from the colour insertion should be gone, after that it's not my fault so I don't care:-) – David Carlisle Dec 29 '16 at 22:01
2

Here a solution with \hhline, we can create a \Toprule which simulate \toprule with two colors black and gray

\def\Toprule{\noalign{\global\arrayrulewidth=\heavyrulewidth}\hhline{>\arrayrulecolor{black}}%
->{\arrayrulecolor{gray}}-}\noalign{\global\arrayrulewidth=\lightrulewidth}}

Here we change thickness of rule with \noalign{\global\arrayrulewidth=\heavyrulewidth} then recover rule thickness with \noalign{\global\arrayrulewidth=\lightrulewidth}.

And a command \Midrule to simulate the \midrule

\def\Midrule{\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{gray}}-}}

Complete code

\documentclass{article}
\usepackage{booktabs,array,hhline,etoolbox}
\usepackage[table]{xcolor}

\def\Toprule{\noalign{\global\arrayrulewidth=\heavyrulewidth}\hhline{>{\arrayrulecolor{black}}%
->{\arrayrulecolor{gray}}-}\noalign{\global\arrayrulewidth=\lightrulewidth}}

\def\Midrule{\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{gray}}-}}

\let\Bottomrule\Toprule

\begin{document}
\begin{tabular}{c>{\color{gray}\arraybackslash}c}
\Toprule
  Black Header  & Grey Header  \\
\Midrule
  Black content & Grey content \\
\Midrule
  Black content & Grey content \\
  Black content & Grey content \\
\arrayrulecolor{black}
\cmidrule(lr){1-1}
  Black content & Grey content \\
\arrayrulecolor{gray}
 \cmidrule(lr){2-2}
   Black content & Grey content \\
 \arrayrulecolor{black}
 \cmidrule(lr){1-1}%
 \arrayrulecolor{gray}
 \cmidrule(lr){2-2}
  Black content & Grey content \\
\Bottomrule
\end{tabular}

\end{document}

enter image description here

  • Thanks, this seems useful. However, \hhline doesn't incorporate the spacing around rules inserted by the booktabs rules, which I'm assuming is part of the problem here. – cfr Dec 28 '16 at 21:31

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.