GeneralWhen using colortbl \multicolumn redefines the colour

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
bramp
Posts: 10
Joined: Mon Aug 25, 2008 5:33 pm

When using colortbl \multicolumn redefines the colour

Post by bramp »

Hi,
So I have the following code:

Code: Select all

\documentclass{article}

\usepackage{array}
\usepackage{colortbl}

\begin{document}

\begin{table}
    \begin{tabular}{ccc}
\rowcolor[rgb]{1,0,0}
    Hello & \multicolumn{2}{c}{Goodbye}
    \end{tabular}
\end{table}

\end{document} 
I was hoping that would print a row where the background is all red, however, the use of \multicolumn resets the colour specified, and thus I get the first cell red, and the last (Goodbye) cells white.

This can be fixed if I do:

Code: Select all

\begin{table}
    \begin{tabular}{ccc}
\rowcolor[rgb]{1,0,0}
    Hello & \multicolumn{2}{>{\columncolor[rgb]{1,0,0}}c}{Goodbye}
    \end{tabular}
\end{table}
But look how ugly that has become, and I have to write the colour red twice now. Is there some way I can make multicolumn not reset the background color? Or can I some how create a newcommand which will use whatever colortbl sets to make the background color change.

thanks for any help
Andrew

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

bramp
Posts: 10
Joined: Mon Aug 25, 2008 5:33 pm

When using colortbl \multicolumn redefines the colour

Post by bramp »

Ok, it appears no one has been able to answer my question, so in lieu of a solution I was wondering if anyone could suggest a hack.

I was thinking of doing this:

Code: Select all

\newcommand{\row}{
% set a variable to the row colour << This is the bit I don't know how to do
\rowcolor[rgb]{1,0,0}
}
\newcommand{\altrow}{
% set a variable to the row colour << This is the bit I don't know how to do
\rowcolor[rgb]{0,1,0}
}
\newcommand{\mymulticolumn}[3]{
 \multicolumn{#1}{>{\MYROWCOLORVARIABLE}#2}{#3}
}

\begin{table}
    \begin{tabular}{ccc}
\row
    Hello & \mymulticolumn{2}{c}{Goodbye}
\altrow
    Hello row 2 & \mymulticolumn{2}{c}{Goodbye}
....
    \end{tabular}
\end{table}
So basically I want to create my own command to store the colour for that row. Then I make my own multicolumn command which can take advantage of this row colour. Hopefully that makes sense?

thanks
Andrew

P.S My thesis is due Tuesday (GMT Time), so I would be very grateful if someone has a solution before then. Otherwise I'm sticking with my original PDF export of a table from word.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

When using colortbl \multicolumn redefines the colour

Post by Juanjo »

Try this:

Code: Select all

\documentclass{article}

\usepackage{array}
\usepackage{colortbl}

\begin{document}

\newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}}
\newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}}

\definecolor{MyRed}{rgb}{1,0.2,0.1}
\definecolor{MyBlue}{rgb}{0.1,0,0.9}

\begin{tabular}{ccc}
  \SetRowColor{green}
    AAAA & \mymulticolumn{2}{c}{BBBB} \\
  \SetRowColor{magenta}
    CCCC & DDDD & EEEE \\
  \SetRowColor{MyBlue}
    FFFF & \mymulticolumn{2}{c}{GGGG} \\
  \SetRowColor{MyRed}
    HHHH & IIII & JJJJ
\end{tabular}

\end{document} 
The argument of \SetRowColor should be the name of a colour, either predefined (red, blue, green, magenta, yellow...) or defined by you through \definecolor.

Hope that helps.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
bramp
Posts: 10
Joined: Mon Aug 25, 2008 5:33 pm

Re: When using colortbl \multicolumn redefines the colour

Post by bramp »

That is awesome. It works for me for the moment.

Originally I was using the xcolor package, which allows me to define alternating row columns at the very top of the time (using the \rowcolors command). Trying to adapt this solution to xcolor didn't work, as I couldn't figure out the correct variable to use. I tried CT@row@color, and some others, but nothing work.

Ah well, for the moment this solution is good enough for me.

thanks
Post Reply