20 Comments

  1. To do alternating row colors, you can also include the xcolor package with the table option (i.e. \usepackage[table]{xcolor}), and immediately preceding the tabular environment use the command

    \rowcolors{start}{color1}{color2}

    — this will produce a table with colored rows, starting at the row indicated by start, with odd rows colored color1 and even rows colored color2.

    A bit simpler than putting the rowcolor command before each row.

  2. Julia Sokol

    For some reason using the \rowcolors command seems to do strange things to the table’s border colours. Especially if this table moves to a different part of the page. Very frustraiting.

    • Hi Julia,

      Could you send a small code example for illustration. I’d be interested to see that and might be able to help…

  3. Mallika Thomas

    Do you know whether it’s possible to have the highlighting get turned on piecewise in a beamer presentation?

    • Hi Mallika,

      Either you could repeat the entire table as shown in the code below or use a more sophisticated solution proposed here.

      \documentclass{beamer}
      \usepackage{colortbl}
      \begin{document}
      
      \begin{frame}
      \begin{table}
      \caption{Table}
      \begin{tabular}{ccc}
      \hline \hline
      First&Second&Third\\\hline%
      \only<1>{%
      A1&B1&C1\\
      \rowcolor{gray}A2&B2&C2\\
      A3&B3&C3\\
      A4&B4&C4\\[-\normalbaselineskip]
      }
      \only<2>{%
      A1&B1&C1\\
      \rowcolor{gray}A2&B2&C2\\
      A3&B3&C3\\
      \rowcolor{gray}A4&B4&C4\\[-\normalbaselineskip]
      }
      \\\hline\hline
      \end{tabular}
      \end{table}
      \end{frame}
      \end{document}
  4. Thanks for this – really helpful. I just wanted to say that \columncolor doesn’t work well with \cline – the colour of the column obscures the line – and that if you wish to colour a table that uses \cline you should switch to \hhline instead.

    • tom

      Hi there,

      Thanks for your comment. Here is a simple example:

      \documentclass[11pt]{article}
      \usepackage{xcolor, colortbl}
      \begin{document}
      \begin{table}[ht]
      \centering
      \begin{tabular}{|cc|}
      \hline
      white&\cellcolor{gray}gray\\
      \cellcolor{gray}gray&white\\
      \hline
      \end{tabular}
      \end{table}%
      \end{document}

      I know there’s an issue with figures and attachement on my blog. Will fix it asap.

      Cheers, Tom.

  5. Vincent

    Hi Tom,

    I have been trying to re-do your combination example, but the borders of some columns are misteriously disappearing… Here is my code :

    \documentclass[a4paper]{report}
    
    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}
    \usepackage[french]{babel}
    \usepackage{color, colortbl}
    \definecolor{BlueTable}{rgb}{0.30,0.58,0.93}
    
    \begin{document}
    \begin{table}[H]
    	\centering
    	\newcolumntype{b}{>{\columncolor{BlueTable}}c}
    	\begin{tabular}{|c|b|b|b|b|b|b|}
    	col1 & col2 & col3 & col4 & col5 & col6 & col7 \\
    	\rowcolor{white}
    	a & b & c & d & e & f & g \\
    	0 & 1 & 2 & 3 & 4 & 5 & 6 \\
    	\rowcolor{white}
    	9 & 8 & 7 & 6 & 5 & 4 & 3 \\
    	z & y & x & w & v & u & t \\
    	\end{tabular}
    \end{table}
    \end{document}

    Some vertical lines are missing and the one to the left is disappearing only for the white rows.
    Thank you in advance for your help.
    Vincent

    • tom

      Hi Vincent,

      Thanks for the nice code example. Unfortunately, I’m not able to reproduce the issue. I can see all vertical lines. Make sure it’s not just the rendering on your screen by zooming in onto the table.

      You may want to try the following line of code. Let me know if the vertical lines still disappear.

      \begin{tabular}{|c|*{6}{>{\hspace{-\tabcolsep}\,}b< {\hspace{-\tabcolsep}\,}|}}[\sourcecode] I got the code from here.

  6. Muhammad Muaz

    Thank you very much. Can you also tell us about a package that colors a figure just like ‘colortbl’ colors table rows.

    • tom

      Hi Muhammad,

      You’d have to be a bit more specific. A figure is usually loaded from a separate file. Therefore, I don’t see how you’d want to color it. See the PGF/TikZ package for how to generate figures with LaTeX.

      Best, Tom

Leave a Reply