17

I am making a table that needs every other row colored. I thought to use this.

\usepackage[table,svgnames]{xcolor}
\rowcolors{1}{white}{gray!15}

But my table has vertical bars

\begin{tabular}{r|*{3}{p{2in}|}}

as well as horizontal bars

\multicolumn{1}{c}{\ }
&\multicolumn{1}{c}{Monday}  
&\multicolumn{1}{c}{Wednesday}  
&\multicolumn{1}{c}{Friday} \\ \hline

and the color makes them disappear (the bar from the "r|" stays as I have it above, but if I change the first multicolumn's "\" to "week" then this bar also disappears). Am I missing something; is there a way to keep the bars and get the colors behind them? (I have a 2010 TeX Live.)

5
  • 1
    that is always a problem with the pdf viewer. The printed output should be ok.
    – user2478
    Jul 4 '11 at 14:08
  • You could give the tabu package a try. Jul 4 '11 at 17:24
  • I'm using beamer so the pdf viewer is where it will be seen. I'll look at tabu, thanks. Jul 4 '11 at 18:41
  • The tabu package isn't showing the row in color at all for me; maybe I'm doing it wrong but I copied the rowcolors example from the manual. I guess I'll stick with xcolor. (I am using AcrobatReader9 on Ubuntu 11; I believe that I need AR because I do some other things that seem to me to require it.) Thanks for the suggestions. Jul 4 '11 at 19:22
  • @Jim: Could you give a complete example of the kind of table you are trying to produce (ideally in a full minimal working example (MWE), which will make it easier for people to try out the code and come up with solutions).
    – Jake
    Jul 7 '11 at 1:18
22

Before this stays completely unanswered, may I propose a solution that won't appeal to purists, but that might be a good approach for you.

There have been a couple of questions concerned with how TikZ matrices can be used as replacements for tables: TikZ matrix as a replacement for tabular, Horizontal row separation line in tikz matrix (like \hline in tabular). Since you're using beamer, you have TikZ loaded already, so this wouldn't require additional packages.

You can use matrix options like every even row/.style,every odd column/.style, row 1/.style and so on to adjust the appearance of your tables. Here's an example based on the snippets in your question:

tikz matrix as a table

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\tikzset{ 
    table/.style={
        matrix of nodes,
        row sep=-\pgflinewidth,
        column sep=-\pgflinewidth,
        nodes={
            rectangle,
            draw=black,
            align=center
        },
        minimum height=1.5em,
        text depth=0.5ex,
        text height=2ex,
        nodes in empty cells,
%%
        every even row/.style={
            nodes={fill=gray!20}
        },
        column 1/.style={
            nodes={text width=2em,font=\bfseries}
        },
        row 1/.style={
            nodes={
                fill=black,
                text=white,
                font=\bfseries
            }
        }
    }
}

\begin{tikzpicture}

\matrix (first) [table,text width=6em]
{
& Monday   & Tuesday & Wednesday & Thursday & Friday\\
1   & A & B & C & D & E \\
2   & F & G & H & J & K \\
3   & A & B & C & D & E \\
4   & F & G & H & J & K \\
};


\end{tikzpicture}
\end{document}
3
  • Thank you. I was unaware of that option. I'll check it out. Jul 7 '11 at 10:01
  • I really enjoyed your clear solution!
    – Googlebot
    Mar 16 '12 at 17:40
  • This is looking amazing. What I am missing in comparison to a regular tabular is the ability to set the width of the whole table and to have the columns automatically set their width based on the content.
    – mat
    May 13 at 11:19
1

Encouraged by nice @F. Pantigny answer (+1) here is solution with use of the new tabularray package:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularray}

\begin{document}

\begin{tblr}{colspec  = {c},
             hlines,   vlines,
             row{odd} = {bg=gray!30},
             row{1}   = {bg=black,fg=white,font=\bfseries},
             }
    & Monday
        & Tuesday
            & Wednesday
                & Thursday
                    & Friday\\
 1  & A & B & C & D & E \\
 2  & F & G & H & J & K \\
 3  & A & B & C & D & E \\
 4  & F & G & H & J & K \\
\end{tblr}

\end{document}

After one compilation, the result is as follows:

enter image description here

4
  • When I look the resulting PDF with SumatraPDF (which uses MuPDF as PDF renderer) I see irregular vertical rules... Jul 16 at 15:13
  • @F.Pantigny, you are right, however in the Reader this pdf viewer artifacts are not visible. Hopefully, your comment will read author of the package.
    – Zarko
    Jul 16 at 16:47
  • I think it is the correct result. We should draw vlines if the cells do not span multi columns.
    – L.J.R.
    Jul 17 at 0:27
  • In Adobe Acrobat Pro 2020, vertical rules disappear at some levels of zoom (and also in Sumatra PDF) and the question of the OP was: how to avoid that problem... Jul 17 at 10:31
1

The package nicematrix has tools specifically designed to address that kind of problem.

That package provides an environment {NiceTabular} similar to the classical environment {tabular} (of array) but with tools to color rows, columns and cells with a perfect result in all PDF viewers, at all levels of zoom.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{>{\bfseries}cccccc}[hvlines]
\CodeBefore
  \rowcolors{2}{white}{gray!15}
  \rowcolor{black}{1}
\Body
\RowStyle[color=white]{}
& Monday & Tuesday & Wednesday & Thursday & Friday\\
1 & A & B & C & D & E \\
2 & F & G & H & J & K \\
3 & A & B & C & D & E \\
4 & F & G & H & J & K \\
\end{NiceTabular}

\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.