3

When I am making a table with colored first raw using xcolor for my thesis, vertical line in first raw behaves differently in random (as visible in image "with color"). When I avoid coloring (just uncommenting "\usepackage[table]{xcolor}" and "\rowcolor{gray}") it is behaving normal without any problem (as visible in image "without color"). What is the problem here and what is simplest and effective solution?. Why some vertical lines (column separator) are darker than others?.

\documentclass[a4paper,11pt]{book}
\usepackage[table]{xcolor}


\begin{document}
\begin{tabular}{ |c|c|c|c|c|c|c|c|c| }
\hline
\rowcolor{gray}
  Hours of incubation  & pH 5.0 & pH 6.0 & pH 7.0 & pH 7.5 & pH 8.0 & pH 8.5 & pH 9.0 & pH 10 \\ \hline 
12 & 0.00 & 0.0238 & 0.4036 & 0.4397 & 0.4493 & 0.4349 & 0.3191 & 0.005 \\\hline
24  &   0.00  &     0.1319 &    1.7929 &    1.8368 &    1.8629 &    1.8032 &    1.4087 &    0.035\\\hline

\end{tabular}
\end{document}

with color`without color

2

2 Answers 2

3

This is a well known issue, mostly connected to your PDF-reader. If you zoom in far enough, you will probably see the lines again.

In addition, LaTeX is focused on good typography, and in good typography horizontal lines and colour should not be mixed in tables. Also vertical lines should not be used.

Read the documentation to the package booktabs to learn how to build good looking tabulars.

If you insist on using colour and lines simultaneously, I suggest that you take a closer look at cals, which handles cell background colour and rules. Below is the source for such tabular. I have also included two other tabulars, using tabular, booktabsand array, and without mixing horizontal rules and colours, and without vertical rules. When you are using \rowcolor you should not remove the tabular's side bearings (@{}, because then `@{} do not remove the colour from the side bearing:

calstable

\documentclass[a4paper,11pt]{book}
\usepackage{xcolor}
\usepackage{cals}

\begin{document}

\begin{calstable}[c]

% Defining column relativ to each other and relativ to the margins
\colwidths{{\dimexpr(1.5\columnwidth)/9\relax}
            {\dimexpr(\columnwidth)/9\relax}
            {\dimexpr(\columnwidth)/9\relax}
            {\dimexpr(\columnwidth)/9\relax}
            {\dimexpr(\columnwidth)/9\relax}
            {\dimexpr(\columnwidth)/9\relax}
            {\dimexpr(\columnwidth)/9\relax}
            {\dimexpr(\columnwidth)/9\relax}
            {\dimexpr(\columnwidth)/9\relax}
            }
% The tabular fills the text area

% 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{0.4pt}
\def\cals@bodyrs@width{0.8pt}
\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{}
\setlength{\cals@paddingR}{3pt}
\setlength{\cals@paddingR}{3pt}

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


% R1
\thead{\bfseries\footnotesize
\brow
    \gray\alignC\cell{\vfil Hours of incubation }
    \cell{\vfil pH 5.0}
    \cell{\vfil pH 6.0}
    \cell{\vfil pH 7.0}
    \cell{\vfil pH 7.5}
    \cell{\vfil pH 8.0}
    \cell{\vfil pH 8.5}
    \cell{\vfil pH 9.0}
    \cell{\vfil pH 10}\gray
\erow
\mdseries
}
\tfoot{%
    \lastrule\nointerlineskip%
    %\textit{\strut Some table caption%
    %}%
    \par%
    }
% R2 Body
\brow
    \alignC\cell{12}
    \cell{0.00}
    \cell{0.0238}
    \cell{0.4036}
    \cell{0.4397}
    \cell{0.4493}
    \cell{0.4349}
    \cell{0.3191}
    \cell{0.005}
\erow
% R3 Body
\brow
    \alignC\cell{24}
    \cell{0.00}
    \cell{0.1319}
    \cell{1.7929}
    \cell{1.8368}
    \cell{1.8629}
    \cell{1.8032}
    \cell{1.4087}
    \cell{0.035}
\erow
\makeatletter

\end{calstable}\par % \par to align the tabular

\end{document}

enter image description here

Alternatives

Below is also a tabular using \rowcolor, but without rules, and a booktabs-tabular. Be aware that both those tabulars are wider than the \linewidth, but I assumed you are using different margins in your document. If not, try using \footnotesize to reduce the width:

\documentclass[a4paper,11pt]{book}
\usepackage[table]{xcolor}
\usepackage{array, booktabs}


\begin{document}
{\setlength{\extrarowheight}{2pt}\centering
\begin{tabular}{>{\centering}p{2cm}cccccccr}
\rowcolor{gray!30}
Hours of incubation  & pH 5.0 & pH 6.0 & pH 7.0 & pH 7.5 & pH 8.0 & pH 8.5 & pH 9.0 & pH 10 \\
12 & 0.00 & 0.0238 & 0.4036 & 0.4397 & 0.4493 & 0.4349 & 0.3191 & 0.005 \\
24  &   0.00  &     0.1319 &    1.7929 &    1.8368 &    1.8629 &    1.8032 &    1.4087 &    0.035\\\bottomrule
\end{tabular}
}

\vspace{3\baselineskip}

{%\setlength{\extrarowheight}{2pt}
\centering
\begin{tabular}{@{}>{\centering}p{2cm}cccccccr@{}}

\toprule
Hours of incubation  & pH\,5.0 & pH\,6.0 & pH\,7.0 & pH\,7.5 & pH\,8.0 & pH\,8.5 & pH\,9.0 & pH\,10\\\midrule
12 & 0.00 & 0.0238 & 0.4036 & 0.4397 & 0.4493 & 0.4349 & 0.3191 & 0.005 \\
24  &   0.00  &     0.1319 &    1.7929 &    1.8368 &    1.8629 &    1.8032 &    1.4087 &    0.035\\\bottomrule
\end{tabular}
}

\end{document}

enter image description here

4
  • Regarding the cals package: did you ever tried to add a color in a cell? e.g. with \cell{\color{green}body 1.1}? Jul 4, 2019 at 8:07
  • It not only colors the rest of the table but also all following text. And it is a problem as it indicates that it doesn't use color groups correctly. I wouldn't trust a package which contains such an error. Jul 4, 2019 at 8:24
  • cells are groups, add e.g. \bfseries in a group and you will see. But the package doesn't use color safe boxing commands. Jul 4, 2019 at 8:44
  • Not we", *you should issue a bug report. I don't use or recommend the package in answers. Jul 4, 2019 at 9:15
1

The package nicematrix has tools designed to address that kind of problem. With its environment {NiceTabular}, the rules won't seem to disappear, whatever PDF viewer you use, at all the levels of zoom.

\documentclass[a4paper,11pt]{book}
\usepackage{xcolor,nicematrix}

\begin{document}

\begin{NiceTabular}{ccccccccc}[colortbl-like,hvlines]
\rowcolor{gray}
Hours of incubation & pH 5.0 & pH 6.0 & pH 7.0 & pH 7.5 & pH 8.0 & pH 8.5 & pH 9.0 & pH 10 \\ 
12 & 0.00 & 0.0238 & 0.4036 & 0.4397 & 0.4493 & 0.4349 & 0.3191 & 0.005 \\
24 & 0.00 & 0.1319 & 1.7929 & 1.8368 & 1.8629 & 1.8032 & 1.4087 & 0.035 \\
\end{NiceTabular}

\end{document}

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

Output of the above code

3
  • 1
    +1: A very nice result that (after installing the updated package) required only minimal changes to my code: replacing "tabular" with "nicetable" in \begin{tabular} and \endtabular}. Still get a warning "! LaTeX3 Error: The key property '.str_set:N' is unknown.", but this doesn't seem to cause any serious upset if the warning is ignored and compilation proceeds. Mar 1, 2022 at 3:22
  • @Jason Whyte. Good remark. Indeed, I have used in the code of nicematrix the key .str_set:N which is available only in recent versions of the LaTeX kernel (≥ 2021-10-30). However, I will use a workaround in the next release of nicematrix and it will be compatible with (for instance) the actual version of Overleaf. Mar 1, 2022 at 10:45
  • @FPantigny I solved the problem through stumbling upon this Stack Exchange question: tex.stackexchange.com/questions/576918/… and following the advice in a comment of running (in mac terminal) fmtutil-sys --all. Compiles with no problem now, so maybe this will save you some work. Mar 2, 2022 at 6:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .