I am trying to create a table with alternating rowcolors. This works fine in itself with a command like:


However, from here it gets a bit more complex. Since the last line of my table need 6 columns, I need to use the \multicolumn comment, which causes all multicolumn parts to not inherit the color as specified by the \rowcolors command.

Code example:

\bf{Item} & \multicolumn{5}{l|}{\bf{Description}}\\ \hline
\bf{Goal} & \multicolumn{5}{l|}{Goal of this}\\ \hline
\bf{Procedure} & \multicolumn{5}{l|}{How to do it}\\ \hline
\bf{Specification} & \multicolumn{5}{l|}{Think of this and that while doing this} \\ \hline
\bf{Comments} & & \bf{Pass} & \bf{Fail} & \bf{Pass} & \bf{Fail}\\ \cline{3-6}
& & $\circ$ & $\circ$ & $\circ$ & $\circ$\\ \hline

Specifying the \multicolumn color like:

\multicolumn{5}{>{\columncolor{gray!20}{some text here}}l|}{}

would force the multicolumn color, however I require the {gray!20} part to be inherited from the \rowcolors command for that row.

migrated from stackoverflow.com Jan 12 '12 at 14:09

This question came from our site for professional and enthusiast programmers.


One way to do this is to modify the \multicolumn command to pick up the row colour on the fly. Row colours are kept in two macros:

  • \@oddrowcolor - for odd rows
  • \@evenrowcolor - for even rows

while the counter \rownum keeps track of the current row number. Here's a minimal working example showing the result of the modification, activated by the newly-created \mcinherit:

enter image description here

\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\newcommand{\mcinherit}{% Activate \multicolumn inheritance
    \oldmc{##1}{##2}{\ifodd\rownum \@oddrowcolor\else\@evenrowcolor\fi ##3}%
  \rowcolors{1}{gray!20}{white} \mcinherit
    Item          & \multicolumn{5}{l|}{\textbf{Description}}\\ \hline
    Goal          & \multicolumn{5}{l|}{Goal of this}\\ \hline
    Procedure     & \multicolumn{5}{l|}{How to do it}\\ \hline
    Specification & \multicolumn{5}{l|}{\cellcolor{green}Think of this and that while doing this} \\ \hline
    Comments      & & \bf{Pass} & \bf{Fail} & \bf{Pass} & \bf{Fail}\\ \cline{3-6}
                  & & $\circ$ & $\circ$ & $\circ$ & $\circ$\\ \hline

I took the liberty of modifying your column formatting as well. For example, with column 1 containing only boldface entries, this can be formatted with the column specification using >{\bfseries} - this inserts \bfseries before every entry (support provided by the array package).

Note that this redefinition of \multicolumn still allows you to change the cell colour to something else, since the third argument is inserted after the colour change. So, a local change will override the inherited colour. With a little work, it would be possible to automate the inheritance based on whether or not you use row colours. However, the extent of your usage is not known at the moment, so the switch* \mcinherit will suffice.

Finally, use \textbf instead of \bf. For more on this, see Will two-letter font style commands (\bf , \it , …) ever be resurrected in LaTeX?

* Note that the redefinition should be made local if you're intermixing some coloured and uncoloured tables. This is "automatic" if you're calling \mcinherit from within a table environment. Again, with some work the redefinition could be made more accommodating.

  • Thank you very much, if have spent quit a lot of time trying to find this one out. And indeed, two-letter font style commands should be resurrected. ;) – Martijn Kriek Feb 15 '12 at 18:18

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.