[Tex/LaTex] LaTeX doesn’t recognize \columncolor from the colortbl package when when it’s in a command

colortblhtlatex

LaTeX doesn't recognize the \columncolor command from the colortbl package when when it's in a commmand.

Here is an example

\documentclass{article}
\usepackage{colortbl}
\newcommand{\cBlue}{\columncolor[rgb]{0,.6,1}}
%\newcommand{\Blue}{\color[rgb]{0,.6,1}}
\begin{document}
\begin{tabular}{ >{\cBlue} cc}
  Blue&White\\
  Blue&White
\end{tabular}

\end{document}

I get the error

! Undefined control sequence.
\cBlue ->\columncolor 
                      [rgb]{0,.6,1}
l.7   B
       lue&White\\
? 

Interestingly, htlatex sails through: it gives the expected output of blue colored column.

Note that there is no problem if I replace \columncolor by \color.

Also, \rowcolor is also fine, as you can test it on this example

\documentclass{article}
\usepackage{colortbl}
%\newcommand{\cBlue}{\columncolor[rgb]{0,.6,1}}
\newcommand{\rBlue}{\rowcolor[rgb]{0,.6,1}}
%\newcommand{\Blue}{\color[rgb]{0,.6,1}}

\begin{document}
\begin{tabular}{cc}
  \rBlue Blue&Blue\\
  White&White
\end{tabular}

\end{document}

Best Answer

As the error message correctly shows, \columncolor is not defined by the colortbl package.

It is just used as a placeholder in a modified array preamble parser so that the colour specification can be extracted and re-inserted where needed.

Depending on the actual use case you can parameterise the colour used by defining a columntype or by defining a colour name

\documentclass{article}
\usepackage{colortbl}

\newcolumntype{B}{>{\columncolor[rgb]{0,.6,1}}}
\definecolor{myblue}{rgb}{0,.6,1}

\begin{document}
\begin{tabular}{ Bcc>{\columncolor{myblue}}c}
  Blue&White&Blue\\
  Blue&White&Blue
\end{tabular}

\end{document}

\color and \rowcolor are defined TeX commands that act at the point they are used. \columncolor (because it seemed a good idea at the time sometime in the last millennium) doesn't act at that point (or act at all, there is no command of that name, the internals use it in a similar way to the way [] are used to delimit optional arguments, to delimit a colour specification that is extracted and used in the table internals.

Related Question