2

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

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

  • Regarding the cals package: did you ever tried to add a color in a cell? e.g. with \cell{\color{green}body 1.1}? – Ulrike Fischer Jul 4 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. – Ulrike Fischer Jul 4 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. – Ulrike Fischer Jul 4 at 8:44
  • Not we", *you should issue a bug report. I don't use or recommend the package in answers. – Ulrike Fischer Jul 4 at 9:15

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.