3

I would like to alternate row colors in a tabular environment. I found colortbl useful. To simplify writing, I built the following macros to alternate row colors (see MWE below). However, I cannot understand why the \crc macro cannot coexist with a \multicolumn, whereas \rca or \rcb work like a charm.

And, more important, how can I simplify writing such as I always use the same macro name to alternate colors, even when I'm using \multicolumn?

\documentclass {article}

    \usepackage {colortbl}

\begin {document}

% define row colors A and B
\def\rca{\rowcolor [rgb] {0.90, 0.95, 1.0}}
\def\rcb{\rowcolor [rgb] {0.80, 0.90, 1.0}}

% This one works like a charm
\begin {tabular} {|l|l|} \hline
    \rca \multicolumn {1} {|c|} {one} & 1 \\ \hline
    \rcb two & 2 \\ \hline
    \rca three & 3 \\ \hline
\end {tabular}

% use current row color and set color for next row
\def\cseta{\gdef\crc{\rca\csetb}}
\def\csetb{\gdef\crc{\rcb\cseta}}
\cseta          % (re)set \crc to the row color A

% This one does not want to compile!
% ! Misplaced \omit.
% \multispan ->\omit
%                   \@multispan
% l.23     \crc \multicolumn {1} {|c} {one}
%                                          & 1 \\ \hline
\begin {tabular} {|l|l|} \hline
    \crc \multicolumn {1} {|c} {one} & 1 \\ \hline
    \crc two & 2 \\ \hline
    \crc three & 3 \\ \hline
\end {tabular}

\end {document}
  • I don't think that this is a duplicate. The problem is here that due to the inner \gdef you have a non-expandable command before the \multicolumn. – Ulrike Fischer Jun 8 '16 at 8:16
1

\multicolumn (and \rowcolor) wants to be the first thing in a cell. Some content before is possible but it must be expandable, and expand only to spaces.

You are using a \gdef before \multicolumn and this is not expandable and so won't work.

\documentclass{article}

\usepackage{colortbl}

\newcommand\CAred{\rowcolor{red}}
\DeclareRobustCommand\CBred{\rowcolor{red}}
\newcommand\CCred[1][]{\rowcolor{red}} 
\newcommand\CD{ } 
\newcommand\CE{\,} 
\begin{document}

\begin{tabular}{l}
\rowcolor{red}\multicolumn{1}{r}{blub}\\ %work
\CAred\multicolumn{1}{r}{blub}        \\ %work
%\CBred\multicolumn{1}{r}{blub}       \\ %fails (robust command)
%\CCred\multicolumn{1}{r}{blub}       \\ %fails (optional argument -> not expandable
\CD\multicolumn{1}{r}{blub}           \\ %works
%\CE\multicolumn{1}{r}{blub}          \\ %fails (content)
%\relax\multicolumn{1}{r}{blub}       \\ %fails
%\def\xy{blub}\multicolumn{1}{r}{blub}\\ %fails (definition)
%\parindent=10pt \multicolumn{1}{r}{blub}\\ %fails (assignment)
%blub\multicolumn{1}{r}{blub}         \\ %fails (content)
\end{tabular}

\end{document}
  • Thanks for this explanation. Do you have a clue to help me solve my problem (use the same macro name to alternate colors)? – pdagog Jun 13 '16 at 7:24
  • Change the color at the end of the previous cell or in a \noalign. – Ulrike Fischer Jun 13 '16 at 7:26
0

Thanks to Ulrike Fischer suggestion about \noalign, here is a working solution:

\documentclass {article}

    \usepackage {colortbl}

\begin {document}

% define row colors A and B
\def\rca{\rowcolor [rgb] {0.90, 0.95, 1.0}}
\def\rcb{\rowcolor [rgb] {0.80, 0.90, 1.0}}

\begin {tabular} {|l|l|} \hline
    \rca \multicolumn {1} {|c|} {one} & 1 \\ \hline
    \rcb two & 2 \\ \hline
    \rca three & 3 \\ \hline
\end {tabular}

% use current row color and set color for next row
\def\cseta{\noalign {\gdef\crc{\rca\csetb}}}
\def\csetb{\noalign {\gdef\crc{\rcb\cseta}}}
\gdef\crc{\rca\csetb}               % (re)set \crc to the row color A

\begin {tabular} {|l|l|} \hline
    \crc \multicolumn {1} {|c} {one} & 1 \\ \hline
    \crc two & 2 \\ \hline
    \crc three & 3 \\ \hline
\end {tabular}

\end {document}
  • In fact, the real solution to my problem with alternating colors was \rowcolors with the xcolor package (using \usepackage [table] {xcolor} or \documentclass [xcolor=table] {beamer}) – pdagog Jun 14 '16 at 13:58

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.