2

When using \cellcolor or \rowcolor to shade a cell (or row of cells) in a table, the vertical and horizontal separators become randomly obscured or hidden when viewing the PDF. This is true for Chrome, Acrobat, and TexStudio's built-in PDF viewer.

I am required to comply with a style guide, and am allowed to use Word (!) or Tex, but the product must be a PDF. The style guide was given in Word, with a table with a shaded top row of cells. I am attempting to re-create this with LaTeX, but the PDF never renders well. The vertical and top horizontal bars around the shaded cells appear and disappear depending on zoom level. For example, see the images below, from three different zoom levels:

Zoom level 1:

Zoom level 1

Zoom level 2:

Zoom level 2

Zoom level 3:

Zoom level 3

After searching online and seeing several other posts about this, I employed and compiled many of them into this MWE. Zero of the six options work or render consistently across all zoom levels.

I have tried: tabular, tabularx, using a hack via a one-column "multicolumn," hhline, etc. Please see the MWE code below.

If anyone has an idea of how to shade a cell in a table without these rendering issues, please help!

MWE:

\documentclass[twocolumn]{article}
\usepackage{lipsum,tabularx,booktabs,tabularx,hhline}
\usepackage[table]{xcolor}
%opening

\begin{document}

Acrobat, Chrome, and TexStudio's native PDF viewer have issues showing the 
vertical bars between and above cells which specify either cell color or row 
color.  Example tables follow:

\begin{table}
    \centering\setlength{\aboverulesep}{0pt}\setlength{\belowrulesep}{0pt}
    \setlength{\extrarowheight}{2pt}
    \caption{As can be seen, the vertical bars between X, Y, and Z are 
    invisible depending on the level of zoom.  The horizontal bar above is also 
    invisible, depending on zoom.}
    \begin{tabular}{|l|l|l|}
        \toprule
        \rowcolor[gray]{0.85}
        X & Y & Z \\
        \midrule
        0 & 1 & 2\\\hline
        0.1 & 0.2 & 0.3\\
        \bottomrule
    \end{tabular}
\end{table}

\begin{table}
    \centering
    \caption{TabularX and the X column type do not help, though this is what I 
    need to use.  Both toprule and hline exhibit this behavior, regarding the 
    top line.}
    \begin{tabularx}{\columnwidth}{|X|X|X|}
        \hline
        \rowcolor[gray]{0.85}
        X & Y & Z \\\hline
        0 & 1 & 2\\\hline
        0.1 & 0.2 & 0.3\\\hline
    \end{tabularx}
\end{table}

\begin{table}
    \centering
    \caption{Multicolumn doesn't help either.}
    \begin{tabularx}{\columnwidth}{|X|X|X|}
        \hline
        \rowcolor[gray]{0.85}
        \multicolumn{1}{|X|}{X} & Y & Z \\\hline
        0 & 1 & 2\\\hline
        0.1 & 0.2 & 0.3\\\hline
    \end{tabularx}
\end{table}

\begin{table}
    \centering
    \caption{hhline is also ineffective at treating this issue.}
    \begin{tabularx}{\columnwidth}{|X|X|X|}
        \hhline{---}
        \rowcolor[gray]{0.85}
        \multicolumn{1}{|X|}{X} & Y & Z \\\hhline{---}
        0 & 1 & 2\\\hhline{---}
        0.1 & 0.2 & 0.3\\\hhline{---}
    \end{tabularx}
\end{table}

\begin{table}
    \centering
    \caption{hhline does not help in regular tabular mode either.}
    \begin{tabular}{|l|l|l|}
        \hhline{---}
        \rowcolor[gray]{0.85}
        X & Y & Z \\\hhline{---}
        0 & 1 & 2\\\hhline{---}
        0.1 & 0.2 & 0.3\\\hhline{---}
    \end{tabular}
\end{table}

\begin{table}[tbph]
    \centering
    \caption{Just to be clear, rowcolor and cellcolor have the same issue, and 
    specifying vertical bars with hhline does not help. Another post suggested 
    putting cellcolor in brackets, which seemed pointless, but I tried.  It was 
    indeed pointless.}
    \begin{tabularx}{\columnwidth}{|X|X|X|}
        \hhline{|-|-|-|}
        {\cellcolor[gray]{.87}}X & {\cellcolor[gray]{.87}}Y & 
        {\cellcolor[gray]{.87}}Z\tabularnewline\hhline{|-|-|-|}
        0 & 1 & 2\\\hhline{---}
        0.1 & 0.2 & 0.3\\\hhline{---}
    \end{tabularx}

\end{table}

\end{document}

  • 1
    You could give calstable a try (examples: https://tex.stackexchange.com/a/530849/134144 and https://tex.stackexchange.com/a/496788/134144) or, depending on the contents of your table, a tikz matrix could also work (example: https://tex.stackexchange.com/a/497080/134144) – leandriis May 05 '20 at 17:29
  • use of booktabs package and vertical rules doesn't really work. If you really must do this (and using rules and coloured panels isn't ever going to look nice) then you can overlay the rules over a table – David Carlisle May 05 '20 at 17:29
  • I was just writing a new version of nicematrix which tries to solve that problem. If you want, I can send you this beta version and you will test it. If you agree, send me a mail (you will find my address in the documentation of nicematrix). – F. Pantigny May 06 '20 at 12:51
  • Your package is pretty awesome for matrices! I'm not quite ready to attempt another solution since I have a workaround right now, but thank you. I will definitely keep your package in mind for matrix formatting though! – Matt Stein May 06 '20 at 15:07

2 Answers2

2

Thanks to Leandriis for pointing me in the right direction with calstable. I took code from one of those links and modified it to make it work for this specific example. People can expand upon it easily since this is a nice, clean, minimal example table.

Final result looks exactly how I expected: a simple table with shaded cells, and rules that appear correctly in any PDF viewer.

calstable working example

\begin{calstable}[c]
    % Defining columns relative to each other and relative to the margins
    \colwidths{{\dimexpr(\columnwidth)/3\relax}
        {\dimexpr(\columnwidth)/3\relax}
        {\dimexpr(\columnwidth)/3\relax}
    }

    % Set up the tabular
    \makeatletter
    \def\cals@framers@width{0.4pt}   % Outside frame rules, reduce if the rule is too heavy
    \def\cals@framecs@width{0.4pt}
    \def\cals@bodyrs@width{0.4pt}
    \cals@setpadding{Ag}
    \cals@setcellprevdepth{Al}
    \def\cals@cs@width{0.4pt}             % Inside rules, reduce if the rule is too heavy
    \def\cals@rs@width{0.4pt}
    \def\cals@bgcolor{}

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

    % Header row, can add alignL/C/R to each cell if necessary
    \thead{%\bfseries
        \brow
        \gray\alignL\cell{\vfil X}
        \cell{\vfil Y}
        \cell{\vfil Z}\gray
        \erow
        \mdseries
    }
    \tfoot{\lastrule\strut}

    % Row 1 (below header)
    \brow
    \alignL\cell{\vfil 0}
    \cell{\vfil 1}
    \cell{\vfil 2}
    \erow

    % Row 2
    \brow
    \alignL\cell{\vfil 0.1}
    \cell{\vfil 0.2}
    \cell{\vfil 0.3}
    \erow

    \makeatletter
\end{calstable}
\end{table}
0

With nicematrix, you have a solution. The color panels are drawn before the rules (but you need several compilations).

\documentclass[twocolumn]{article}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{document}

\begin{table} \centering \caption{With NiceTabularX} \begin{NiceTabularX}{\columnwidth}{XXX}[colortbl-like,hvlines] \rowcolor[gray]{0.85} X & Y & Z \ 0 & 1 & 2\ 0.1 & 0.2 & 0.3\ \end{NiceTabularX} \end{table}

\end{document}

Output of the above code

F. Pantigny
  • 40,250
  • My humble suggestion. Can you put another MWE with a stretch table like between an image of the question? All the best. – Sebastiano Nov 03 '20 at 12:51
  • 1
    @Sebastiano: The problem is that, as of now, there is no letter type X (like the X of tabularx) in the environements of nicematrix... – F. Pantigny Nov 04 '20 at 21:20
  • I hope you are not angry with my observation. I always wish you the best. – Sebastiano Nov 04 '20 at 21:23
  • 1
    It's a good suggestion for a future version of nicematrix. I will try add a type X... – F. Pantigny Nov 04 '20 at 21:36
  • I'm happy also for your future implementation of your package. – Sebastiano Nov 04 '20 at 21:37
  • 1
    @Sebastiano: Since there is now an environment {NiceTabularX} (v. 6.0 of nicematrix), I have put an example with {NiceTabularX} similar to those of the OP... :-) – F. Pantigny Aug 24 '21 at 17:38
  • Very kind (and colleague) F. Pantigny you're great! :-) Thank you very much for your advice and for a wonderful package. I wait also some improved on cascade package :-). Now I not have understood the negative vote. Boh! – Sebastiano Aug 24 '21 at 21:10