3

I have a table in my documentation, where i'm switching the rowcolor between white(default) and gray. Those rows, where the color is changed with \rowcolor{gray}, disabling the horizontal and vertical lines?

\begin{table}[h]
\begin{tabular}{|l|p{7cm}|}
    \hline  
    \rowcolor{Blackgray}
    \textcolor{white}{\textit{Column 1}}                &       \textcolor{white}{\textit{Column 2}}                        \\      \hline 
    Fooo -- Bar                                         &       Some text here with enum:
                                                                \begin{itemize}
                                                                    \item Item1
                                                                    \item Item2
                                                                    \item ...
                                                                \end{itemize}                                               \\      \hline
    \rowcolor{Gray} 
    Foo -- Baar                                         &       Some text here....                                          \\      \hline
    Fooo -- Baaaar                                      &       Some other text here                                        \\      \hline
    \rowcolor{Gray}
    Baar -- FOOO                                        &       Nope
                                                                \begin{itemize}
                                                                    \item Item1
                                                                \end{itemize}                                               \\      \hline
\end{tabular} 
\end{table}

enter image description here

What is wrong with my latex? why are there no vertical and horizontal lines? What should i include in my latex?

1
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – Thruston
    Feb 25 '15 at 11:35
4

This happens because colortbl draws the rows over the columns.

Adopt this trick.

Add the following lines in your preamble:

\usepackage{etoolbox}

\makeatletter
\patchcmd{\@classz}
  {\CT@row@color}
  {\oldCT@column@color}
  {}
  {}
\patchcmd{\@classz}
  {\CT@column@color}
  {\CT@row@color}
  {}
  {}
\patchcmd{\@classz}
  {\oldCT@column@color}
  {\CT@column@color}
  {}
  {}
\makeatother

MWE (I've changed your custom colors)

\documentclass[10pt,a4paper]{report}
\usepackage{colortbl}
\usepackage{xcolor}

\usepackage{etoolbox}

\makeatletter
\patchcmd{\@classz}
  {\CT@row@color}
  {\oldCT@column@color}
  {}
  {}
\patchcmd{\@classz}
  {\CT@column@color}
  {\CT@row@color}
  {}
  {}
\patchcmd{\@classz}
  {\oldCT@column@color}
  {\CT@column@color}
  {}
  {}
\makeatother

\begin{document}

\begin{table}[h]
\begin{tabular}{|l|p{7cm}|}
    \hline
    \rowcolor{gray}
    \textcolor{white}{\textit{Column 1}}                &       \textcolor{white}{\textit{Column 2}}                        \\      \hline
    Fooo -- Bar                                         &       Some text here with enum:
                                                                \begin{itemize}
                                                                    \item Item1
                                                                    \item Item2
                                                                    \item ...
                                                                \end{itemize}                                               \\      \hline
    \rowcolor{lightgray}
    Foo -- Baar                                         &       Some text here....                                          \\      \hline
    Fooo -- Baaaar                                      &       Some other text here                                        \\      \hline
    \rowcolor{lightgray}
    Baar -- FOOO                                        &       Nope
                                                                \begin{itemize}
                                                                    \item Item1
                                                                \end{itemize}                                               \\      \hline
\end{tabular}
\end{table}

\end{document} 

Output:

enter image description here

3
  • Thank you very much @karlkoeller , it works really fine! Can perhaps exmaple the lines what you included there ?
    – T.C.
    Feb 25 '15 at 12:24
  • @T.C. Of course. colortbl.sty issues \CT@column@color\CT@row@color in this order (format the columns and then the rows). My trick changes it to \CT@row@color\CT@column@color so that the columns are formatted as last. Feb 25 '15 at 12:38
  • @T.C. In simple words my trick does: temp <- a, a <- b, b <- temp to switch a and b. Feb 25 '15 at 13:04
0

In case it helps, I've noticed that replacing \hline with the appropriate one of \toprule, \midrule, and \bottomrule revives the horizontal lines in a table of mine that has coloured rows.

1
  • The question here asks also about the vertical rules which \toprule, \midrule and \bottomrule disregards by design. Moreover, using a 1-to-1 replacement will clearly show some "white lines" as well, since that's how the booktabs draws these.
    – Werner
    Dec 4 '16 at 17:47
0

The package nicematrix provides an environment {NiceTabular} with tools which have been designed to address that kind of problem.

With {NiceTabular}, you will have a perfect result in the PDF viewers (at all levels of zoom) because the colored panels are filled before the rules are drawn thanks to PGF/Tikz and informations written on the |aux| file.

However, you need several compilations.

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{document}

\begin{table}[h]
\begin{NiceTabular}{lp{7cm}}[hvlines,colortbl-like]
\rowcolor{gray}%
\textcolor{white}{\textit{Column 1}} & \textcolor{white}{\textit{Column 2}} \\  
Fooo -- Bar                          & Some text here with enum:
                                       \begin{itemize}
                                           \item Item1
                                           \item Item2
                                           \item ...
                                       \end{itemize}                        \\ 
\rowcolor{lightgray} %
Foo -- Baar                          & Some text here....                   \\ 
Fooo -- Baaaar                       & Some other text here                 \\ 
\rowcolor{lightgray}%
Baar -- FOOO                         & Nope
                                       \begin{itemize}
                                           \item Item1
                                       \end{itemize}                        \\ 
\end{NiceTabular} 
\end{table}

\end{document}

Output of the above code

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.