Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

\rowcolors{1}{gray!20}{white}

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:

\begin{table}[!ht]
\rowcolors{1}{gray!20}{white}
\begin{tabular}{|p{0.184\linewidth}|p{0.452\linewidth}*{4}{|c}|}\hline
\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
\end{tabular}
\end{table}

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.

share|improve this question

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

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

1 Answer 1

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

\documentclass{article}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\let\oldmc\multicolumn
\makeatletter
\newcommand{\mcinherit}{% Activate \multicolumn inheritance
  \renewcommand{\multicolumn}[3]{%
    \oldmc{##1}{##2}{\ifodd\rownum \@oddrowcolor\else\@evenrowcolor\fi ##3}%
  }}
\makeatother
\begin{document}
\begin{table}[!ht]
  \rowcolors{1}{gray!20}{white} \mcinherit
  \begin{tabular}{|>{\bfseries}p{0.184\linewidth}|p{0.452\linewidth}*{4}{|c}|}\hline
    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
  \end{tabular}
\end{table}
\end{document}

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.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.